-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Timer Service Refactoring #140
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d81090f
refactor: Timer 분리
lee-ji-hoon 80ee918
refactor: Focus / Rest Service 분리
lee-ji-hoon 11f9988
refactor: 패키지 분리
lee-ji-hoon 14effa0
chore: Timer 관련 로그 추가
lee-ji-hoon 9513a16
chore: Service Manifest 정의
lee-ji-hoon a46386e
chore: meta-data > property 수정
lee-ji-hoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어제 밤에 이야기 했던 NotificationBuilder 를 hilt를 아직 유지한 이유는 app과 관련된 정보를 presenter에서 알 수가 없다 보니 우선 유지하고 해당 정보들을 어떻게 할지 좀 고민해볼게 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 알림 추가 기능 확인하고 변경해도 괜찮을듯! |
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Android 14 대응이구나
가이드 보니까 meta-data 말고 property 태그를 사용하던데 차이가 있을까?
https://developer.android.com/about/versions/14/changes/fgs-types-required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
meta-data
가 익숙해서 사용한거였는데, 찾아 보니property
는 Android 14부터 도입된 개념으로 나오네 이 방법으로 해동 상관 없을거 같아 변경해두고 다시 멘션할게!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HyomK chore: meta-data > property 수정 반영완료~!