From 2c9477a73478129474aca149294185dfadccaf41 Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Sun, 11 Jan 2026 10:03:39 +0300 Subject: [PATCH 1/5] Task 1.1 implementation( Timer by coroutines) --- .../otus/coroutineshomework/ui/timer/TimerFragment.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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..eba029c 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 @@ -25,6 +25,8 @@ class TimerFragment : Fragment() { binding.time.text = newValue.toDisplayString() } + private var timerTask: Job? = null + private var started by Delegates.observable(false) { _, _, newValue -> setButtonsState(newValue) if (newValue) { @@ -75,11 +77,16 @@ class TimerFragment : Fragment() { } private fun startTimer() { - // TODO: Start timer + timerTask = viewLifecycleOwner.lifecycleScope.launch { + while (isActive) { + delay(10) + time += 10.milliseconds + } + } } private fun stopTimer() { - // TODO: Stop timer + timerTask?.cancel() } override fun onDestroyView() { From 26d7690cef0af040cd51894424769a226f96526a Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Sun, 11 Jan 2026 10:23:19 +0300 Subject: [PATCH 2/5] Task 1.2 implementation ( converting timer to Kotlin Flow) --- .../ui/timer/TimerFragment.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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 eba029c..7faadff 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 @@ -26,7 +29,7 @@ class TimerFragment : Fragment() { } private var timerTask: Job? = null - + private var timerFlow = MutableStateFlow(Duration.ZERO) private var started by Delegates.observable(false) { _, _, newValue -> setButtonsState(newValue) if (newValue) { @@ -55,12 +58,16 @@ class TimerFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) savedInstanceState?.let { - time = it.getLong(TIME).milliseconds + timerFlow.value = it.getLong(TIME).milliseconds started = it.getBoolean(STARTED) } setButtonsState(started) with(binding) { - time.text = this@TimerFragment.time.toDisplayString() + lifecycleScope.launch { + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { + timerFlow.collect { value -> time.text = value.toDisplayString() } + } + } btnStart.setOnClickListener { started = true } @@ -72,7 +79,7 @@ class TimerFragment : Fragment() { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putLong(TIME, time.inWholeMilliseconds) + outState.putLong(TIME, timerFlow.value.inWholeMilliseconds) outState.putBoolean(STARTED, started) } @@ -80,7 +87,7 @@ class TimerFragment : Fragment() { timerTask = viewLifecycleOwner.lifecycleScope.launch { while (isActive) { delay(10) - time += 10.milliseconds + timerFlow.emit(timerFlow.value + 10.milliseconds) } } } From 7c63386c4d29636b3193d16facfcd64517cdaef3 Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Sun, 11 Jan 2026 10:34:28 +0300 Subject: [PATCH 3/5] Task 1.3 implementation ( Login by coroutines ) --- .../ui/login/LoginViewModel.kt | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) 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..e848d58 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,19 @@ 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.launch +import kotlinx.coroutines.withContext +import ru.otus.coroutineshomework.ui.login.data.Credentials +import ru.otus.coroutineshomework.ui.login.data.User class LoginViewModel : ViewModel() { private val _state = MutableLiveData(LoginViewState.Login()) val state: LiveData = _state + private val loginApi = LoginApi() + private lateinit var userInfo: User /** * Login to the network @@ -15,13 +23,33 @@ class LoginViewModel : ViewModel() { * @param password user password */ fun login(name: String, password: String) { - // TODO: Implement login + + _state.value = LoginViewState.LoggingIn + viewModelScope.launch { + try { + withContext(Dispatchers.IO) { + userInfo = loginApi.login(Credentials(name, password)) + } + _state.value = LoginViewState.Content(userInfo) + } catch (e: IllegalArgumentException) { + withContext(Dispatchers.Main) { + _state.value = LoginViewState.Login(e) + } + } + } } /** * Logout from the network */ fun logout() { - // TODO: Implement logout + + _state.value = LoginViewState.LoggingOut + viewModelScope.launch { + withContext(Dispatchers.IO) { + loginApi.logout() + } + _state.value = LoginViewState.Login() + } } } From 32127c07422f14e3c2130a21c5a25c893058c5e9 Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Sun, 11 Jan 2026 11:20:43 +0300 Subject: [PATCH 4/5] Task 2.2 implementation ( Login using Kotlin Flow ) --- .../ui/login/LoginFragment.kt | 23 +++++++--- .../ui/login/LoginViewModel.kt | 44 ++++++++++++------- 2 files changed, 45 insertions(+), 22 deletions(-) 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..b38b335 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,11 @@ import android.view.ViewGroup import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels +import androidx.lifecycle.Lifecycle +//import androidx.lifecycle.LiveData +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle +import kotlinx.coroutines.launch import ru.otus.coroutineshomework.databinding.ContentBinding import ru.otus.coroutineshomework.databinding.FragmentLoginBinding import ru.otus.coroutineshomework.databinding.LoadingBinding @@ -42,16 +47,20 @@ class LoginFragment : Fragment() { setupLogin() setupContent() - - loginViewModel.state.observe(viewLifecycleOwner) { - when(it) { - is LoginViewState.Login -> showLogin(it) - LoginViewState.LoggingIn -> showLoggingIn() - is LoginViewState.Content -> showContent(it) - LoginViewState.LoggingOut -> showLoggingOut() + lifecycleScope.launch { + viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { + loginViewModel.state.collect { + when (it) { + is LoginViewState.Login -> showLogin(it) + LoginViewState.LoggingIn -> showLoggingIn() + is LoginViewState.Content -> showContent(it) + LoginViewState.LoggingOut -> showLoggingOut() + } + } } } } + private fun setupLogin() { login.loginButton.setOnClickListener { 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 e848d58..ab939c5 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 @@ -4,7 +4,13 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import ru.otus.coroutineshomework.ui.login.data.Credentials @@ -12,10 +18,10 @@ import ru.otus.coroutineshomework.ui.login.data.User class LoginViewModel : ViewModel() { - private val _state = MutableLiveData(LoginViewState.Login()) - val state: LiveData = _state + private val stateFlow = MutableStateFlow(LoginViewState.Login()) + val state: StateFlow = stateFlow private val loginApi = LoginApi() - private lateinit var userInfo: User + private lateinit var userCred: User /** * Login to the network @@ -24,17 +30,11 @@ class LoginViewModel : ViewModel() { */ fun login(name: String, password: String) { - _state.value = LoginViewState.LoggingIn + //_state.value = LoginViewState.LoggingIn + stateFlow.value = LoginViewState.LoggingIn viewModelScope.launch { - try { - withContext(Dispatchers.IO) { - userInfo = loginApi.login(Credentials(name, password)) - } - _state.value = LoginViewState.Content(userInfo) - } catch (e: IllegalArgumentException) { - withContext(Dispatchers.Main) { - _state.value = LoginViewState.Login(e) - } + loginFlow(name, password).collect { + stateFlow.value = it } } } @@ -44,12 +44,26 @@ class LoginViewModel : ViewModel() { */ fun logout() { - _state.value = LoginViewState.LoggingOut + //_state.value = LoginViewState.LoggingOut + stateFlow.value = LoginViewState.LoggingOut viewModelScope.launch { withContext(Dispatchers.IO) { loginApi.logout() } - _state.value = LoginViewState.Login() + //_state.value = LoginViewState.Login() + stateFlow.value = LoginViewState.Login() } } + + private fun loginFlow(name: String, password: String): Flow = flow { + emit(LoginViewState.LoggingIn) + try { + userCred = loginApi.login(Credentials(name, password)) + emit(LoginViewState.Content(userCred)) + } catch (e: IllegalArgumentException) { + emit(LoginViewState.Login(e)) + + } + }.flowOn(Dispatchers.IO) } + From 2deb865b0cac7b785b62bfa5dbf2e42866dd4b09 Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Sun, 11 Jan 2026 11:38:34 +0300 Subject: [PATCH 5/5] Task 3 implementation ( Speed test ) --- .../ui/network/NetworkViewModel.kt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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..32273bd 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,11 @@ 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.isActive +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlin.random.Random @@ -18,7 +21,19 @@ class NetworkViewModel : ViewModel() { val result: LiveData = _result fun startTest(numberOfThreads: Int) { - // TODO: Implement the logic + val results = mutableListOf>() + + viewModelScope.launch { + _running.value = true + repeat(numberOfThreads) { + results.add(async { emulateBlockingNetworkRequest() }.await()) + } + + val successRes = results.mapNotNull { it.getOrNull() } + + _result.value = successRes.average().toLong() + _running.value = false + } } private companion object {