Skip to content

Commit

Permalink
Merge pull request #178 from Nexters/feature/SCRUM-115-navigation-args
Browse files Browse the repository at this point in the history
[SCRUM-115] Timer 쪽 기술 부채 1차 해결
  • Loading branch information
lee-ji-hoon authored Feb 18, 2025
2 parents 8f90c39 + 77f6e2a commit 6ecf2c0
Show file tree
Hide file tree
Showing 23 changed files with 442 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,29 @@ interface PomodoroTimerDao {
"""
UPDATE pomodoro_timer
SET focusedTime = focusedTime + 1
WHERE focusTimeId = (
SELECT focusTimeId
FROM pomodoro_timer
ORDER BY ROWID DESC
LIMIT 1
)
WHERE focusTimeId = :pomodoroTimerId
""",
)
suspend fun incrementFocusedTime()
suspend fun incrementFocusedTime(pomodoroTimerId: String)

@Query(
"""
UPDATE pomodoro_timer
SET restedTime = restedTime + 1
WHERE focusTimeId = (
SELECT focusTimeId
FROM pomodoro_timer
ORDER BY ROWID DESC
LIMIT 1
)
WHERE focusTimeId = :pomodoroTimerId
""",
)
suspend fun incrementRestTime()
suspend fun incrementRestTime(pomodoroTimerId: String)

@Query("UPDATE pomodoro_timer SET doneAt = :doneAt WHERE focusTimeId = :focusTimerId")
suspend fun updateDoneAt(doneAt: String, focusTimerId: String)
@Query("UPDATE pomodoro_timer SET doneAt = :doneAt WHERE focusTimeId = :pomodoroTimerId")
suspend fun updateDoneAt(doneAt: String, pomodoroTimerId: String)

@Query("UPDATE pomodoro_timer SET doneAt = :doneAt WHERE focusTimeId = (SELECT focusTimeId FROM pomodoro_timer ORDER BY ROWID DESC LIMIT 1)")
suspend fun updateDoneAtRecentPomodoro(doneAt: String)

@Query("UPDATE pomodoro_timer SET doneAt = :doneAt WHERE focusTimeId = :pomodoroTimerId")
suspend fun updateDoneAtPomodoro(doneAt: String, pomodoroTimerId: String)

@Query("DELETE FROM pomodoro_timer")
suspend fun deletePomodoroTimerAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import kotlinx.coroutines.flow.Flow

interface PomodoroTimerRepository {
suspend fun insertPomodoroTimerInitData(categoryNo: Int, pomodoroTimerId: String)
suspend fun incrementFocusedTime()
suspend fun incrementRestedTime()
suspend fun incrementFocusedTime(pomodoroTimerId: String)
suspend fun incrementRestedTime(pomodoroTimerId: String)
suspend fun updatePomodoroDone(pomodoroTimerId: String)
suspend fun updateRecentPomodoroDone()
suspend fun savePomodoroData(pomodoroTimerId: String)
suspend fun savePomodoroCacheData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ internal class PomodoroTimerRepositoryImpl @Inject constructor(
)
}

override suspend fun incrementFocusedTime() {
pomodoroTimerDao.incrementFocusedTime()
override suspend fun incrementFocusedTime(pomodoroTimerId: String) {
pomodoroTimerDao.incrementFocusedTime(pomodoroTimerId)
}

override suspend fun incrementRestedTime() {
pomodoroTimerDao.incrementRestTime()
override suspend fun incrementRestedTime(pomodoroTimerId: String) {
pomodoroTimerDao.incrementRestTime(pomodoroTimerId)
}

override suspend fun updatePomodoroDone(pomodoroTimerId: String) {
pomodoroTimerDao.updateDoneAtPomodoro(getCurrentIsoInstant(), pomodoroTimerId)
}

override suspend fun updateRecentPomodoroDone() {
Expand All @@ -38,27 +42,44 @@ internal class PomodoroTimerRepositoryImpl @Inject constructor(

override suspend fun savePomodoroData(pomodoroTimerId: String) {
pomodoroTimerDao.updateDoneAt(getCurrentIsoInstant(), pomodoroTimerId)
val pomodoro = pomodoroTimerDao.getTimer(pomodoroTimerId).firstOrNull()
Timber.tag(TAG).d("savePomodoroData > $pomodoro")
pomodoro?.let {
mohaNyangService.saveFocusTime(
listOf(it.toRequestModel()),
).onSuccess {
pomodoroTimerDao.deletePomodoroTimer(pomodoroTimerId)

pomodoroTimerDao.getTimer(pomodoroTimerId)
.firstOrNull()
?.also { Timber.tag(TAG).d("savePomodoroData > $it") }
?.let { pomodoro ->
when {
pomodoro.focusedTime >= 60 -> mohaNyangService.saveFocusTime(
listOf(pomodoro.toRequestModel()),
).onSuccess {
pomodoroTimerDao.deletePomodoroTimer(pomodoroTimerId)
}
else -> pomodoroTimerDao.deletePomodoroTimer(pomodoroTimerId)
}
}
}
}

override suspend fun savePomodoroCacheData() {
val pomodoroList = pomodoroTimerDao.getAllPomodoroTimers()
.filter { it.focusedTime >= 60 }
.map { pomodoro -> pomodoro.toRequestModel() }
Timber.tag(TAG).d("savePomodoroCacheData > $pomodoroList")
mohaNyangService.saveFocusTime(pomodoroList).onSuccess {
pomodoroTimerDao.deletePomodoroTimerAll()
}.onFailure {
Timber.tag(TAG).d("savePomodoroCacheData onFailure> $it")
}
pomodoroTimerDao.getAllPomodoroTimers()
.partition { it.focusedTime >= 60 }
.let { (validPomodoros, invalidPomodoros) ->
invalidPomodoros
.map { it.focusTimeId }
.forEach {
Timber.tag(TAG).d("deletePomodoroTimer min 60s > $it")
pomodoroTimerDao.deletePomodoroTimer(it)
}

validPomodoros
.map { it.toRequestModel() }
.let { pomodoroList ->
Timber.tag(TAG).d("savePomodoroCacheData > $pomodoroList")
mohaNyangService.saveFocusTime(pomodoroList)
.onSuccess { pomodoroTimerDao.deletePomodoroTimerAll() }
.onFailure {
Timber.tag(TAG).d("savePomodoroCacheData onFailure> $it")
}
}
}
}

override fun getPomodoroTimer(timerId: String): Flow<PomodoroTimerEntity?> = pomodoroTimerDao.getTimer(timerId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.pomonyang.mohanyang.presentation.screen.home.setting

import android.annotation.SuppressLint
import android.content.Context
import androidx.annotation.DrawableRes
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -62,8 +61,6 @@ import com.pomonyang.mohanyang.presentation.theme.MnTheme
import com.pomonyang.mohanyang.presentation.util.MnNotificationManager
import com.pomonyang.mohanyang.presentation.util.clickableSingle
import com.pomonyang.mohanyang.presentation.util.collectWithLifecycle
import com.pomonyang.mohanyang.presentation.util.stopFocusTimer
import com.pomonyang.mohanyang.presentation.util.stopRestTimer
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -93,7 +90,7 @@ fun PomodoroSettingRoute(

LaunchedEffect(Unit) {
pomodoroSettingViewModel.handleEvent(PomodoroSettingEvent.Init)
stopAllNotification(context)
MnNotificationManager.stopInterrupt(context)
}

if (state.showCategoryBottomSheet) {
Expand All @@ -112,12 +109,6 @@ fun PomodoroSettingRoute(
)
}

private fun stopAllNotification(context: Context) {
context.stopFocusTimer()
context.stopRestTimer()
MnNotificationManager.stopInterrupt(context)
}

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun PomodoroSettingScreen(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
package com.pomonyang.mohanyang.presentation.screen.pomodoro

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.composable
import androidx.navigation.navigation
import androidx.navigation.toRoute
import com.pomonyang.mohanyang.presentation.screen.pomodoro.focus.PomodoroFocusRoute
import com.pomonyang.mohanyang.presentation.screen.pomodoro.rest.PomodoroRestRoute
import com.pomonyang.mohanyang.presentation.screen.pomodoro.waiting.PomodoroRestWaitingRoute
import com.pomonyang.mohanyang.presentation.screen.pomodoro.waiting.PomodoroBreakReadyRoute
import java.util.*
import kotlinx.serialization.Serializable

@Serializable
data object Pomodoro

@Serializable
internal data object PomodoroFocusTimer
data object PomodoroFocusTimer

@Serializable
internal data class PomodoroRestWaiting(
data class PomodoroRestWaiting(
val pomodoroId: String,
val type: String,
val focusTime: Int,
val exceededTime: Int,
)

@Serializable
internal data object PomodoroRest
data class PomodoroRest(
val pomodoroId: String,
)

fun NavGraphBuilder.pomodoroScreen(
onForceGoHome: () -> Unit,
Expand All @@ -42,16 +39,18 @@ fun NavGraphBuilder.pomodoroScreen(
) {
composable<PomodoroFocusTimer> {
PomodoroFocusRoute(
pomodoroTimerViewModel = it.sharedViewModel(navHostController),
goToRest = { type, focusTime, exceededTime ->
goToRest = { type, focusTime, exceededTime, pomodoroId ->
navHostController.navigate(
PomodoroRestWaiting(
type = type,
focusTime = focusTime,
exceededTime = exceededTime,
pomodoroId = pomodoroId,
),
) {
popUpTo(PomodoroFocusTimer) { inclusive = true }
popUpTo<PomodoroFocusTimer> {
inclusive = true
}
}
},
goToHome = {
Expand All @@ -60,17 +59,13 @@ fun NavGraphBuilder.pomodoroScreen(
)
}

composable<PomodoroRestWaiting> { backStackEntry ->
val routeData = backStackEntry.toRoute<PomodoroRestWaiting>()
PomodoroRestWaitingRoute(
type = routeData.type,
focusTime = routeData.focusTime,
exceedTime = routeData.exceededTime,
composable<PomodoroRestWaiting> {
PomodoroBreakReadyRoute(
goToHome = {
navHostController.popBackStack()
},
goToPomodoroRest = {
navHostController.navigate(PomodoroRest) {
navHostController.navigate(PomodoroRest(it)) {
popUpTo<PomodoroRestWaiting> {
inclusive = true
}
Expand All @@ -86,26 +81,20 @@ fun NavGraphBuilder.pomodoroScreen(

composable<PomodoroRest> {
PomodoroRestRoute(
pomodoroTimerViewModel = it.sharedViewModel(navHostController),
onShowSnackbar = onShowSnackbar,
goToHome = {
navHostController.popBackStack()
},
goToFocus = {
navHostController.navigate(Pomodoro) {
popUpTo(PomodoroRest) { inclusive = true }
navHostController.navigate(
PomodoroFocusTimer,
) {
popUpTo<PomodoroRest> {
inclusive = true
}
}
},
)
}
}
}

@Composable
inline fun <reified T : ViewModel> NavBackStackEntry.sharedViewModel(navHostController: NavHostController): T {
val navGraphRoute = destination.parent?.route ?: return viewModel()
val parentEntry = remember(this) {
navHostController.getBackStackEntry(navGraphRoute)
}
return hiltViewModel(parentEntry)
}

This file was deleted.

Loading

0 comments on commit 6ecf2c0

Please sign in to comment.