From f0c5be1196e4badf0614e0ab0ebdf7d7716ff678 Mon Sep 17 00:00:00 2001 From: Karavaev Date: Sun, 25 Jan 2026 17:30:22 +0300 Subject: [PATCH 1/5] 1.1 Timer_by_coroutines --- .../coroutineshomework/ui/timer/TimerFragment.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 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..0b7ab63 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 @@ -21,6 +21,8 @@ class TimerFragment : Fragment() { private var _binding: FragmentTimerBinding? = null private val binding get() = _binding!! + private var timerJob: Job? = null + private var time: Duration by Delegates.observable(Duration.ZERO) { _, _, newValue -> binding.time.text = newValue.toDisplayString() } @@ -75,11 +77,16 @@ class TimerFragment : Fragment() { } private fun startTimer() { - // TODO: Start timer + timerJob = viewLifecycleOwner.lifecycleScope.launch { + while (isActive) { + delay(10) + time += 10.milliseconds + } + } } private fun stopTimer() { - // TODO: Stop timer + timerJob?.cancel() } override fun onDestroyView() { @@ -96,7 +103,7 @@ class TimerFragment : Fragment() { "%02d:%02d.%03d", this.inWholeMinutes.toInt(), this.inWholeSeconds.toInt(), - this.inWholeMilliseconds.toInt() + this.inWholeMilliseconds.toInt() - this.inWholeSeconds.toInt() * 1000 ) } } \ No newline at end of file From 10dee6fd29d8f8dc57bfc33937fa82d668963289 Mon Sep 17 00:00:00 2001 From: Karavaev Date: Sun, 25 Jan 2026 23:48:17 +0300 Subject: [PATCH 2/5] 1.2 Timer_by_flow --- .../ui/timer/TimerFragment.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 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 0b7ab63..23d8ee8 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 @@ -15,6 +15,9 @@ import java.util.Locale import kotlin.properties.Delegates import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds +import kotlinx.coroutines.flow.MutableStateFlow +import androidx.lifecycle.repeatOnLifecycle +import androidx.lifecycle.Lifecycle class TimerFragment : Fragment() { @@ -23,9 +26,7 @@ class TimerFragment : Fragment() { private var timerJob: Job? = null - private var time: Duration by Delegates.observable(Duration.ZERO) { _, _, newValue -> - binding.time.text = newValue.toDisplayString() - } + private var timeFlow = MutableStateFlow(Duration.ZERO) private var started by Delegates.observable(false) { _, _, newValue -> setButtonsState(newValue) @@ -55,12 +56,18 @@ class TimerFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) savedInstanceState?.let { - time = it.getLong(TIME).milliseconds + timeFlow.value = it.getLong(TIME).milliseconds started = it.getBoolean(STARTED) } setButtonsState(started) with(binding) { - time.text = this@TimerFragment.time.toDisplayString() + viewLifecycleOwner.lifecycleScope.launch { + repeatOnLifecycle(Lifecycle.State.STARTED) { + timeFlow.collect { duration -> + binding.time.text = duration.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, timeFlow.value.inWholeMilliseconds) outState.putBoolean(STARTED, started) } @@ -80,7 +87,7 @@ class TimerFragment : Fragment() { timerJob = viewLifecycleOwner.lifecycleScope.launch { while (isActive) { delay(10) - time += 10.milliseconds + timeFlow.emit(timeFlow.value + 10.milliseconds) } } } From 392b468674ad1e4c79de504d186846df9587578e Mon Sep 17 00:00:00 2001 From: Karavaev Date: Mon, 26 Jan 2026 00:59:45 +0300 Subject: [PATCH 3/5] 2.1 Login_by_coroutines --- .../ui/login/LoginViewModel.kt | 30 +++++++++++++++++-- 1 file changed, 28 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..7ec10d4 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,25 +3,51 @@ 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.withContext +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +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 user: User + /** * Login to the network * @param name user name * @param password user password */ fun login(name: String, password: String) { - // TODO: Implement login + _state.value = LoginViewState.LoggingIn + viewModelScope.launch { + try { + withContext(Dispatchers.IO) { + user = loginApi.login(Credentials(name, password)) + } + _state.value = LoginViewState.Content(user) + } catch (e: IllegalArgumentException) { + _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 6d178abaafded732722e4ccadb1eba0747bc751e Mon Sep 17 00:00:00 2001 From: Karavaev Date: Mon, 26 Jan 2026 02:06:18 +0300 Subject: [PATCH 4/5] 2.2 Login_by_flow --- .../ui/login/LoginFragment.kt | 20 +++++++--- .../ui/login/LoginViewModel.kt | 37 ++++++++++++------- 2 files changed, 37 insertions(+), 20 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..b05bb81 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,10 @@ 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.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 @@ -43,12 +47,16 @@ 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() + } + } } } } 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 7ec10d4..716a41b 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 @@ -1,11 +1,14 @@ 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.withContext 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 ru.otus.coroutineshomework.ui.login.data.Credentials import ru.otus.coroutineshomework.ui.login.data.User @@ -13,8 +16,8 @@ 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 user: User @@ -25,15 +28,10 @@ class LoginViewModel : ViewModel() { * @param password user password */ fun login(name: String, password: String) { - _state.value = LoginViewState.LoggingIn + stateFlow.value = LoginViewState.LoggingIn viewModelScope.launch { - try { - withContext(Dispatchers.IO) { - user = loginApi.login(Credentials(name, password)) - } - _state.value = LoginViewState.Content(user) - } catch (e: IllegalArgumentException) { - _state.value = LoginViewState.Login(e) + loginFlow(name, password).collect { + stateFlow.value = it } } } @@ -42,12 +40,23 @@ class LoginViewModel : ViewModel() { * Logout from the network */ fun logout() { - _state.value = LoginViewState.LoggingOut + stateFlow.value = LoginViewState.LoggingOut viewModelScope.launch { withContext(Dispatchers.IO) { loginApi.logout() } - _state.value = LoginViewState.Login() + stateFlow.value = LoginViewState.Login() } } + + private fun loginFlow(name: String, password: String): Flow = flow { + emit(LoginViewState.LoggingIn) + try { + user = loginApi.login(Credentials(name, password)) + emit(LoginViewState.Content(user)) + } catch (e: IllegalArgumentException) { + emit(LoginViewState.Login(e)) + + } + }.flowOn(Dispatchers.IO) } From 9418f117bce74c48f94ada3b332e79817b9bfc69 Mon Sep 17 00:00:00 2001 From: Karavaev Date: Mon, 26 Jan 2026 02:35:11 +0300 Subject: [PATCH 5/5] 3. Speed-test_by_coroutines --- .../ui/network/NetworkViewModel.kt | 18 +++++++++++++++++- 1 file changed, 17 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..031adad 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 @@ -8,6 +8,9 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.isActive import kotlinx.coroutines.withContext import kotlin.random.Random +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.async +import kotlinx.coroutines.launch class NetworkViewModel : ViewModel() { @@ -18,7 +21,20 @@ class NetworkViewModel : ViewModel() { val result: LiveData = _result fun startTest(numberOfThreads: Int) { - // TODO: Implement the logic + + val resultsList = mutableListOf>() + + viewModelScope.launch { + _running.value = true + repeat(numberOfThreads) { + resultsList.add(async { emulateBlockingNetworkRequest() }.await()) + } + + val successResultsList = resultsList.mapNotNull { it.getOrNull() } + + _result.value = successResultsList.average().toLong() + _running.value = false + } } private companion object {