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
144 changes: 140 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,23 @@
package iam.thevoid.epic.timeapp

import androidx.appcompat.app.AppCompatActivity
import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.FrameLayout
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 @@ -35,26 +48,149 @@ class MainActivity : AppCompatActivity() {
private lateinit var countdownText: TextView
private lateinit var countdownSecondsEditText: EditText
private lateinit var countdownStartButton: Button
private lateinit var timerLayoutEdit: FrameLayout

// Секундомер
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

@SuppressLint("SimpleDateFormat")
val dateFormat = SimpleDateFormat("HH:mm:ss")
val formatTimer = DateTimeFormatter.ofPattern("mm:ss")

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

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

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

countdownStartButton.setOnClickListener {
var secondText = countdownSecondsEditText.text.toString()
if (!secondText.isEmpty()) {
countdownStartButton.isEnabled = false
countdownSecondsEditText.isEnabled = false
val secondEdit = secondText.toLong()
var ofSecondOfDay = LocalTime.ofSecondOfDay(secondEdit)
countdownText.setText(
ofSecondOfDay.format(formatTimer)
)
timer(ofSecondOfDay)
}
}

stopwatchText = findViewById(R.id.stopwatchText)
stopwatchMillisText = findViewById(R.id.stopwatchMillisText)
stopwatchStartButton = findViewById(R.id.stopwatchStartButton)
stopwatchEndButton = findViewById(R.id.stopwatchEndButton)
stopwatchText.setText("00:00:00")
stopwatchMillisText.setText("00")
var subscribe: Disposable? = null

var stopwatchState = StopwatchState.STOP
var millisecondsPassed = 0L
stopwatchStartButton.setOnClickListener {
when (stopwatchState) {
StopwatchState.STOP, StopwatchState.PAUSE -> {
stopwatchState = StopwatchState.RUN
stopwatchEndButton.text = "PAUSE"
stopwatchStartButton.text = "START"
stopwatchStartButton.isEnabled = false
}
StopwatchState.RUN -> {
millisecondsPassed = 0L
subscribe?.dispose()
}
}

subscribe = Observable.interval(1, TimeUnit.MILLISECONDS, Schedulers.computation())
.takeWhile { stopwatchState == StopwatchState.RUN }
.map { millisecondsPassed + it }
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
var secondNow = LocalTime.ofSecondOfDay(it / 1000)
stopwatchText.setText(secondNow.format(DateTimeFormatter.ofPattern("HH:mm:ss")))
stopwatchMillisText.setText((it % 1000).toString())
}
}

stopwatchEndButton.setOnClickListener {
when (stopwatchState) {
StopwatchState.RUN -> {
stopwatchState = StopwatchState.PAUSE
stopwatchStartButton.isEnabled = true
stopwatchStartButton.text = "CONTINUE"
stopwatchEndButton.text = "RESET"
millisecondsPassed = LocalTime.parse(stopwatchText.text, DateTimeFormatter.ofPattern("HH:mm:ss")).toSecondOfDay() * 1000L
}
StopwatchState.PAUSE -> {
stopwatchState = StopwatchState.STOP
millisecondsPassed = 0L
stopwatchText.setText("00:00:00")
stopwatchMillisText.setText("00")
stopwatchStartButton.text = "START"
subscribe?.dispose()
}
}
}

}
}

fun getNowTime() {
Observable.interval(1, TimeUnit.SECONDS, Schedulers.computation())
.map {
dateFormat.format(Date())
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe { clockText.setText(it) }
}

private fun timer(ofSecondOfDay: LocalTime) {
var ofSecondOfDay1 = ofSecondOfDay
Observable.interval(1, TimeUnit.SECONDS, Schedulers.computation())
.takeWhile { ofSecondOfDay1.toSecondOfDay() > 0 }
.map { ofSecondOfDay1 = ofSecondOfDay1.minusSeconds(1) }
.observeOn(AndroidSchedulers.mainThread())
.doOnDispose { }
.subscribe(
{ countdownText.setText(ofSecondOfDay1.format(formatTimer)) },
{ Log.d(LOG_TAG, it.message.orEmpty()) },
{
Observable.intervalRange(
0L,
3,
100L,
200L,
TimeUnit.MILLISECONDS,
Schedulers.computation()
)
.map {
if (it % 2L != 0L)
Color.GREEN
else Color.YELLOW
}
.observeOn(AndroidSchedulers.mainThread())
.doOnComplete {
countdownStartButton.isEnabled = true
countdownSecondsEditText.isEnabled = true
timerLayoutEdit.setBackgroundColor(Color.WHITE)
}
.subscribe {
timerLayoutEdit.setBackgroundColor(it)
}
})
}

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

enum class StopwatchState { RUN, PAUSE, STOP }
}
14 changes: 8 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
android:background="#ccc" />

<FrameLayout
android:id="@+id/timerLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
Expand Down Expand Up @@ -59,12 +60,12 @@
tools:text="[02:37]" />

<Button
android:layout_marginStart="16dp"
android:layout_marginBottom="4dp"
android:layout_gravity="bottom"
android:id="@+id/countdownStartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginStart="16dp"
android:layout_marginBottom="4dp"
android:text="Start" />
</FrameLayout>

Expand All @@ -83,7 +84,7 @@
android:text="Секундомер:" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_width="213dp"
android:layout_height="wrap_content"
android:layout_gravity="center">

Expand All @@ -98,14 +99,15 @@

<TextView
android:id="@+id/stopwatchMillisText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="39dp"
android:layout_height="26dp"
android:layout_gravity="bottom"
android:fontFamily="sans-serif-medium"
android:textColor="#2b2b2b"
android:textSize="20sp"
app:layout_constraintBaseline_toBaselineOf="@id/stopwatchText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@id/stopwatchText"
tools:text="[0]" />
</androidx.constraintlayout.widget.ConstraintLayout>
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