Skip to content
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

[refactor] 위젯 모듈 의존성 제거 #294

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.boostcamp.dreamteam.dreamdiary.manager

import android.content.Context
import android.content.Intent
import com.boostcamp.dreamteam.dreamdiary.feature.widget.DiaryWriteWidgetReceiver
import javax.inject.Singleton

@Singleton
class DiaryWidgetManger {
fun update(context: Context) {
val updateWidgetIntent = Intent(context, DiaryWriteWidgetReceiver::class.java).apply {
setAction(DiaryWriteWidgetReceiver.ACTION_APPWIDGET_MANUAL_UPDATE)
}

context.sendBroadcast(updateWidgetIntent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fun DreamDiaryNavHost(
modifier: Modifier = Modifier,
) {
val navController = appState.navController
val diaryWidgetManger = appState.diaryWidgetManger

Surface(
color = MaterialTheme.colorScheme.background,
Expand Down Expand Up @@ -81,6 +82,9 @@ fun DreamDiaryNavHost(
.build()
navController.navigate(SignInRoute, options)
},
updateDiaryWidget = { context ->
diaryWidgetManger.update(context)
},
)

communityGraph(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.boostcamp.dreamteam.dreamdiary.manager.DiaryWidgetManger
import kotlinx.coroutines.CoroutineScope

@Composable
fun rememberAppState(
coroutineScope: CoroutineScope = rememberCoroutineScope(),
navController: NavHostController = rememberNavController(),
diaryWidgetManger: DiaryWidgetManger = DiaryWidgetManger(),
) = DreamDiaryAppState(
coroutineScope = coroutineScope,
navController = navController,
diaryWidgetManger = diaryWidgetManger,
)

@Stable
class DreamDiaryAppState(
val navController: NavHostController,
coroutineScope: CoroutineScope,
val diaryWidgetManger: DiaryWidgetManger,
)
1 change: 0 additions & 1 deletion feature/diary/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ dependencies {
implementation(projects.core.ui)
implementation(projects.core.data)
implementation(projects.core.synchronization)
implementation(projects.feature.widget)

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.boostcamp.dreamteam.dreamdiary.feature.diary.detail

import android.content.Context
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -40,7 +41,6 @@ import com.boostcamp.dreamteam.dreamdiary.feature.diary.detail.model.DiaryDetail
import com.boostcamp.dreamteam.dreamdiary.feature.diary.model.DiaryContentUi
import com.boostcamp.dreamteam.dreamdiary.feature.diary.model.LabelUi
import com.boostcamp.dreamteam.dreamdiary.feature.diary.model.filteredLabelsPreview
import com.boostcamp.dreamteam.dreamdiary.feature.widget.util.updateWidget
import com.boostcamp.dreamteam.dreamdiary.ui.component.GoToSignInDialog
import timber.log.Timber
import java.io.File
Expand All @@ -52,18 +52,19 @@ fun DiaryDetailScreen(
onEditDiary: (diaryId: String) -> Unit,
onShareDiary: (diaryId: String) -> Unit,
onDialogConfirmClick: () -> Unit,
updateDiaryWidget: (Context) -> Unit,
viewModel: DiaryDetailViewModel = hiltViewModel(),
) {
val context = LocalContext.current
val email by viewModel.email.collectAsStateWithLifecycle()
var showDialog by remember { mutableStateOf(false) }

LaunchedEffect(onBackClick, context) {
LaunchedEffect(onBackClick, updateDiaryWidget) {
viewModel.event.collect { event ->
Timber.d("DiaryDetailScreen event: $event")
when (event) {
is DiaryDetailEvent.DeleteDiary.Success -> {
updateWidget(context)
updateDiaryWidget(context)
Toast.makeText(context, "삭제 성공", Toast.LENGTH_SHORT).show()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.boostcamp.dreamteam.dreamdiary.feature.diary.home

import android.content.Context
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
Expand Down Expand Up @@ -38,6 +39,7 @@ fun NavGraphBuilder.diaryGraph(
onShareDiary: (diaryId: String) -> Unit,
navController: NavController,
onDialogConfirmClick: () -> Unit,
updateDiaryWidget: (Context) -> Unit,
) {
navigation<DiaryGraph>(
startDestination = DiaryGraph.DiaryHomeRoute,
Expand Down Expand Up @@ -65,6 +67,7 @@ fun NavGraphBuilder.diaryGraph(
},
)
},
updateDiaryWidget = updateDiaryWidget,
)
}

Expand All @@ -88,6 +91,7 @@ fun NavGraphBuilder.diaryGraph(
},
)
},
updateDiaryWidget = updateDiaryWidget,
)
}

Expand All @@ -103,6 +107,7 @@ fun NavGraphBuilder.diaryGraph(
onEditDiary = { diaryId -> navController.navigateToWriteScreen(diaryId = diaryId) },
onShareDiary = onShareDiary,
onDialogConfirmClick = onDialogConfirmClick,
updateDiaryWidget = updateDiaryWidget,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.boostcamp.dreamteam.dreamdiary.feature.diary.home

import android.content.Context
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloatAsState
Expand Down Expand Up @@ -61,7 +62,6 @@ import com.boostcamp.dreamteam.dreamdiary.feature.diary.home.tablist.DiaryListTa
import com.boostcamp.dreamteam.dreamdiary.feature.diary.home.tablist.pagedDiariesPreview
import com.boostcamp.dreamteam.dreamdiary.feature.diary.model.DiaryUi
import com.boostcamp.dreamteam.dreamdiary.feature.diary.model.LabelUi
import com.boostcamp.dreamteam.dreamdiary.feature.widget.util.updateWidget
import com.boostcamp.dreamteam.dreamdiary.ui.HomeBottomNavItem
import com.boostcamp.dreamteam.dreamdiary.ui.HomeBottomNavigation
import com.boostcamp.dreamteam.dreamdiary.ui.component.GoToSignInDialog
Expand All @@ -78,6 +78,7 @@ fun DiaryHomeScreen(
onNavigateToSetting: () -> Unit,
onDialogConfirmClick: () -> Unit,
onClickSearch: () -> Unit,
updateDiaryWidget: (Context) -> Unit,
viewModel: DiaryHomeViewModel = hiltViewModel(),
onNavigateToWriteScreen: () -> Unit,
) {
Expand All @@ -104,11 +105,11 @@ fun DiaryHomeScreen(
}
}.collectAsStateWithLifecycle(SyncStateUi.IDLE)

LaunchedEffect(Unit) {
LaunchedEffect(updateDiaryWidget) {
viewModel.event.collect {
when (it) {
is DiaryHomeEvent.Delete.Success -> {
updateWidget(context)
updateDiaryWidget(context)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.boostcamp.dreamteam.dreamdiary.feature.diary.write

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
Expand Down Expand Up @@ -49,7 +50,6 @@ import com.boostcamp.dreamteam.dreamdiary.feature.diary.model.filteredLabelsPrev
import com.boostcamp.dreamteam.dreamdiary.feature.diary.model.selectedLabelsPreview
import com.boostcamp.dreamteam.dreamdiary.feature.diary.write.model.DiaryWriteEvent
import com.boostcamp.dreamteam.dreamdiary.feature.diary.write.model.LabelAddFailureReason
import com.boostcamp.dreamteam.dreamdiary.feature.widget.util.updateWidget
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
Expand All @@ -63,17 +63,18 @@ import java.util.UUID
fun DiaryWriteScreen(
onBackClick: () -> Unit,
onWriteSuccess: (diaryId: String) -> Unit,
updateDiaryWidget: (Context) -> Unit,
modifier: Modifier = Modifier,
viewModel: DiaryWriteViewModel = hiltViewModel(),
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()

val context = LocalContext.current
LaunchedEffect(onBackClick, onWriteSuccess) {
LaunchedEffect(onBackClick, onWriteSuccess, updateDiaryWidget) {
viewModel.event.collectLatest { writeEvent ->
when (writeEvent) {
is DiaryWriteEvent.DiaryAddSuccess -> {
updateWidget(context)
updateDiaryWidget(context)
onWriteSuccess(writeEvent.diaryId)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.boostcamp.dreamteam.dreamdiary.feature.widget

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE
import android.content.Context
import android.content.Intent
Expand All @@ -22,7 +23,7 @@ import java.time.temporal.ChronoUnit
import javax.inject.Inject

@AndroidEntryPoint
internal class DiaryWriteWidgetReceiver : GlanceAppWidgetReceiver() {
class DiaryWriteWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = DiaryWriteWidget()

@Inject
Expand All @@ -42,7 +43,10 @@ internal class DiaryWriteWidgetReceiver : GlanceAppWidgetReceiver() {
intent: Intent,
) {
super.onReceive(context = context, intent = intent)
if (intent.action == ACTION_APPWIDGET_UPDATE || intent.action == "android.appwidget.action.APPWIDGET_UPDATE_OPTIONS") {
if (intent.action == ACTION_APPWIDGET_MANUAL_UPDATE ||
intent.action == ACTION_APPWIDGET_UPDATE ||
intent.action == ACTION_APPWIDGET_OPTIONS_CHANGED
) {
collectData(context = context)
}
}
Expand Down Expand Up @@ -72,4 +76,9 @@ internal class DiaryWriteWidgetReceiver : GlanceAppWidgetReceiver() {
}
}
}

companion object {
const val ACTION_APPWIDGET_MANUAL_UPDATE =
"com.boostcamp.dreamteam.dreamdiary.feature.widget.ACTION_APPWIDGET_MANUAL_UPDATE"
}
}

This file was deleted.

Loading