Skip to content
This repository has been archived by the owner. It is now read-only.
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {

defaultConfig {
applicationId "iam.thevoid.epic.timeapp"
minSdk 21
minSdk 26
targetSdk 31
versionCode 1
versionName "1.0"
Expand Down
182 changes: 166 additions & 16 deletions app/src/main/java/iam/thevoid/epic/timeapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package iam.thevoid.epic.timeapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
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.schedulers.Schedulers
import java.time.Instant
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeUnit
import kotlin.time.DurationUnit
import kotlin.time.toDuration

// Дан экран с готовой разметкой
// Реализовать при помощи RxJava
Expand All @@ -28,33 +38,173 @@ import android.widget.TextView

class MainActivity : AppCompatActivity() {

private val TAG = this::class.simpleName

// Часы:
private lateinit var clockText: TextView

// Обратный отсчёт
private lateinit var countdownText: TextView
private lateinit var countdownSecondsEditText: EditText
private lateinit var countdownStartButton: Button
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 stopWatchText: TextView
private lateinit var stopWatchMillisText: TextView
private lateinit var stopWatchStartButton: Button
private lateinit var stopWatchEndButton: Button

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)
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)


// 1. Часы
clock()

// 2. Таймер
displayCountDown(0) // Начальное значение
timer()


// 3. Секундомер
displayStopWatch() // Начальное значение
stopWatch()
}

private fun clock() {
Observable.interval(1, TimeUnit.SECONDS, Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())

.subscribe {
clockText.text = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"))
}
}

private val delimiter = ":"

private val twoDigits: (Number) -> String =
{ value -> if (value.toLong() < 10) "0$value" else "$value" }

private val formatTime: (Long, Int, Int, Int) -> java.lang.StringBuilder =
{ hours, minutes, seconds, nano ->
StringBuilder()
.append(twoDigits(hours))
.append(delimiter)
.append(twoDigits(minutes))
.append(delimiter)
.append(twoDigits(seconds))
}

private val displayCountDown: (Long) -> Unit =
{ seconds ->
countDownText.text =
seconds.toDuration(DurationUnit.SECONDS).toComponents(formatTime)
}

private fun timer() {
val countDownValue: () -> Long =
{ countDownSecondsEditText.text.toString().toLongOrNull() ?: 0L }

countDownStartButton.setOnClickListener {
val seconds = countDownValue()
Log.i(TAG, "seconds: $seconds")

if (seconds > 0) {
// Время окончания
val end = Instant.now().plusSeconds(seconds)

Observable.interval(1, TimeUnit.SECONDS, Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())

.takeWhile { Instant.now() <= end }

.subscribe {
val remained = end.minusMillis(Instant.now().toEpochMilli())
val duration =
remained.toEpochMilli().toDuration(DurationUnit.MILLISECONDS)
countDownText.text = duration.toComponents(formatTime)
}
}
}
}

private enum class EStopWatchStatus {
STOP,
RUN,
PAUSE
}

private object stopWatchStatus {
var status: EStopWatchStatus = EStopWatchStatus.STOP
var value: Long = 0
}

private val displayStopWatch: () -> Unit = {
stopWatchText.text =
stopWatchStatus.value.toDuration(DurationUnit.MILLISECONDS)
.toComponents(formatTime)
stopWatchMillisText.text =
"${stopWatchStatus.value % 1000 / 100}" // 10-е мс
}

private fun stopWatch() {
stopWatchStartButton.setOnClickListener {
Log.i(TAG, "${stopWatchStatus.status}: ${stopWatchStatus.value}")

if (stopWatchStatus.status == EStopWatchStatus.STOP || stopWatchStatus.status == EStopWatchStatus.PAUSE) {
stopWatchStatus.status = EStopWatchStatus.RUN // Начинаем работу

stopWatchEndButton.text = "Пауза"

val start = Instant.now()
.toEpochMilli() - stopWatchStatus.value // При состоянии паузы "отходим назад"

Observable.interval(100, TimeUnit.MILLISECONDS, Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())

.takeWhile { stopWatchStatus.status == EStopWatchStatus.RUN }

.subscribe {
stopWatchStatus.value = Instant.now().toEpochMilli() - start
displayStopWatch()
}
}
}

stopWatchEndButton.setOnClickListener()
{
Log.i(TAG, "${stopWatchStatus.status}: ${stopWatchStatus.value}")
when (stopWatchStatus.status) {
EStopWatchStatus.RUN -> {
// Приостанавливаем
stopWatchStatus.status = EStopWatchStatus.PAUSE
stopWatchEndButton.text = "Сброс"
}
EStopWatchStatus.PAUSE -> {
// Сброс
stopWatchStatus.status = EStopWatchStatus.STOP
stopWatchStatus.value = 0

stopWatchEndButton.text = "Пауза"

stopwatchText = findViewById(R.id.stopwatchText)
stopwatchMillisText = findViewById(R.id.stopwatchMillisText)
stopwatchStartButton = findViewById(R.id.stopwatchStartButton)
stopwatchEndButton = findViewById(R.id.stopwatchEndButton)
displayStopWatch()
}
EStopWatchStatus.STOP -> {
stopWatchEndButton.text = "Пауза"
}
}
}
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
android:id="@+id/countdownStartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
android:text="Старт" />
</FrameLayout>

<View
Expand Down Expand Up @@ -123,7 +123,7 @@
android:id="@+id/stopwatchStartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
android:text="Старт" />

<View
android:layout_width="0dp"
Expand All @@ -134,7 +134,7 @@
android:id="@+id/stopwatchEndButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
android:text="Пауза" />
</LinearLayout>
</FrameLayout>

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

Expand Down