From bd74f18192999a75d05549b381b83d5abee7ffa5 Mon Sep 17 00:00:00 2001 From: Gena Date: Sun, 30 Nov 2025 18:18:18 +0300 Subject: [PATCH 1/5] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=201.1=20-=20=D0=A2=D0=B0=D0=B9=D0=BC=D0=B5=D1=80=20=D1=81=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=D0=BC=20coroutines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../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..681b7f4 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 @@ -18,6 +18,7 @@ import kotlin.time.Duration.Companion.milliseconds class TimerFragment : Fragment() { + private var timerJob: Job? = null private var _binding: FragmentTimerBinding? = null private val binding get() = _binding!! @@ -75,11 +76,17 @@ class TimerFragment : Fragment() { } private fun startTimer() { - // TODO: Start timer + + timerJob = viewLifecycleOwner.lifecycleScope.launch { + while (true){ + delay(10) + time += 10.milliseconds + } + } } private fun stopTimer() { - // TODO: Stop timer + timerJob?.cancel() } override fun onDestroyView() { From b14413fe0d3b828d9d01147553314e397fad3f42 Mon Sep 17 00:00:00 2001 From: Gena Date: Sun, 30 Nov 2025 18:30:41 +0300 Subject: [PATCH 2/5] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=201.2=20-=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B2=D0=BE=D0=B4=20?= =?UTF-8?q?=D1=82=D0=B0=D0=B9=D0=BC=D0=B5=D1=80=D0=B0=20=D0=BD=D0=B0=20Kot?= =?UTF-8?q?lin=20Flow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coroutineshomework/ui/timer/TimerFragment.kt | 15 ++++++++++----- 1 file changed, 10 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 681b7f4..c5a7ba1 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 @@ -8,6 +8,7 @@ import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope 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 @@ -19,6 +20,7 @@ import kotlin.time.Duration.Companion.milliseconds class TimerFragment : Fragment() { private var timerJob: Job? = null + private val timeFlow = MutableStateFlow(Duration.ZERO) private var _binding: FragmentTimerBinding? = null private val binding get() = _binding!! @@ -54,12 +56,16 @@ 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() + lifecycleScope.launch { + timeFlow.collect { + time.text = this@TimerFragment.timeFlow.value.toDisplayString() + } + } btnStart.setOnClickListener { started = true } @@ -71,16 +77,15 @@ 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() { - timerJob = viewLifecycleOwner.lifecycleScope.launch { while (true){ delay(10) - time += 10.milliseconds + timeFlow.emit(timeFlow.value + 10.milliseconds) } } } From 1355b340ca000e28ba27aee3cdb7748a8b29155e Mon Sep 17 00:00:00 2001 From: Gena Date: Sun, 30 Nov 2025 18:48:09 +0300 Subject: [PATCH 3/5] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=202.1=20-=20Login=20=D1=81=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=D0=BC=20corout?= =?UTF-8?q?ines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/login/LoginViewModel.kt | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 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..1fa949e 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,52 @@ 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 class LoginViewModel : ViewModel() { private val _state = MutableLiveData(LoginViewState.Login()) val state: LiveData = _state - + val loginApi = LoginApi() /** * 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 { + val credentials = Credentials(name, password) + + try { + val user = withContext(Dispatchers.IO) { + loginApi.login(credentials) + } + _state.value = LoginViewState.Content(user) + } catch (e: Exception){ + _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 f3eb7115694a9136922ac90a951c36140984b901 Mon Sep 17 00:00:00 2001 From: Gena Date: Sun, 30 Nov 2025 19:37:50 +0300 Subject: [PATCH 4/5] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=202.2=20-=20Login=20=D1=81=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=D0=BC=20Kotlin?= =?UTF-8?q?=20Flow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/login/LoginFragment.kt | 17 ++++--- .../ui/login/LoginViewModel.kt | 51 +++++++++++-------- 2 files changed, 41 insertions(+), 27 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..0d95826 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 @@ -11,6 +11,8 @@ import ru.otus.coroutineshomework.databinding.ContentBinding import ru.otus.coroutineshomework.databinding.FragmentLoginBinding import ru.otus.coroutineshomework.databinding.LoadingBinding import ru.otus.coroutineshomework.databinding.LoginBinding +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.launch class LoginFragment : Fragment() { @@ -39,16 +41,17 @@ class LoginFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - 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 { + loginViewModel.stateFlow.collect { viewState -> + when (viewState) { + is LoginViewState.Login -> showLogin(viewState) + LoginViewState.LoggingIn -> showLoggingIn() + is LoginViewState.Content -> showContent(viewState) + 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 1fa949e..c0f573f 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 @@ -5,35 +5,49 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +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 class LoginViewModel : ViewModel() { - private val _state = MutableLiveData(LoginViewState.Login()) - val state: LiveData = _state + private val _stateFlow = MutableStateFlow(LoginViewState.Login()) + val stateFlow: StateFlow = _stateFlow.asStateFlow() val loginApi = LoginApi() + + fun loginFlow(name: String, password: String) = flow { + emit(LoginViewState.LoggingIn) + + val credentials = Credentials(name, password) + + try { + val user = loginApi.login(credentials) + emit(LoginViewState.Content(user)) + } catch (e: Exception){ + emit(LoginViewState.Login(e)) + } + }.flowOn(Dispatchers.IO) + + fun logoutFlow() = flow { + emit(LoginViewState.LoggingOut) + loginApi.logout() + emit(LoginViewState.Login()) + }.flowOn(Dispatchers.IO) + /** * Login to the network * @param name user name * @param password user password */ fun login(name: String, password: String) { - _state.value = LoginViewState.LoggingIn - - - viewModelScope.launch { - val credentials = Credentials(name, password) - - try { - val user = withContext(Dispatchers.IO) { - loginApi.login(credentials) - } - _state.value = LoginViewState.Content(user) - } catch (e: Exception){ - _state.value = LoginViewState.Login(e) + loginFlow(name, password).collect { + _stateFlow.emit(it) } } } @@ -42,13 +56,10 @@ class LoginViewModel : ViewModel() { * Logout from the network */ fun logout() { - _state.value = LoginViewState.LoggingOut - viewModelScope.launch { - withContext(Dispatchers.IO) { - loginApi.logout() + logoutFlow().collect { + _stateFlow.emit(it) } - _state.value = LoginViewState.Login() } } } From 66a13442a6a1dfa2dab2bcf52638782e810c5bcf Mon Sep 17 00:00:00 2001 From: Gena Date: Sun, 30 Nov 2025 20:14:47 +0300 Subject: [PATCH 5/5] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=203=20-=20Speed-test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/network/NetworkViewModel.kt | 22 ++++++++++++++++++- 1 file changed, 21 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..27ed1ba 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,24 @@ class NetworkViewModel : ViewModel() { val result: LiveData = _result fun startTest(numberOfThreads: Int) { - // TODO: Implement the logic + viewModelScope.launch { + _running.value = true + _result.value = null + + val jobs = List(numberOfThreads) { + async { emulateBlockingNetworkRequest() } + } + + val successfulTimeList = jobs.mapNotNull { it.await().getOrNull() } + + val averageTime = if (successfulTimeList.isNotEmpty()) { + successfulTimeList.average().toLong() + } else { + 0L + } + _result.value = averageTime + _running.value = false + } } private companion object {