Advertisement
DroidZed

timer service

May 5th, 2025 (edited)
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.82 KB | Software | 0 0
  1. class TimerService : Service(), KoinComponent {
  2.  
  3.     companion object {
  4.         const val TIMER_VAL = "TIMER_VAL"
  5.     }
  6.  
  7.     private val notifManager by inject<NotificationHandler>()
  8.     private val repo by inject<TimerRepo>()
  9.  
  10.     private val timerState = repo.sharedTimerState
  11.         .stateIn(
  12.             serviceScope,
  13.             SharingStarted.WhileSubscribed(5000L),
  14.             TimerGlobalState()
  15.         )
  16.  
  17.     private val tag = "TimerService"
  18.  
  19.     private var notifId by mutableIntStateOf(0)
  20.  
  21.     private lateinit var notif: NotificationCompat.Builder
  22.  
  23.     override fun onBind(p0: Intent?): IBinder? = null
  24.  
  25.     override fun onCreate() {
  26.         super.onCreate()
  27.         notif =
  28.             NotificationCompat
  29.                 .Builder(this, Consts.TIMER_CHANNEL)
  30.                 .setSmallIcon(R.mipmap.ic_launcher_foreground)
  31.                 .setPriority(NotificationManager.IMPORTANCE_HIGH)
  32.                 .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
  33.                 .setContentTitle("KModoro - Timer")
  34.                 .setOngoing(true)
  35.                 .setAllowSystemGeneratedContextualActions(false)
  36.                 .setOnlyAlertOnce(true)
  37.                 .setContentIntent(
  38.                     PendingIntent.getActivity(
  39.                         this,
  40.                         0,
  41.                         Intent(
  42.                             this,
  43.                             MainActivity::class.java
  44.                         ),
  45.                         PendingIntent.FLAG_MUTABLE,
  46.                     ),
  47.                 )
  48.     }
  49.  
  50.     override fun onStartCommand(
  51.         intent: Intent?,
  52.         flags: Int,
  53.         startId: Int,
  54.     ): Int {
  55.         super.onStartCommand(intent, flags, startId)
  56.  
  57.         when (intent?.action) {
  58.             TimerActions.START.name -> {
  59.                 val currentValue =
  60.                     intent.getLongExtra(TIMER_VAL, 0L)
  61.  
  62.                 KLogger.d("onStartCommand: $currentValue", tag = tag)
  63.  
  64.                 startTimer(currentValue)
  65.             }
  66.  
  67.             TimerActions.CLEAR.name -> clearTimer()
  68.             TimerActions.PAUSE.name -> pauseTimer()
  69.             TimerActions.ADD_MINUTE.name -> addOneMinute()
  70.             TimerActions.REPLAY.name -> replayTimer()
  71.         }
  72.         return START_STICKY
  73.     }
  74.  
  75.     private fun addOneMinute() {
  76.  
  77.     }
  78.  
  79.     private fun replayTimer() {
  80.  
  81.     }
  82.  
  83.     private fun startTimer(value: Long) {
  84.         val notification = notif.setContentText(value.formatTime()).build()
  85.  
  86.         serviceScope.launch {
  87.             repo.activateTimer()
  88.             repo.sharedTimerState.collect {
  89.                 updateNotification(it.time)
  90.             }
  91.         }
  92.  
  93.         startForeground(Consts.TIMER_NOTIFICATION_ID, notification)
  94.     }
  95.  
  96.     private fun clearTimer() {
  97.         repo.resetTimer()
  98.         stopSelf()
  99.     }
  100.  
  101.     private fun pauseTimer() {
  102.         serviceScope.launch {
  103.             repo.stopTimer(TimerState.Paused)
  104.         }
  105.     }
  106.  
  107.     private fun updateNotification(value: Long) {
  108.         val notification =
  109.             notif
  110.                 .setContentText(value.formatTime())
  111.                 .setContentIntent(
  112.                     PendingIntent.getActivity(
  113.                         this,
  114.                         0,
  115.                         Intent(
  116.                             this, MainActivity::class.java
  117.                         ).also {
  118.                             it.putExtra("TARGET_SCREEN", Destination.TimerScreen::class.simpleName)
  119.                         },
  120.                         PendingIntent.FLAG_IMMUTABLE,
  121.                     ),
  122.                 ).build()
  123.  
  124.         notifId = notification.number
  125.  
  126.         notifManager.notify(Consts.TIMER_NOTIFICATION_ID, notification)
  127.     }
  128. }
  129.  
  130. enum class TimerActions {
  131.     START,
  132.     CLEAR,
  133.     PAUSE,
  134.     ADD_MINUTE,
  135.     REPLAY,
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement