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
135 changes: 132 additions & 3 deletions app/src/main/java/iam/thevoid/epic/timeapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package iam.thevoid.epic.timeapp

import androidx.appcompat.app.AppCompatActivity
import android.graphics.Color
import android.os.Bundle
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.util.*
import java.util.concurrent.TimeUnit

// Дан экран с готовой разметкой
// Реализовать при помощи RxJava
Expand Down Expand Up @@ -39,22 +47,143 @@ 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
private var timerDisposable: Disposable? = null
private var pausedStopWatchVal: Long = 0
private var currentStopWatchVal: Long = 0
private val stopWatchDisposables: MutableList<Disposable> = mutableListOf()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


clockText = findViewById(R.id.clockText)
subscribeClock()

countdownText = findViewById(R.id.countdownText)
countdownSecondsEditText = findViewById(R.id.countdownEditText)
countdownStartButton = findViewById(R.id.countdownStartButton)

countdownStartButton.setOnClickListener {
startTimerDown()
}

stopwatchText = findViewById(R.id.stopwatchText)
stopwatchMillisText = findViewById(R.id.stopwatchMillisText)
stopwatchStartButton = findViewById(R.id.stopwatchStartButton)
stopwatchEndButton = findViewById(R.id.stopwatchEndButton)

stopwatchStartButton.setOnClickListener {
startTimerUp()
}

stopwatchEndButton.setOnClickListener {
pauseTimerUp()
}

}

private fun subscribeClock() {
Observable.interval(1000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ clockText.text = getCurrentTime() },
{}
)

}

private fun getCurrentTime(): String {
val calendar: Calendar = Calendar.getInstance()
val simpleDateFormat = SimpleDateFormat("HH:mm:ss", Locale.ENGLISH)
return simpleDateFormat.format(calendar.time)
}

private fun startTimerDown() {
timerDisposable?.dispose()
val text = countdownSecondsEditText.text.toString()
if (text == "") {
countdownText.text = "Введите количество"
} else {
val amountSec = text.toLong()
var currentAmountSec = amountSec
countdownText.setBackgroundColor(Color.WHITE)
timerDisposable =
Observable.intervalRange(amountSec, amountSec + 1, 0, 1000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnComplete { countdownText.setBackgroundColor(Color.BLUE) }
.subscribe(
{
countdownText.text = secondsToTimeString(currentAmountSec)
currentAmountSec--
},
{}
)
}
}

private fun startTimerUp() {
if (stopWatchDisposables.isEmpty()) {
val observable: Observable<Long> = Observable.interval(1, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.doOnDispose { pausedStopWatchVal = currentStopWatchVal }
.map { it + pausedStopWatchVal }
.doOnNext { currentStopWatchVal = it }

stopWatchDisposables.add(observable.observeOn(AndroidSchedulers.mainThread())
.subscribe { stopwatchMillisText.text = "${it % 1000}" })

stopWatchDisposables.add(observable
.filter { it % 1000 == 0L }
.map { it / 1000 }
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
stopwatchText.text = secondsToTimeString(it)
})
stopWatchStartedPerformance()
}
}

private fun secondsToTimeString(seconds: Long): String {
return "${seconds / 3600}:${(seconds % 3600) / 60}:${(seconds % 3600) % 60}"
}

private fun pauseTimerUp() {
stopWatchDispose()
if (currentStopWatchVal == 0L) {
pausedStopWatchVal = 0
stopWatchStoppedPerformance()
} else {
stopWatchPausedPerformance()
}
currentStopWatchVal = 0
}

private fun stopWatchDispose() {
stopWatchDisposables.forEach {
it.dispose()
}
stopWatchDisposables.clear()
}

private fun stopWatchStartedPerformance() {
stopwatchEndButton.text = "Pause"
stopwatchStartButton.text = "Started"
}

private fun stopWatchPausedPerformance() {
stopwatchEndButton.text = "Stop"
stopwatchStartButton.text = "Continue"
}

private fun stopWatchStoppedPerformance() {
stopwatchText.text = secondsToTimeString(0)
stopwatchMillisText.text = "0"
stopwatchEndButton.text = "Stop"
stopwatchStartButton.text = "Start"
}
}