mirror of
				https://github.com/SimpleMobileTools/Simple-Clock.git
				synced 2025-06-05 22:19:17 +02:00 
			
		
		
		
	Handle deletion of timers when running
This commit is contained in:
		@@ -14,7 +14,7 @@ import com.facebook.stetho.Stetho
 | 
			
		||||
import com.simplemobiletools.clock.extensions.getOpenTimerTabIntent
 | 
			
		||||
import com.simplemobiletools.clock.extensions.getTimerNotification
 | 
			
		||||
import com.simplemobiletools.clock.extensions.timerHelper
 | 
			
		||||
import com.simplemobiletools.clock.helpers.TIMER_NOTIF_ID
 | 
			
		||||
import com.simplemobiletools.clock.helpers.INVALID_TIMER_ID
 | 
			
		||||
import com.simplemobiletools.clock.models.TimerEvent
 | 
			
		||||
import com.simplemobiletools.clock.models.TimerState
 | 
			
		||||
import com.simplemobiletools.clock.services.TimerStopService
 | 
			
		||||
@@ -74,6 +74,14 @@ class App : Application(), LifecycleObserver {
 | 
			
		||||
        countDownTimers[event.timerId]?.cancel()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Subscribe(threadMode = ThreadMode.MAIN)
 | 
			
		||||
    fun onMessageEvent(event: TimerEvent.Delete) {
 | 
			
		||||
        countDownTimers[event.timerId]?.cancel()
 | 
			
		||||
        timerHelper.deleteTimer(event.timerId){
 | 
			
		||||
            EventBus.getDefault().post(TimerEvent.Refresh(INVALID_TIMER_ID))
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Subscribe(threadMode = ThreadMode.MAIN)
 | 
			
		||||
    fun onMessageEvent(event: TimerEvent.Start) {
 | 
			
		||||
        val countDownTimer = object : CountDownTimer(event.duration, 1000) {
 | 
			
		||||
@@ -95,7 +103,7 @@ class App : Application(), LifecycleObserver {
 | 
			
		||||
            val pendingIntent = getOpenTimerTabIntent(event.timerId)
 | 
			
		||||
            val notification = getTimerNotification(timer, pendingIntent, false)
 | 
			
		||||
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
 | 
			
		||||
            notificationManager.notify(TIMER_NOTIF_ID, notification)
 | 
			
		||||
            notificationManager.notify(event.timerId, notification)
 | 
			
		||||
            updateTimerState(event.timerId, TimerState.Finished)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,6 @@ import com.simplemobiletools.clock.activities.SimpleActivity
 | 
			
		||||
import com.simplemobiletools.clock.extensions.getFormattedDuration
 | 
			
		||||
import com.simplemobiletools.clock.extensions.hideTimerNotification
 | 
			
		||||
import com.simplemobiletools.clock.extensions.secondsToMillis
 | 
			
		||||
import com.simplemobiletools.clock.extensions.timerHelper
 | 
			
		||||
import com.simplemobiletools.clock.models.Timer
 | 
			
		||||
import com.simplemobiletools.clock.models.TimerEvent
 | 
			
		||||
import com.simplemobiletools.clock.models.TimerState
 | 
			
		||||
@@ -93,9 +92,7 @@ class TimerAdapter(
 | 
			
		||||
            getItem(position)
 | 
			
		||||
        }
 | 
			
		||||
        removeSelectedItems(positions)
 | 
			
		||||
        activity.timerHelper.deleteTimers(timersToRemove) {
 | 
			
		||||
            onRefresh.invoke()
 | 
			
		||||
        }
 | 
			
		||||
        timersToRemove.forEach(::deleteTimer)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun setupView(view: View, timer: Timer) {
 | 
			
		||||
@@ -117,7 +114,7 @@ class TimerAdapter(
 | 
			
		||||
 | 
			
		||||
            timer_reset.applyColorFilter(textColor)
 | 
			
		||||
            timer_reset.setOnClickListener {
 | 
			
		||||
                stopTimer(timer)
 | 
			
		||||
                resetTimer(timer)
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            timer_play_pause.applyColorFilter(if (adjustedPrimaryColor == Color.WHITE) Color.BLACK else Color.WHITE)
 | 
			
		||||
@@ -139,8 +136,13 @@ class TimerAdapter(
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun stopTimer(timer: Timer) {
 | 
			
		||||
    private fun resetTimer(timer: Timer) {
 | 
			
		||||
        EventBus.getDefault().post(TimerEvent.Reset(timer.id!!, timer.seconds.secondsToMillis))
 | 
			
		||||
        simpleActivity.hideTimerNotification()
 | 
			
		||||
        simpleActivity.hideTimerNotification(timer.id!!)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun deleteTimer(timer: Timer) {
 | 
			
		||||
        EventBus.getDefault().post(TimerEvent.Delete(timer.id!!))
 | 
			
		||||
        simpleActivity.hideTimerNotification(timer.id!!)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -148,7 +148,7 @@ fun Context.getOpenTimerTabIntent(timerId: Int): PendingIntent {
 | 
			
		||||
    val intent = getLaunchIntent() ?: Intent(this, SplashActivity::class.java)
 | 
			
		||||
    intent.putExtra(OPEN_TAB, TAB_TIMER)
 | 
			
		||||
    intent.putExtra(TIMER_ID, timerId)
 | 
			
		||||
    return PendingIntent.getActivity(this, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
 | 
			
		||||
    return PendingIntent.getActivity(this, timerId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fun Context.getAlarmIntent(alarm: Alarm): PendingIntent {
 | 
			
		||||
@@ -167,7 +167,7 @@ fun Context.hideNotification(id: Int) {
 | 
			
		||||
    manager.cancel(id)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fun Context.hideTimerNotification() = hideNotification(TIMER_NOTIF_ID)
 | 
			
		||||
fun Context.hideTimerNotification(timerId: Int) = hideNotification(timerId)
 | 
			
		||||
 | 
			
		||||
fun Context.updateWidgets() {
 | 
			
		||||
    val widgetsCnt =
 | 
			
		||||
@@ -321,7 +321,7 @@ fun Context.getTimerNotification(timer: Timer, pendingIntent: PendingIntent, add
 | 
			
		||||
        .setAutoCancel(true)
 | 
			
		||||
        .setSound(Uri.parse(soundUri), STREAM_ALARM)
 | 
			
		||||
        .setChannelId(channelId)
 | 
			
		||||
        .addAction(R.drawable.ic_cross_vector, getString(R.string.dismiss), if (addDeleteIntent) reminderActivityIntent else getHideTimerPendingIntent())
 | 
			
		||||
        .addAction(R.drawable.ic_cross_vector, getString(R.string.dismiss), if (addDeleteIntent) reminderActivityIntent else getHideTimerPendingIntent(timer.id!!))
 | 
			
		||||
 | 
			
		||||
    if (addDeleteIntent) {
 | 
			
		||||
        builder.setDeleteIntent(reminderActivityIntent)
 | 
			
		||||
@@ -339,9 +339,10 @@ fun Context.getTimerNotification(timer: Timer, pendingIntent: PendingIntent, add
 | 
			
		||||
    return notification
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fun Context.getHideTimerPendingIntent(): PendingIntent {
 | 
			
		||||
fun Context.getHideTimerPendingIntent(timerId: Int): PendingIntent {
 | 
			
		||||
    val intent = Intent(this, HideTimerReceiver::class.java)
 | 
			
		||||
    return PendingIntent.getBroadcast(this, TIMER_NOTIF_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
 | 
			
		||||
    intent.putExtra(TIMER_ID, timerId)
 | 
			
		||||
    return PendingIntent.getBroadcast(this, timerId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fun Context.getHideAlarmPendingIntent(alarm: Alarm): PendingIntent {
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,6 @@ const val OPEN_ALARMS_TAB_INTENT_ID = 9996
 | 
			
		||||
const val UPDATE_WIDGET_INTENT_ID = 9997
 | 
			
		||||
const val OPEN_APP_INTENT_ID = 9998
 | 
			
		||||
const val ALARM_NOTIF_ID = 9998
 | 
			
		||||
const val TIMER_NOTIF_ID = 9999
 | 
			
		||||
const val TIMER_RUNNING_NOTIF_ID = 10000
 | 
			
		||||
 | 
			
		||||
const val OPEN_TAB = "open_tab"
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package com.simplemobiletools.clock.models
 | 
			
		||||
 | 
			
		||||
sealed class TimerEvent(open val timerId: Int) {
 | 
			
		||||
    data class Delete(override val timerId: Int) : TimerEvent(timerId)
 | 
			
		||||
    data class Reset(override val timerId: Int, val duration: Long) : TimerEvent(timerId)
 | 
			
		||||
    data class Start(override val timerId: Int, val duration: Long) : TimerEvent(timerId)
 | 
			
		||||
    data class Pause(override val timerId: Int, val duration: Long) : TimerEvent(timerId)
 | 
			
		||||
 
 | 
			
		||||
@@ -4,12 +4,15 @@ import android.content.BroadcastReceiver
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import android.content.Intent
 | 
			
		||||
import com.simplemobiletools.clock.extensions.hideTimerNotification
 | 
			
		||||
import com.simplemobiletools.clock.helpers.INVALID_TIMER_ID
 | 
			
		||||
import com.simplemobiletools.clock.helpers.TIMER_ID
 | 
			
		||||
import com.simplemobiletools.clock.models.TimerState
 | 
			
		||||
import org.greenrobot.eventbus.EventBus
 | 
			
		||||
 | 
			
		||||
class HideTimerReceiver : BroadcastReceiver() {
 | 
			
		||||
    override fun onReceive(context: Context, intent: Intent) {
 | 
			
		||||
        context.hideTimerNotification()
 | 
			
		||||
        val timerId = intent.getIntExtra(TIMER_ID, INVALID_TIMER_ID)
 | 
			
		||||
        context.hideTimerNotification(timerId)
 | 
			
		||||
        EventBus.getDefault().post(TimerState.Idle)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user