-
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.
refactor: BasePomodoroTimer 추가 및 Timer에서 추가적인 비즈니스로직 제거
- Loading branch information
1 parent
6a4f169
commit 2777d28
Showing
10 changed files
with
131 additions
and
139 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
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
50 changes: 50 additions & 0 deletions
50
presentation/src/main/java/com/pomonyang/mohanyang/presentation/service/BasePomodoroTimer.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,50 @@ | ||
package com.pomonyang.mohanyang.presentation.service | ||
|
||
import com.pomonyang.mohanyang.presentation.model.setting.PomodoroCategoryType | ||
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 java.util.* | ||
import kotlin.concurrent.fixedRateTimer | ||
import timber.log.Timber | ||
|
||
internal abstract class BasePomodoroTimer : PomodoroTimer { | ||
private var timer: Timer? = null | ||
private var timeElapsed = 0 | ||
|
||
protected abstract fun getTagName(): String | ||
|
||
override fun startTimer( | ||
maxTime: Int, | ||
eventHandler: PomodoroTimerEventHandler, | ||
category: PomodoroCategoryType? | ||
) { | ||
Timber.tag(getTagName()).d("startTimer / maxTime : $maxTime") | ||
if (timer == null) { | ||
timeElapsed = 0 | ||
timer = fixedRateTimer(initialDelay = TIMER_DELAY, period = TIMER_DELAY) { | ||
timeElapsed += ONE_SECOND | ||
|
||
Timber.tag(getTagName()).d("countTime: $timeElapsed ") | ||
|
||
eventHandler.updateTimer( | ||
time = if (timeElapsed >= maxTime) maxTime.toString() else timeElapsed.toString(), | ||
overtime = if (maxTime >= timeElapsed) "0" else (timeElapsed - maxTime).toString(), | ||
category = category | ||
) | ||
|
||
if (timeElapsed == maxTime) { | ||
eventHandler.onTimeEnd() | ||
} else if (timeElapsed >= maxTime + MAX_EXCEEDED_TIME) { | ||
eventHandler.onTimeExceeded() | ||
stopTimer() | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun stopTimer() { | ||
timer?.cancel() | ||
timer = null | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package com.pomonyang.mohanyang.presentation.service | ||
|
||
import com.pomonyang.mohanyang.presentation.model.setting.PomodoroCategoryType | ||
import com.pomonyang.mohanyang.presentation.noti.PomodoroNotificationManager | ||
|
||
internal interface PomodoroTimer { | ||
|
||
fun startTimer( | ||
maxTime: Int, | ||
eventHandler: PomodoroTimerEventHandler, | ||
pomodoroNotificationManager: PomodoroNotificationManager, | ||
category: PomodoroCategoryType? = null | ||
) | ||
|
||
fun stopTimer() | ||
} |
3 changes: 3 additions & 0 deletions
3
...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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package com.pomonyang.mohanyang.presentation.service | ||
|
||
import com.pomonyang.mohanyang.presentation.model.setting.PomodoroCategoryType | ||
|
||
internal interface PomodoroTimerEventHandler { | ||
fun onTimeEnd() | ||
fun onTimeExceeded() | ||
fun updateTimer(time: String, overtime: String, category: PomodoroCategoryType?) | ||
} |
62 changes: 3 additions & 59 deletions
62
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 |
---|---|---|
@@ -1,65 +1,9 @@ | ||
package com.pomonyang.mohanyang.presentation.service.focus | ||
|
||
import com.pomonyang.mohanyang.data.repository.pomodoro.PomodoroTimerRepository | ||
import com.pomonyang.mohanyang.presentation.model.setting.PomodoroCategoryType | ||
import com.pomonyang.mohanyang.presentation.noti.PomodoroNotificationManager | ||
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.* | ||
import com.pomonyang.mohanyang.presentation.service.BasePomodoroTimer | ||
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 { | ||
internal class FocusTimer @Inject constructor() : BasePomodoroTimer() { | ||
|
||
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) | ||
private var timer: Timer? = null | ||
private var timeElapsed = 0 | ||
|
||
override fun startTimer( | ||
maxTime: Int, | ||
eventHandler: PomodoroTimerEventHandler, | ||
pomodoroNotificationManager: PomodoroNotificationManager, | ||
category: PomodoroCategoryType? | ||
) { | ||
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 ") | ||
|
||
pomodoroNotificationManager.updateNotification( | ||
time = if (timeElapsed >= maxTime) maxTime.toString() else timeElapsed.toString(), | ||
overtime = if (maxTime >= timeElapsed) "0" else (timeElapsed - maxTime).toString(), | ||
category = category | ||
) | ||
|
||
if (timeElapsed >= maxTime) { | ||
eventHandler.onTimeEnd() | ||
} else if (timeElapsed >= maxTime + MAX_EXCEEDED_TIME) { | ||
eventHandler.onTimeExceeded() | ||
stopTimer() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun stopTimer() { | ||
timer?.cancel() | ||
timer = null | ||
} | ||
override fun getTagName(): String = "TIMER_FOCUS" | ||
} |
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
60 changes: 3 additions & 57 deletions
60
presentation/src/main/java/com/pomonyang/mohanyang/presentation/service/rest/RestTimer.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 |
---|---|---|
@@ -1,63 +1,9 @@ | ||
package com.pomonyang.mohanyang.presentation.service.rest | ||
|
||
import com.pomonyang.mohanyang.data.repository.pomodoro.PomodoroTimerRepository | ||
import com.pomonyang.mohanyang.presentation.model.setting.PomodoroCategoryType | ||
import com.pomonyang.mohanyang.presentation.noti.PomodoroNotificationManager | ||
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.* | ||
import com.pomonyang.mohanyang.presentation.service.BasePomodoroTimer | ||
import javax.inject.Inject | ||
import kotlin.concurrent.fixedRateTimer | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
internal class RestTimer @Inject constructor( | ||
private val timerRepository: PomodoroTimerRepository | ||
) : PomodoroTimer { | ||
internal class RestTimer @Inject constructor() : BasePomodoroTimer() { | ||
|
||
private var timer: Timer? = null | ||
private var timeElapsed = 0 | ||
private val scope = CoroutineScope(Dispatchers.IO) | ||
|
||
override fun startTimer( | ||
maxTime: Int, | ||
eventHandler: PomodoroTimerEventHandler, | ||
pomodoroNotificationManager: PomodoroNotificationManager, | ||
category: PomodoroCategoryType? | ||
) { | ||
Timber.tag("TIMER").d("startRest timer / maxTime : $maxTime") | ||
if (timer == null) { | ||
timeElapsed = 0 | ||
timer = fixedRateTimer(initialDelay = TIMER_DELAY, period = TIMER_DELAY) { | ||
scope.launch { | ||
timeElapsed += ONE_SECOND | ||
timerRepository.incrementRestedTime() | ||
|
||
Timber.tag("TIMER").d("countRestTime: $timeElapsed ") | ||
pomodoroNotificationManager.updateNotification( | ||
time = if (timeElapsed >= maxTime) maxTime.toString() else timeElapsed.toString(), | ||
overtime = if (maxTime >= timeElapsed) "0" else (timeElapsed - maxTime).toString(), | ||
category = null | ||
) | ||
|
||
if (timeElapsed >= maxTime) { | ||
eventHandler.onTimeEnd() | ||
} else if (timeElapsed >= maxTime + MAX_EXCEEDED_TIME) { | ||
eventHandler.onTimeExceeded() | ||
stopTimer() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun stopTimer() { | ||
timer?.cancel() | ||
timer = null | ||
} | ||
override fun getTagName(): String = "TIMER_REST" | ||
} |
Oops, something went wrong.