Skip to content

Instantly share code, notes, and snippets.

View GianpaMX's full-sized avatar

Juan Russell GianpaMX

View GitHub Profile
@GianpaMX
GianpaMX / .adb-ip-address.sh
Last active March 29, 2022 10:48
Prints phone ip address
#!/bin/bash
adb() {
if [[ $@ == "ip" ]]; then
command adb shell netcfg | grep wlan0 | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
else
command adb "$@"
fi
}
@Test
fun `60 seconds clock`() = coroutineRule.testDispatcher.runBlockingTest {
whenever(observeState.invoke()).thenReturn(flowOf(State.Pomodoro(60)))
viewModel = ClockViewModel(
observeState = observeState,
nextState = nextState,
errorChannel = errorChannel,
defaultDispatcher = coroutineRule.testDispatcher
)
class ClockViewModel @Inject constructor(
observeState: ObserveState,
private val nextState: NextState,
private val errorChannel: BroadcastChannel<Throwable>,
private val defaultDispatcher: CoroutineDispatcher
) : ViewModel() {
val viewState: LiveData<ClockViewState> = observeState()
.map { it.toViewState() }
.flowOn(defaultDispatcher)
@Test
fun `Turn on DND when Pomodoro starts`() = runBlockingTest {
zenModeApi.mode = ZenMode.Off
whenever(transitionApi.observeTransitionLog()).thenReturn(
flowOf(
Transition(State.IDLE, 0),
Transition(State.POMODORO, 0),
Transition(State.DONE, 0)
)
)
@Test
fun `Idle State`() = runBlockingTest {
whenever(transitionApi.observeTransitionLog()).thenReturn(flowOf(Transition(State.IDLE, 0)))
val observeStateFlow = observeState()
observeStateFlow.test {
assertThat(expectItem()).isEqualTo(ObserveState.State.Idle)
expectComplete()
}
val errors = errorChannel.asFlow().handleErrors()
private fun Flow<Throwable>.handleErrors(): Flow<Throwable> = flow {
distinctUntilChanged().collect {
if (it is ZenModeApi.AccessDeniedException) {
navigationChannel.send(ClockNavDirection.AskDndPermission)
} else {
emit(it)
}
}
suspend operator fun invoke(action: Action): State {
val currentState = transitionApi
.getLastTransition()
?.state
?: throw IllegalNullStateException
val nextState = when {
currentState == State.IDLE && action == Action.START -> State.POMODORO
currentState == State.POMODORO && action == Action.STOP -> State.IDLE
currentState == State.POMODORO && action == Action.COMPLETE -> State.DONE
suspend operator fun invoke(): State? = when {
transitionApi.getLastTransition() == null -> State.IDLE.also {
transitionApi.newTransition(timeApi.now(), it)
}
else -> null
}
val viewState: LiveData<ClockViewState> = observeState()
.map { it.toViewState() }
.flowOn(defaultDispatcher)
.catch { errorChannel.send(it) }
.asLiveData(viewModelScope.coroutineContext)
override fun observeTransitionLog(): Flow<Transition> = stateLogDao
.observeLast()
.map { it?.toTransition() }
.filterNotNull()