Skip to content
This repository has been archived by the owner. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 133 additions & 4 deletions app/src/main/java/iam/thevoid/epic/timeapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,8 +51,120 @@ class MainActivity : AppCompatActivity() {
// Секундомер
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 disposableCountdown: Disposable? = null
private var disposableStopwatch: Disposable? = null

private fun disposeCountdown() {
disposableCountdown?.dispose()
disposableCountdown = null
}

private fun disposeStopwatch() {
disposableStopwatch?.dispose()
disposableStopwatch = null
}

@SuppressLint("NewApi")
override fun onStart() {
super.onStart()

//1) Отображение часов
Observable.interval(1, TimeUnit.SECONDS)
.map {
SimpleDateFormat.getTimeInstance()
.format(Calendar.getInstance().time)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { i -> clockText.text = i }

//2) Таймер
countdownStartButton.setOnClickListener {
disposeCountdown()
val countdown = countdownSecondsEditText.text.toString()
.toLongOrNull() ?: 0L
disposableCountdown = Observable.interval(1, TimeUnit.SECONDS, Schedulers.io())
.takeUntil { it >= countdown }
.map { s ->
DateUtils
.formatElapsedTime(countdown - s)
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ i ->
countdownText.text = i
countdownText.setTextColor(Color.BLACK)
},
{}
)
{
disposeCountdown()
countdownText.setTextColor(Color.RED)
countdownSecondsEditText.text = null
}
}

//3) Секундомер
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()
disposeCountdown()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -57,4 +181,9 @@ class MainActivity : AppCompatActivity() {
stopwatchStartButton = findViewById(R.id.stopwatchStartButton)
stopwatchEndButton = findViewById(R.id.stopwatchEndButton)
}
}

companion object {
private val LOG_TAG = MainActivity::class.simpleName
}

}