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
148 changes: 146 additions & 2 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 android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.format.DateUtils
import android.widget.Button
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
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 @@ -35,13 +45,25 @@ class MainActivity : AppCompatActivity() {
private lateinit var countdownText: TextView
private lateinit var countdownSecondsEditText: EditText
private lateinit var countdownStartButton: Button
private lateinit var countdownFrameLayout: 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

private var disposableTimer: Disposable? = null
private var disposableStopwatch: Disposable? = null

enum class StopwatchState {
IS_RUNNING, IS_PAUSED, IS_STOPPED
}

private var stopwatchState: StopwatchState = StopwatchState.IS_STOPPED
private var stopwatchTime = 0L

//@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -51,10 +73,132 @@ class MainActivity : AppCompatActivity() {
countdownText = findViewById(R.id.countdownText)
countdownSecondsEditText = findViewById(R.id.countdownEditText)
countdownStartButton = findViewById(R.id.countdownStartButton)
countdownFrameLayout = findViewById(R.id.countdownFrameLayout)

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

// Часы
getTime()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { item ->
clockText.text = item
}

//Таймер
countdownStartButton.setOnClickListener {
disposableTimer?.dispose()
val inputSeconds = countdownSecondsEditText.text.toString().toLongOrNull() ?: 0L
disposableTimer = timer(inputSeconds)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ item ->
countdownText.text = item
},
{

},
{
frameBlink()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { item -> countdownFrameLayout.setBackgroundColor(item) }
}
)

}

//Секундомер
stopwatchStartButton.setOnClickListener {
when (stopwatchState) {
StopwatchState.IS_STOPPED, StopwatchState.IS_PAUSED -> {
stopwatchState = StopwatchState.IS_RUNNING
stopwatchStartButton.text = "START"
stopwatchEndButton.text = "PAUSE"
}
StopwatchState.IS_RUNNING -> {
stopwatchTime = 0
}
}
disposableStopwatch?.dispose()
disposableStopwatch = getStopwatch(stopwatchTime)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
stopwatchText.text = it

}

}

stopwatchEndButton.setOnClickListener {
disposableStopwatch?.dispose()
when (stopwatchState) {
StopwatchState.IS_RUNNING -> {
stopwatchState = StopwatchState.IS_PAUSED
stopwatchStartButton.text = "RESUME"
stopwatchEndButton.text = "STOP"

}
StopwatchState.IS_PAUSED -> {
stopwatchState = StopwatchState.IS_STOPPED
stopwatchTime = 0L
stopwatchText.text = ""
stopwatchStartButton.text = "START"

}
else -> {}
}
}
}

private fun getTime(): Observable<String> {
return Observable.interval(1000, TimeUnit.MILLISECONDS)
.map { getTimeStr() }
}

private fun getTimeStr(): String {
val date = Calendar.getInstance().time
val timeFormat = SimpleDateFormat.getTimeInstance()
return timeFormat.format(date)
}

private fun timer(time: Long): Observable<String> {
return Observable.interval(1000, TimeUnit.MILLISECONDS)
.takeWhile { it <= time }
.map { timerCountdown(time, it) }
}

private fun timerCountdown(time: Long, tick: Long): String {
return DateUtils.formatElapsedTime(time - tick)
}

private fun frameBlink(): Observable<Int> {
return Observable.interval(200, TimeUnit.MILLISECONDS)
.takeWhile { it < 5 }
.map { t -> getColorByNum(t.toInt()) }

}

private fun getColorByNum(num: Int): Int {
return if (num % 2 == 0) Color.GRAY else Color.GREEN
}

private fun getStopwatch(curTime: Long): Observable<String> {
return Observable.interval(1L, TimeUnit.MILLISECONDS)
.map { t -> getStopwatchTime(t + curTime) }
}

private fun getStopwatchTime(time: Long): String {
stopwatchTime = time
val sdf = SimpleDateFormat("HH:mm:ss:SS", Locale.getDefault())
sdf.timeZone = TimeZone.getTimeZone("3")
val timeMain = sdf.format(time)
return timeMain.toString()
}

}
11 changes: 7 additions & 4 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/countdownFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
Expand All @@ -50,7 +51,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:inputType="number" />
android:inputType="number"
tools:ignore="SpeakableTextPresentCheck" />
</com.google.android.material.textfield.TextInputLayout>

<TextView
Expand All @@ -59,12 +61,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 Down Expand Up @@ -136,6 +138,7 @@
android:layout_height="wrap_content"
android:text="Pause" />
</LinearLayout>

</FrameLayout>

</LinearLayout>