From 86d4feab5b39b2c6a26a0ae93b3195463ffe8e1a Mon Sep 17 00:00:00 2001 From: WooJin Kong Date: Fri, 29 Mar 2024 16:18:26 +0900 Subject: [PATCH] feat: Make title bold when opened from notification --- .../koreatechboard/service/FCMService.kt | 1 + .../koreatechboard/ui/main/MainActivity.kt | 12 +++- .../koreatechboard/ui/main/MainEvent.kt | 1 + .../koreatechboard/ui/main/MainState.kt | 3 +- .../koreatechboard/ui/main/MainViewModel.kt | 5 ++ .../koreatechboard/ui/main/board/Board.kt | 59 +++++++++++++++---- .../koreatechboard/ui/search/Search.kt | 1 + 7 files changed, 69 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/kongjak/koreatechboard/service/FCMService.kt b/app/src/main/java/com/kongjak/koreatechboard/service/FCMService.kt index b43434a6..02f25efd 100644 --- a/app/src/main/java/com/kongjak/koreatechboard/service/FCMService.kt +++ b/app/src/main/java/com/kongjak/koreatechboard/service/FCMService.kt @@ -23,6 +23,7 @@ class FCMService : FirebaseMessagingService() { addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) putExtra("screen", message.data["screen"] ?: "board") putExtra("department", message.data["department"]) + putExtra("openedFromNotification", true) } val pendingIntent = PendingIntent.getActivity( diff --git a/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainActivity.kt b/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainActivity.kt index 2f1391db..e9711a52 100644 --- a/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainActivity.kt +++ b/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainActivity.kt @@ -23,7 +23,6 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState -import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.navigation.NavHostController @@ -58,6 +57,11 @@ class MainActivity : ComponentActivity() { mainViewModel.setDefaultDepartment(Department.valueOf(defaultDepartment)) } + val isOpenedFromNotification = intent.getBooleanExtra("openedFromNotification", false) + if (isOpenedFromNotification) { + mainViewModel.setOpenedFromNotification() + } + setContent { val isDynamicColor by themeViewModel.isDynamicTheme.observeAsState(true) val isDarkTheme by themeViewModel.isDarkTheme.observeAsState() @@ -159,12 +163,16 @@ fun NavigationGraph(navController: NavHostController, mainViewModel: MainViewMod val uiState by mainViewModel.uiState.collectAsState() val defaultScreen = uiState.defaultScreen val defaultDepartment = uiState.defaultDepartment + val isOpenedFromNotification = uiState.isOpenedFromNotification NavHost(navController = navController, startDestination = defaultScreen.name) { composable(BottomNavigationItem.Home.name) { HomeScreen() } composable(BottomNavigationItem.Board.name) { - BoardScreen(defaultDepartment = defaultDepartment) + BoardScreen( + defaultDepartment = defaultDepartment, + isOpenedFromNotification = isOpenedFromNotification + ) } composable(BottomNavigationItem.Settings.name) { SettingsScreen() diff --git a/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainEvent.kt b/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainEvent.kt index 59990761..3562df38 100644 --- a/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainEvent.kt +++ b/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainEvent.kt @@ -7,4 +7,5 @@ import com.kongjak.koreatechboard.util.routes.Department sealed class MainEvent : UiEvent { data class SetDefaultScreen(val defaultScreen: BottomNavigationItem) : MainEvent() data class SetDefaultDepartment(val defaultDepartment: Department) : MainEvent() + data object SetOpenedFromNotification : MainEvent() } diff --git a/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainState.kt b/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainState.kt index 41191b55..d3ca7e3e 100644 --- a/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainState.kt +++ b/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainState.kt @@ -6,5 +6,6 @@ import com.kongjak.koreatechboard.util.routes.Department data class MainState( val defaultScreen: BottomNavigationItem = BottomNavigationItem.Home, - val defaultDepartment: Department? = null + val defaultDepartment: Department? = null, + val isOpenedFromNotification: Boolean = false ) : UiState diff --git a/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainViewModel.kt b/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainViewModel.kt index c9422b9c..dde6c853 100644 --- a/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainViewModel.kt +++ b/app/src/main/java/com/kongjak/koreatechboard/ui/main/MainViewModel.kt @@ -9,6 +9,7 @@ class MainViewModel : BaseViewModel(MainState()) { when (event) { is MainEvent.SetDefaultScreen -> setState(oldState.copy(defaultScreen = event.defaultScreen)) is MainEvent.SetDefaultDepartment -> setState(oldState.copy(defaultDepartment = event.defaultDepartment)) + MainEvent.SetOpenedFromNotification -> setState(oldState.copy(isOpenedFromNotification = true)) } } @@ -19,4 +20,8 @@ class MainViewModel : BaseViewModel(MainState()) { fun setDefaultDepartment(defaultDepartment: Department) { sendEvent(MainEvent.SetDefaultDepartment(defaultDepartment)) } + + fun setOpenedFromNotification() { + sendEvent(MainEvent.SetOpenedFromNotification) + } } diff --git a/app/src/main/java/com/kongjak/koreatechboard/ui/main/board/Board.kt b/app/src/main/java/com/kongjak/koreatechboard/ui/main/board/Board.kt index 411b5773..6515bcd9 100644 --- a/app/src/main/java/com/kongjak/koreatechboard/ui/main/board/Board.kt +++ b/app/src/main/java/com/kongjak/koreatechboard/ui/main/board/Board.kt @@ -86,17 +86,26 @@ import kotlinx.coroutines.launch @Composable fun BoardScreen( boardInitViewModel: BoardInitViewModel = hiltViewModel(), - defaultDepartment: Department? // Default department from MainActivity. + defaultDepartment: Department?, // Default department from MainActivity. + isOpenedFromNotification: Boolean = false ) { val uiState by boardInitViewModel.uiState.collectAsState() val initDepartment = uiState.initDepartment val userDepartment = uiState.userDepartment - BottomSheetScaffold(defaultDepartment ?: fullDeptList[initDepartment], userDepartment) + BottomSheetScaffold( + defaultDepartment ?: fullDeptList[initDepartment], + userDepartment, + isOpenedFromNotification = isOpenedFromNotification + ) } @OptIn(ExperimentalMaterial3Api::class) @Composable -fun BottomSheetScaffold(initDepartment: Department = Department.School, userDepartment: Int = 0) { +fun BottomSheetScaffold( + initDepartment: Department = Department.School, + userDepartment: Int = 0, + isOpenedFromNotification: Boolean +) { val scaffoldState = rememberBottomSheetScaffoldState() val scope = rememberCoroutineScope() @@ -130,14 +139,22 @@ fun BottomSheetScaffold(initDepartment: Department = Department.School, userDepa } } ) { innerPadding -> - Board(contentPadding = innerPadding, department = department.value) + Board( + contentPadding = innerPadding, + department = department.value, + isOpenedFromNotification + ) } } @OptIn(ExperimentalFoundationApi::class) @Composable -fun Board(contentPadding: PaddingValues, department: Department) { +fun Board( + contentPadding: PaddingValues, + department: Department, + isOpenedFromNotification: Boolean +) { val pagerState = rememberPagerState( initialPage = 0 ) { @@ -176,7 +193,11 @@ fun Board(contentPadding: PaddingValues, department: Department) { state = pagerState, verticalAlignment = Alignment.Top ) { page -> - BoardContent(department = department, page = page) + BoardContent( + department = department, + page = page, + isOpenedFromNotification = isOpenedFromNotification + ) } } } @@ -186,6 +207,7 @@ fun Board(contentPadding: PaddingValues, department: Department) { fun BoardContent( department: Department, page: Int, + isOpenedFromNotification: Boolean, networkViewModel: NetworkViewModel = hiltViewModel() ) { val boardViewModel = @@ -282,7 +304,9 @@ fun BoardContent( ), title = it.title, writer = it.writer, - date = it.writeDate + isNew = it.isNew, + date = it.writeDate, + isOpenedFromNotification = isOpenedFromNotification ) HorizontalDivider(thickness = 0.5.dp, color = Gray) } @@ -316,7 +340,10 @@ fun BoardContent( modifier = Modifier.align(Alignment.TopCenter), state = pullToRefreshState, indicator = { pullRefreshState -> - PullToRefreshDefaults.Indicator(state = pullRefreshState, color = MaterialTheme.colorScheme.primary) + PullToRefreshDefaults.Indicator( + state = pullRefreshState, + color = MaterialTheme.colorScheme.primary + ) } ) } @@ -325,7 +352,16 @@ fun BoardContent( } @Composable -fun BoardItem(modifier: Modifier, title: String, writer: String, date: String) { +fun BoardItem( + modifier: Modifier, + title: String, + writer: String, + date: String, + isNew: Boolean, + isOpenedFromNotification: Boolean = false +) { + val shouldSetBold = isOpenedFromNotification && isNew + Row( modifier = modifier, verticalAlignment = Alignment.CenterVertically @@ -337,7 +373,10 @@ fun BoardItem(modifier: Modifier, title: String, writer: String, date: String) { modifier = Modifier .fillMaxWidth() .weight(1f), - style = MaterialTheme.typography.boardItemTitle + style = if (shouldSetBold) + MaterialTheme.typography.boardItemTitle.copy(fontWeight = FontWeight.Bold) + else + MaterialTheme.typography.boardItemTitle ) Column( modifier = Modifier.padding(start = 8.dp), diff --git a/app/src/main/java/com/kongjak/koreatechboard/ui/search/Search.kt b/app/src/main/java/com/kongjak/koreatechboard/ui/search/Search.kt index 29ee35e6..a02655f0 100644 --- a/app/src/main/java/com/kongjak/koreatechboard/ui/search/Search.kt +++ b/app/src/main/java/com/kongjak/koreatechboard/ui/search/Search.kt @@ -119,6 +119,7 @@ fun SearchContent( ), title = it.title, writer = it.writer, + isNew = it.isNew, date = it.writeDate ) HorizontalDivider(thickness = 0.5.dp, color = Color.Gray)