diff --git a/app/src/main/kotlin/ru/otus/coroutineshomework/ui/login/LoginFragment.kt b/app/src/main/kotlin/ru/otus/coroutineshomework/ui/login/LoginFragment.kt index 06c3afe..d6c870c 100644 --- a/app/src/main/kotlin/ru/otus/coroutineshomework/ui/login/LoginFragment.kt +++ b/app/src/main/kotlin/ru/otus/coroutineshomework/ui/login/LoginFragment.kt @@ -7,6 +7,9 @@ import android.view.ViewGroup import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import ru.otus.coroutineshomework.databinding.ContentBinding import ru.otus.coroutineshomework.databinding.FragmentLoginBinding import ru.otus.coroutineshomework.databinding.LoadingBinding @@ -43,14 +46,14 @@ class LoginFragment : Fragment() { setupLogin() setupContent() - loginViewModel.state.observe(viewLifecycleOwner) { - when(it) { - is LoginViewState.Login -> showLogin(it) + loginViewModel.state.onEach { state -> + when(state) { + is LoginViewState.Login -> showLogin(state) LoginViewState.LoggingIn -> showLoggingIn() - is LoginViewState.Content -> showContent(it) + is LoginViewState.Content -> showContent(state) LoginViewState.LoggingOut -> showLoggingOut() } - } + }.launchIn(viewLifecycleOwner.lifecycleScope) } private fun setupLogin() { diff --git a/app/src/main/kotlin/ru/otus/coroutineshomework/ui/login/LoginViewModel.kt b/app/src/main/kotlin/ru/otus/coroutineshomework/ui/login/LoginViewModel.kt index 5fae38a..0c6a58d 100644 --- a/app/src/main/kotlin/ru/otus/coroutineshomework/ui/login/LoginViewModel.kt +++ b/app/src/main/kotlin/ru/otus/coroutineshomework/ui/login/LoginViewModel.kt @@ -3,11 +3,23 @@ package ru.otus.coroutineshomework.ui.login import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch +import ru.otus.coroutineshomework.ui.login.data.Credentials class LoginViewModel : ViewModel() { - - private val _state = MutableLiveData(LoginViewState.Login()) - val state: LiveData = _state + private val api = LoginApi() + private val _state = MutableStateFlow(LoginViewState.Login()) + val state: StateFlow = _state /** * Login to the network @@ -16,6 +28,9 @@ class LoginViewModel : ViewModel() { */ fun login(name: String, password: String) { // TODO: Implement login + loginFlow(name, password) + .onEach { _state.value = it} + .launchIn(viewModelScope) } /** @@ -23,5 +38,22 @@ class LoginViewModel : ViewModel() { */ fun logout() { // TODO: Implement logout + logoutFlow() + .onEach { _state.value = it} + .launchIn(viewModelScope) } + + private fun loginFlow(name: String, password: String): Flow = flow { + emit(LoginViewState.LoggingIn) + val user = api.login(Credentials(name, password)) + emit(LoginViewState.Content(user)) + }.catch { e -> + emit(LoginViewState.Login(e as Exception?)) + }.flowOn(Dispatchers.IO) + + private fun logoutFlow(): Flow = flow { + emit(LoginViewState.LoggingOut) + api.logout() + emit(LoginViewState.Login()) + }.flowOn(Dispatchers.IO) } diff --git a/app/src/main/kotlin/ru/otus/coroutineshomework/ui/network/NetworkViewModel.kt b/app/src/main/kotlin/ru/otus/coroutineshomework/ui/network/NetworkViewModel.kt index f006e03..e2dd22a 100644 --- a/app/src/main/kotlin/ru/otus/coroutineshomework/ui/network/NetworkViewModel.kt +++ b/app/src/main/kotlin/ru/otus/coroutineshomework/ui/network/NetworkViewModel.kt @@ -4,8 +4,12 @@ import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlin.random.Random @@ -19,6 +23,24 @@ class NetworkViewModel : ViewModel() { fun startTest(numberOfThreads: Int) { // TODO: Implement the logic + viewModelScope.launch { + _running.value = true + _result.value = null + + val requests = (1..numberOfThreads).map { + async { emulateBlockingNetworkRequest() } + } + val results = requests.awaitAll() + val successResults = results.filter { it.isSuccess } + + _result.value = if (successResults.isNotEmpty()) { + successResults.map { it.getOrNull() ?:0}.average().toLong() + } else { + null + } + + _running.value = false + } } private companion object { diff --git a/app/src/main/kotlin/ru/otus/coroutineshomework/ui/timer/TimerFragment.kt b/app/src/main/kotlin/ru/otus/coroutineshomework/ui/timer/TimerFragment.kt index 1b7c0f1..3040ae6 100644 --- a/app/src/main/kotlin/ru/otus/coroutineshomework/ui/timer/TimerFragment.kt +++ b/app/src/main/kotlin/ru/otus/coroutineshomework/ui/timer/TimerFragment.kt @@ -5,9 +5,12 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment +import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import kotlinx.coroutines.Job import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import ru.otus.coroutineshomework.databinding.FragmentTimerBinding @@ -20,10 +23,8 @@ class TimerFragment : Fragment() { private var _binding: FragmentTimerBinding? = null private val binding get() = _binding!! - - private var time: Duration by Delegates.observable(Duration.ZERO) { _, _, newValue -> - binding.time.text = newValue.toDisplayString() - } + private var timerJob: Job? = null + private var timeFlow: MutableStateFlow = MutableStateFlow(0.milliseconds) private var started by Delegates.observable(false) { _, _, newValue -> setButtonsState(newValue) @@ -53,12 +54,20 @@ class TimerFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) savedInstanceState?.let { - time = it.getLong(TIME).milliseconds + timeFlow = MutableStateFlow(it.getLong(TIME).milliseconds) started = it.getBoolean(STARTED) } + + viewLifecycleOwner.lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + timeFlow.collect { duration -> + binding.time.text = duration.toDisplayString() + } + } + } + setButtonsState(started) with(binding) { - time.text = this@TimerFragment.time.toDisplayString() btnStart.setOnClickListener { started = true } @@ -70,16 +79,23 @@ class TimerFragment : Fragment() { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putLong(TIME, time.inWholeMilliseconds) + outState.putLong(TIME, timeFlow.value.inWholeMilliseconds) outState.putBoolean(STARTED, started) } private fun startTimer() { // TODO: Start timer + timerJob = lifecycleScope.launch { + while (isActive) { + delay(100) + timeFlow.emit(timeFlow.value + 100.milliseconds) + } + } } private fun stopTimer() { // TODO: Stop timer + timerJob?.cancel() } override fun onDestroyView() {