Fixed issue #77 Do not stop the timer at closing the app

This commit is contained in:
Pavol Franek 2020-03-09 06:49:49 +01:00
parent 44ac30ada4
commit f6d9c5aece
6 changed files with 25 additions and 34 deletions

View File

@ -377,5 +377,3 @@ fun Context.checkAlarmsWithDeletedSoundUri(uri: String) {
dbHelper.updateAlarm(it)
}
}
val Context.preferences: SharedPreferences get() = PreferenceManager.getDefaultSharedPreferences(this)

View File

@ -167,7 +167,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
* sensitive.
*
* @throws IllegalArgumentException if either {@code type} or {@code label}
* have already been registered on this type adapter.
* have already been registered on this type adapter.
*/
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type, String label) {
if (type == null || label == null) {
@ -186,7 +186,7 @@ public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
* name}. Labels are case sensitive.
*
* @throws IllegalArgumentException if either {@code type} or its simple name
* have already been registered on this type adapter.
* have already been registered on this type adapter.
*/
public RuntimeTypeAdapterFactory<T> registerSubtype(Class<? extends T> type) {
return registerSubtype(type, type.getSimpleName());

View File

@ -1,22 +1,22 @@
package app.common
package com.simplemobiletools.clock.extensions.gson
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.TypeAdapterFactory
import com.simplemobiletools.clock.extensions.gson.RuntimeTypeAdapterFactory
import com.simplemobiletools.clock.services.TimerState
val timerStates = valueOf<TimerState>()
.registerSubtype(TimerState.Idle::class.java)
.registerSubtype(TimerState.Start::class.java)
.registerSubtype(TimerState.Running::class.java)
.registerSubtype(TimerState.Pause::class.java)
.registerSubtype(TimerState.Paused::class.java)
.registerSubtype(TimerState.Finish::class.java)
.registerSubtype(TimerState.Idle::class.java)
.registerSubtype(TimerState.Start::class.java)
.registerSubtype(TimerState.Running::class.java)
.registerSubtype(TimerState.Pause::class.java)
.registerSubtype(TimerState.Paused::class.java)
.registerSubtype(TimerState.Finish::class.java)
inline fun <reified T: Any> valueOf(): RuntimeTypeAdapterFactory<T> = RuntimeTypeAdapterFactory.of(T::class.java)
inline fun <reified T : Any> valueOf(): RuntimeTypeAdapterFactory<T> = RuntimeTypeAdapterFactory.of(T::class.java)
fun GsonBuilder.registerTypes(vararg types: TypeAdapterFactory) = apply {
types.forEach { registerTypeAdapterFactory(it) }
}
val gson = GsonBuilder().registerTypes(timerStates).create()
val gson: Gson = GsonBuilder().registerTypes(timerStates).create()

View File

@ -26,18 +26,9 @@ import org.greenrobot.eventbus.ThreadMode
class TimerFragment : Fragment() {
lateinit var view: ViewGroup
private var timerState: TimerState = TimerState.Idle
override fun onResume() {
super.onResume()
timerState = requiredActivity.config.timerState
updateViewStates(timerState)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
EventBus.getDefault().register(this)
}
@ -67,9 +58,7 @@ class TimerFragment : Fragment() {
timer_sound.colorLeftDrawable(textColor)
timer_time.setOnClickListener {
EventBus.getDefault().post(TimerState.Idle)
requiredActivity.hideTimerNotification()
requiredActivity.toast(R.string.timer_stopped)
stopTimer()
}
timer_play_pause.setOnClickListener {
@ -77,9 +66,7 @@ class TimerFragment : Fragment() {
}
timer_reset.setOnClickListener {
EventBus.getDefault().post(TimerState.Idle)
requiredActivity.hideTimerNotification()
requiredActivity.toast(R.string.timer_stopped)
stopTimer()
}
timer_initial_time.setOnClickListener {
@ -117,6 +104,12 @@ class TimerFragment : Fragment() {
return view
}
private fun stopTimer() {
EventBus.getDefault().post(TimerState.Idle)
requiredActivity.hideTimerNotification()
requiredActivity.toast(R.string.timer_stopped)
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(state: TimerState.Idle) {
view.timer_time.text = 0.getFormattedDuration()

View File

@ -1,15 +1,13 @@
package com.simplemobiletools.clock.helpers
import android.content.Context
import app.common.gson
import com.google.gson.Gson
import com.simplemobiletools.clock.extensions.gson.gson
import com.simplemobiletools.clock.services.StateWrapper
import com.simplemobiletools.clock.services.TimerState
import com.simplemobiletools.commons.extensions.getDefaultAlarmTitle
import com.simplemobiletools.commons.extensions.getDefaultAlarmUri
import com.simplemobiletools.commons.helpers.ALARM_SOUND_TYPE_ALARM
import com.simplemobiletools.commons.helpers.BaseConfig
import java.sql.Time
class Config(context: Context) : BaseConfig(context) {
companion object {

View File

@ -12,7 +12,10 @@ import android.os.CountDownTimer
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.extensions.getOpenTimerTabIntent
import com.simplemobiletools.clock.extensions.getTimerNotification
import com.simplemobiletools.clock.extensions.secondsToMillis
import com.simplemobiletools.clock.helpers.TIMER_NOTIF_ID
import com.simplemobiletools.clock.helpers.TIMER_RUNNING_NOTIF_ID
import com.simplemobiletools.commons.extensions.getFormattedDuration
@ -87,7 +90,6 @@ class TimerService : Service() {
val pendingIntent = getOpenTimerTabIntent()
val notification = getTimerNotification(pendingIntent, false) //MAYBE IN FUTURE ADD TIME TO NOTIFICATION
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(TIMER_NOTIF_ID, notification)
bus.post(TimerState.Idle)