Add sleep timer

This closes #105
This commit is contained in:
Ensar Sarajčić
2023-09-04 10:39:33 +02:00
parent 10c3913fed
commit db5b553ce8
60 changed files with 507 additions and 50 deletions

View File

@ -5,11 +5,14 @@ import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.WindowManager
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.getContrastColor
import com.simplemobiletools.commons.extensions.viewBinding
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.flashlight.databinding.ActivityBrightDisplayBinding
import com.simplemobiletools.flashlight.extensions.config
import com.simplemobiletools.flashlight.helpers.stopSleepTimerCountDown
import com.simplemobiletools.flashlight.models.Events
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
class BrightDisplayActivity : SimpleActivity() {
private val binding by viewBinding(ActivityBrightDisplayBinding::inflate)
@ -45,6 +48,8 @@ class BrightDisplayActivity : SimpleActivity() {
}
}
}
binding.sleepTimerStop.setOnClickListener { stopSleepTimer() }
}
override fun onResume() {
@ -60,6 +65,16 @@ class BrightDisplayActivity : SimpleActivity() {
toggleBrightness(false)
}
override fun onStart() {
super.onStart()
EventBus.getDefault().register(this)
}
override fun onStop() {
super.onStop()
EventBus.getDefault().unregister(this)
}
private fun setBackgroundColor(color: Int) {
binding.apply {
brightDisplay.background = ColorDrawable(color)
@ -77,4 +92,19 @@ class BrightDisplayActivity : SimpleActivity() {
layout.screenBrightness = (if (increase) 1 else 0).toFloat()
window.attributes = layout
}
private fun stopSleepTimer() {
binding.sleepTimerHolder.fadeOut()
stopSleepTimerCountDown()
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun sleepTimerChanged(event: Events.SleepTimerChanged) {
binding.sleepTimerValue.text = event.seconds.getFormattedDuration()
binding.sleepTimerHolder.beVisible()
if (event.seconds == 0) {
finish()
}
}
}