From c7dec0b8dcda4913096cfe9b69a591daf9245240 Mon Sep 17 00:00:00 2001 From: "voronkov.konstantin" Date: Mon, 16 May 2022 18:29:11 +0300 Subject: [PATCH] Voronkov.K. hw-2 --- .../iam/thevoid/epic/timeapp/MainActivity.kt | 143 ++++++++++++++++-- 1 file changed, 134 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/iam/thevoid/epic/timeapp/MainActivity.kt b/app/src/main/java/iam/thevoid/epic/timeapp/MainActivity.kt index d08be6e..474b887 100644 --- a/app/src/main/java/iam/thevoid/epic/timeapp/MainActivity.kt +++ b/app/src/main/java/iam/thevoid/epic/timeapp/MainActivity.kt @@ -1,10 +1,22 @@ package iam.thevoid.epic.timeapp -import androidx.appcompat.app.AppCompatActivity +import android.annotation.SuppressLint +import android.graphics.Color import android.os.Bundle +import android.text.format.DateUtils import android.widget.Button import android.widget.EditText import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers +import io.reactivex.rxjava3.core.Observable +import io.reactivex.rxjava3.disposables.Disposable +import io.reactivex.rxjava3.schedulers.Schedulers +import java.text.SimpleDateFormat +import java.time.LocalTime +import java.time.format.DateTimeFormatter +import java.util.* +import java.util.concurrent.TimeUnit // Дан экран с готовой разметкой // Реализовать при помощи RxJava @@ -27,34 +39,147 @@ import android.widget.TextView // состояния паузы class MainActivity : AppCompatActivity() { - // Часы: private lateinit var clockText: TextView - // Обратный отсчёт private lateinit var countdownText: TextView private lateinit var countdownSecondsEditText: EditText private lateinit var countdownStartButton: Button - // Секундомер private lateinit var stopwatchText: TextView private lateinit var stopwatchMillisText: TextView - private lateinit var stopwatchStartButton: EditText - private lateinit var stopwatchEndButton: EditText + private lateinit var stopwatchStartButton: Button + private lateinit var stopwatchEndButton: Button + + enum class StopwatchState { RUN, STOP, PAUSE } + + private var disposableTimer: Disposable? = null + private var disposableStopwatch: Disposable? = null + + private fun disposeTimer() { + disposableTimer?.dispose() + disposableTimer = null + } + + private fun disposeStopwatch() { + disposableStopwatch?.dispose() + disposableStopwatch = null + } + + + @SuppressLint("pop") + override fun onStart() { + super.onStart() + + /** + * Таймер + */ + countdownStartButton.setOnClickListener { + disposeTimer() + val input = countdownSecondsEditText.text.toString() + .toLongOrNull() ?: 0L + disposableTimer = Observable.interval(1, TimeUnit.SECONDS, Schedulers.io()) + .takeUntil { it >= input } + .map { s -> + DateUtils + .formatElapsedTime(input - s) + } + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + { i -> + countdownText.text = i + countdownText.setTextColor(Color.BLACK) + }, + {} + ) + { + disposeTimer() + countdownText.setTextColor(Color.RED) + countdownSecondsEditText.text = null + } + } + + /** + * Секундомер + */ + var stopwatchState = StopwatchState.STOP + var milliseconds = 0L + stopwatchStartButton.setOnClickListener { + when (stopwatchState) { + StopwatchState.PAUSE, StopwatchState.STOP -> { + stopwatchState = StopwatchState.RUN + stopwatchStartButton.text = "START" + stopwatchEndButton.text = "PAUSE" + } + StopwatchState.RUN -> { + milliseconds = 0L + disposeStopwatch() + } + } + //disposeStopwatch() + disposableStopwatch = Observable.interval(1, TimeUnit.MILLISECONDS, Schedulers.io()) + .takeWhile { stopwatchState == StopwatchState.RUN } + .map { milliseconds + it } + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { + stopwatchText.text = LocalTime.ofSecondOfDay(it / 1000) + .format(DateTimeFormatter.ofPattern("HH:mm:ss")) + stopwatchMillisText.text = (it % 1000).toString() + } + } + + stopwatchEndButton.setOnClickListener { + disposeStopwatch() + when (stopwatchState) { + StopwatchState.RUN -> { + stopwatchState = StopwatchState.PAUSE + stopwatchStartButton.text = "RUN" + stopwatchEndButton.text = "STOP" + milliseconds = LocalTime.parse( + stopwatchText.text, + DateTimeFormatter.ofPattern("HH:mm:ss") + ).toSecondOfDay() * 1000L + } + StopwatchState.PAUSE -> { + stopwatchState = StopwatchState.STOP + milliseconds = 0L + stopwatchText.setText("00:00:00") + stopwatchMillisText.setText("00") + stopwatchStartButton.text = "START" + stopwatchEndButton.text = "PAUSE" + disposeStopwatch() + } + } + } + } + + override fun onStop() { + super.onStop() + disposeTimer() + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - clockText = findViewById(R.id.clockText) - countdownText = findViewById(R.id.countdownText) countdownSecondsEditText = findViewById(R.id.countdownEditText) countdownStartButton = findViewById(R.id.countdownStartButton) - stopwatchText = findViewById(R.id.stopwatchText) stopwatchMillisText = findViewById(R.id.stopwatchMillisText) stopwatchStartButton = findViewById(R.id.stopwatchStartButton) stopwatchEndButton = findViewById(R.id.stopwatchEndButton) + + /** + * Часы + */ + Observable.interval(1, TimeUnit.SECONDS) + .map { + SimpleDateFormat.getTimeInstance() + .format(Calendar.getInstance().time) + } + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { i -> clockText.text = i } } } \ No newline at end of file