-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from Nexters/feature/service-refactor
Feature: Timer Service Refactoring
- Loading branch information
Showing
16 changed files
with
400 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
presentation/src/main/java/com/pomonyang/mohanyang/presentation/di/PomodoroModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.pomonyang.mohanyang.presentation.di | ||
|
||
import com.pomonyang.mohanyang.data.repository.pomodoro.PomodoroTimerRepository | ||
import com.pomonyang.mohanyang.presentation.service.PomodoroTimer | ||
import com.pomonyang.mohanyang.presentation.service.focus.FocusTimer | ||
import com.pomonyang.mohanyang.presentation.service.rest.RestTimer | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.components.ServiceComponent | ||
|
||
@Module | ||
@InstallIn(ServiceComponent::class) | ||
internal object PomodoroModule { | ||
|
||
@Provides | ||
@FocusTimerType | ||
fun provideFocusTimer( | ||
timerRepository: PomodoroTimerRepository | ||
): PomodoroTimer = FocusTimer(timerRepository = timerRepository) | ||
|
||
@Provides | ||
@RestTimerType | ||
fun provideRestTimer( | ||
timerRepository: PomodoroTimerRepository | ||
): PomodoroTimer = RestTimer(timerRepository = timerRepository) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...on/src/main/java/com/pomonyang/mohanyang/presentation/noti/PomodoroNotificationManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.pomonyang.mohanyang.presentation.noti | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import androidx.core.app.NotificationCompat | ||
import com.pomonyang.mohanyang.presentation.di.PomodoroNotification | ||
import com.pomonyang.mohanyang.presentation.screen.PomodoroConstants.POMODORO_NOTIFICATION_CHANNEL_ID | ||
import com.pomonyang.mohanyang.presentation.screen.PomodoroConstants.POMODORO_NOTIFICATION_CHANNEL_NAME | ||
import com.pomonyang.mohanyang.presentation.screen.PomodoroConstants.POMODORO_NOTIFICATION_ID | ||
import javax.inject.Inject | ||
import timber.log.Timber | ||
|
||
internal class PomodoroNotificationManager @Inject constructor( | ||
@PomodoroNotification private val notificationBuilder: NotificationCompat.Builder, | ||
private val notificationManager: NotificationManager | ||
) { | ||
fun createNotification(isFocus: Boolean): Notification { | ||
Timber.tag("TIMER").d("createNotification > isFocus $isFocus") | ||
val notificationChannelId = POMODORO_NOTIFICATION_CHANNEL_ID | ||
val notificationChannel = NotificationChannel( | ||
notificationChannelId, | ||
POMODORO_NOTIFICATION_CHANNEL_NAME, | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
) | ||
notificationManager.createNotificationChannel(notificationChannel) | ||
|
||
return notificationBuilder | ||
.setContentText(if (isFocus) "집중 시간이다냥" else "휴식 시간이다냥") | ||
.build().apply { | ||
flags = Notification.FLAG_NO_CLEAR | ||
} | ||
} | ||
|
||
fun notifyFocusEnd() { | ||
notificationManager.notify( | ||
POMODORO_NOTIFICATION_ID, | ||
notificationBuilder.setContentText("집중 시간이 끝났습니다!").build() | ||
) | ||
} | ||
|
||
fun notifyFocusExceed() { | ||
notificationManager.notify( | ||
POMODORO_NOTIFICATION_ID, | ||
notificationBuilder.setContentText( | ||
"너무 오랫동안 자리를 비웠다냥" | ||
).build() | ||
) | ||
} | ||
|
||
fun notifyRestEnd() { | ||
notificationManager.notify( | ||
POMODORO_NOTIFICATION_ID, | ||
notificationBuilder.setContentText("휴식 시간이 끝났습니다!").build() | ||
) | ||
} | ||
|
||
fun notifyRestExceed() { | ||
notificationManager.notify( | ||
POMODORO_NOTIFICATION_ID, | ||
notificationBuilder.setContentText( | ||
"너무 오랫동안 자리를 비웠다냥" | ||
).build() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
presentation/src/main/java/com/pomonyang/mohanyang/presentation/service/PomodoroTimer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.pomonyang.mohanyang.presentation.service | ||
|
||
internal interface PomodoroTimer { | ||
fun startTimer(maxTime: Int, eventHandler: PomodoroTimerEventHandler) | ||
fun stopTimer() | ||
} |
6 changes: 6 additions & 0 deletions
6
...n/src/main/java/com/pomonyang/mohanyang/presentation/service/PomodoroTimerEventHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.pomonyang.mohanyang.presentation.service | ||
|
||
internal interface PomodoroTimerEventHandler { | ||
fun onTimeEnd() | ||
fun onTimeExceeded() | ||
} |
5 changes: 2 additions & 3 deletions
5
...tation/util/PomodoroTimerServiceExtras.kt → ...ion/service/PomodoroTimerServiceExtras.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
presentation/src/main/java/com/pomonyang/mohanyang/presentation/service/focus/FocusTimer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.pomonyang.mohanyang.presentation.service.focus | ||
|
||
import com.pomonyang.mohanyang.data.repository.pomodoro.PomodoroTimerRepository | ||
import com.pomonyang.mohanyang.presentation.screen.PomodoroConstants.MAX_EXCEEDED_TIME | ||
import com.pomonyang.mohanyang.presentation.screen.PomodoroConstants.ONE_SECOND | ||
import com.pomonyang.mohanyang.presentation.screen.PomodoroConstants.TIMER_DELAY | ||
import com.pomonyang.mohanyang.presentation.service.PomodoroTimer | ||
import com.pomonyang.mohanyang.presentation.service.PomodoroTimerEventHandler | ||
import java.util.Timer | ||
import javax.inject.Inject | ||
import kotlin.concurrent.fixedRateTimer | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
internal class FocusTimer @Inject constructor( | ||
private val timerRepository: PomodoroTimerRepository | ||
) : PomodoroTimer { | ||
|
||
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) | ||
private var timer: Timer? = null | ||
private var timeElapsed = 0 | ||
|
||
override fun startTimer(maxTime: Int, eventHandler: PomodoroTimerEventHandler) { | ||
Timber.tag("TIMER").d("startFocus timer / maxTime : $maxTime") | ||
if (timer == null) { | ||
timeElapsed = 0 | ||
timer = fixedRateTimer(initialDelay = TIMER_DELAY, period = TIMER_DELAY) { | ||
scope.launch { | ||
timeElapsed += ONE_SECOND | ||
timerRepository.incrementFocusedTime() | ||
|
||
Timber.tag("TIMER").d("countFocusTime: $timeElapsed ") | ||
|
||
if (timeElapsed >= maxTime) { | ||
eventHandler.onTimeEnd() | ||
} else if (timeElapsed >= maxTime + MAX_EXCEEDED_TIME) { | ||
eventHandler.onTimeExceeded() | ||
stopTimer() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun stopTimer() { | ||
timer?.cancel() | ||
timer = null | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...main/java/com/pomonyang/mohanyang/presentation/service/focus/PomodoroFocusTimerService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.pomonyang.mohanyang.presentation.service.focus | ||
|
||
import android.app.Service | ||
import android.content.Intent | ||
import android.os.IBinder | ||
import com.pomonyang.mohanyang.presentation.di.FocusTimerType | ||
import com.pomonyang.mohanyang.presentation.noti.PomodoroNotificationManager | ||
import com.pomonyang.mohanyang.presentation.screen.PomodoroConstants.POMODORO_NOTIFICATION_ID | ||
import com.pomonyang.mohanyang.presentation.service.PomodoroTimer | ||
import com.pomonyang.mohanyang.presentation.service.PomodoroTimerEventHandler | ||
import com.pomonyang.mohanyang.presentation.service.PomodoroTimerServiceExtras | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import javax.inject.Inject | ||
import timber.log.Timber | ||
|
||
@AndroidEntryPoint | ||
internal class PomodoroFocusTimerService : | ||
Service(), | ||
PomodoroTimerEventHandler { | ||
|
||
@FocusTimerType | ||
@Inject | ||
lateinit var focusTimer: PomodoroTimer | ||
|
||
@Inject | ||
lateinit var pomodoroNotificationManager: PomodoroNotificationManager | ||
|
||
override fun onBind(intent: Intent?): IBinder? = null | ||
|
||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | ||
val maxTime = intent.getIntExtra(PomodoroTimerServiceExtras.INTENT_TIMER_MAX_TIME, 0) | ||
Timber.tag("TIMER").d("onStartCommand > ${intent.action} / maxTime: $maxTime") | ||
when (intent.action) { | ||
PomodoroTimerServiceExtras.ACTION_TIMER_START -> { | ||
startForeground( | ||
POMODORO_NOTIFICATION_ID, | ||
pomodoroNotificationManager.createNotification(true) | ||
) | ||
focusTimer.startTimer(maxTime, this) | ||
} | ||
|
||
PomodoroTimerServiceExtras.ACTION_TIMER_STOP -> { | ||
stopForeground(STOP_FOREGROUND_REMOVE) | ||
focusTimer.stopTimer() | ||
} | ||
} | ||
|
||
return START_NOT_STICKY | ||
} | ||
|
||
override fun onTimeEnd() { | ||
pomodoroNotificationManager.notifyFocusEnd() | ||
} | ||
|
||
override fun onTimeExceeded() { | ||
pomodoroNotificationManager.notifyFocusExceed() | ||
} | ||
|
||
override fun stopService(name: Intent?): Boolean { | ||
stopForeground(STOP_FOREGROUND_REMOVE) | ||
focusTimer.stopTimer() | ||
return super.stopService(name) | ||
} | ||
} |
Oops, something went wrong.