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

feat: redirect detail when click notification #278

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -51,7 +51,10 @@ class TwotooFirebaseMessagingService : FirebaseMessagingService() {

val title = remoteMessage.data.getOrDefault("title", "")
val message = remoteMessage.data.getOrDefault("body", "")
sendNotification(title, message)
val challengeNo = remoteMessage.data.getOrDefault("challengeNo", "0")
val commitNo = remoteMessage.data.getOrDefault("commitNo", "0")

sendNotification(title, message, challengeNo.toInt(), commitNo.toInt())
}
}

Expand All @@ -60,17 +63,21 @@ class TwotooFirebaseMessagingService : FirebaseMessagingService() {
Log.d(TAG, "sendRegistrationTokenToServer($token)")
}

private fun sendNotification(title: String, messageBody: String) {
private fun sendNotification(title: String, messageBody: String, challengeNo: Int, commitNo: Int) {
// Since android Oreo notification channel is needed.
Log.i(TAG, "sendNotification: challengeNo= $challengeNo, commitNo=$commitNo")
val intent = Intent(this, MainActivity::class.java).apply {
putExtra("challengeNo", challengeNo)
putExtra("commitNo", commitNo)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val requestCode = 0
val pendingIntent = PendingIntent.getActivity(
this,
requestCode,
intent,
PendingIntent.FLAG_IMMUTABLE,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)

val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.mashup.twotoo.presenter

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.ManagedActivityResultLauncher
import androidx.activity.compose.rememberLauncherForActivityResult
Expand All @@ -21,10 +23,22 @@ import com.mashup.twotoo.presenter.twotoo.TwoTooApp

class MainActivity : ComponentActivity() {

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
val challengeNo = intent?.extras?.getInt("challengeNo", 0)
val commitNo = intent?.extras?.getInt("commitNo", 0)
Log.i(TAG, "onNewIntent: challnegeNo = $challengeNo, commitNo = $commitNo")
finish()
startActivity(intent)
}

override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false)

super.onCreate(savedInstanceState)
val challengeNo = intent.getIntExtra("challengeNo", 0)
val commitNo = intent.getIntExtra("commitNo", 0)
Log.i(TAG, "onCreate: challengeNo= $challengeNo, commitNo= $commitNo")
setContent {
TwoTooTheme {
val systemUiController = rememberSystemUiController()
Expand All @@ -43,6 +57,10 @@ class MainActivity : ComponentActivity() {
}
}
}

companion object {
const val TAG = "MainActivity"
}
}

private fun checkPermission(context: Context, launcher: ManagedActivityResultLauncher<String, Boolean>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mashup.twotoo.presenter.history

import androidx.compose.foundation.background
import android.app.Activity
import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
Expand All @@ -9,7 +11,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand All @@ -22,13 +23,15 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.mashup.twotoo.presenter.R
import com.mashup.twotoo.presenter.constant.TAG
import com.mashup.twotoo.presenter.designsystem.component.bottomsheet.BottomSheetData
import com.mashup.twotoo.presenter.designsystem.component.bottomsheet.BottomSheetType
import com.mashup.twotoo.presenter.designsystem.component.bottomsheet.TwoTooBottomSheet
Expand All @@ -39,30 +42,43 @@ import com.mashup.twotoo.presenter.designsystem.component.loading.FlowerLoadingI
import com.mashup.twotoo.presenter.designsystem.component.toolbar.TwoTooBackToolbar
import com.mashup.twotoo.presenter.designsystem.theme.TwoTooTheme
import com.mashup.twotoo.presenter.designsystem.theme.TwotooPink
import com.mashup.twotoo.presenter.home.TwoTooGoalAchievementProgressbar
import com.mashup.twotoo.presenter.home.model.HomeGoalAchievePartnerAndMeUiModel
import kotlinx.coroutines.launch
import org.orbitmvi.orbit.compose.collectAsState

@Composable
fun HistoryRoute(
from: String,
challengeNo: Int,
historyViewModel: HistoryViewModel,
homeGoalAchievePartnerAndMeUiModel: HomeGoalAchievePartnerAndMeUiModel? = null,
onClickBackButton: () -> Unit,
navigateToHistoryDetail: (Int) -> Unit,
) {
val lifecycleOwner = LocalLifecycleOwner.current

val activity = LocalContext.current as Activity
val commitNo = activity.intent.getIntExtra("commitNo", 0)
Log.d(TAG, "HistoryRoute: commitNo= $commitNo, from= $from viewModel= $historyViewModel")
val state by historyViewModel.collectAsState()
LaunchedEffect(Unit) {
lifecycleOwner.repeatOnLifecycle(state = Lifecycle.State.STARTED) {
historyViewModel.getChallengeByUser(challengeNo)
launch {
lifecycleOwner.repeatOnLifecycle(state = Lifecycle.State.STARTED) {
historyViewModel.getChallengeByUser(challengeNo).join()
launch {
Log.i(
TAG,
"HistoryScreen: navigateToDetail: navigateChallengeDetail = ${state.navigateToChallengeDetail}",
)
if (from == "notification" && state.navigateToChallengeDetail) {
if (commitNo != 0) {
navigateToHistoryDetail(commitNo)
}
historyViewModel.setNavigateToChallengeDetail(false)
}
}
}
}
}

val state by historyViewModel.collectAsState()

HistoryScreen(
homeGoalAchievePartnerAndMeUiModel = homeGoalAchievePartnerAndMeUiModel,
onClickBackButton = onClickBackButton,
navigateToHistoryDetail = navigateToHistoryDetail,
quiteChallenge = { historyViewModel.quiteChallenge(challengeNo) },
Expand All @@ -76,7 +92,6 @@ fun HistoryRoute(

@Composable
fun HistoryScreen(
homeGoalAchievePartnerAndMeUiModel: HomeGoalAchievePartnerAndMeUiModel? = null,
onClickBackButton: () -> Unit,
navigateToHistoryDetail: (Int) -> Unit,
quiteChallenge: () -> Unit,
Expand Down Expand Up @@ -145,16 +160,6 @@ fun HistoryScreen(
state.challengeInfoUiModel,
)
}
if (homeGoalAchievePartnerAndMeUiModel == null && state.homeGoalAchievePartnerAndMeUiModel != null) {
TwoTooGoalAchievementProgressbar(
modifier = Modifier
.padding(top = 12.dp, start = 24.dp)
.width(210.dp)
.height(59.dp)
.background(color = Color.White, shape = RoundedCornerShape(15.dp)),
homeGoalAchievePartnerAndMeUiModel = state.homeGoalAchievePartnerAndMeUiModel,
)
}
OwnerNickNames(state.ownerNickNamesUiModel)
Spacer(modifier = Modifier.height(12.dp))
Divider(
Expand Down Expand Up @@ -243,7 +248,6 @@ private fun PreviewHistoryScreenEmpty() {
private fun PreviewHistoryScreenWithProgressBar() {
TwoTooTheme {
HistoryScreen(
homeGoalAchievePartnerAndMeUiModel = HomeGoalAchievePartnerAndMeUiModel.default,
onClickBackButton = {},
state = HistoryState.default,
navigateToHistoryDetail = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import com.mashup.twotoo.presenter.history.datail.model.HistoryDetailInfoUiModel
import com.mashup.twotoo.presenter.history.model.ChallengeInfoUiModel
import com.mashup.twotoo.presenter.history.model.HistoryItemUiModel
import com.mashup.twotoo.presenter.history.model.OwnerNickNamesUiModel
import com.mashup.twotoo.presenter.home.mapper.toProgress
import com.mashup.twotoo.presenter.home.model.HomeGoalAchievePartnerAndMeUiModel
import com.mashup.twotoo.presenter.home.model.HomeGoalAchieveUiModel
import com.mashup.twotoo.presenter.home.model.UserType
import model.challenge.response.ChallengeDetailResponseDomainModel


data class HistoryState(
val loadingIndicatorState: Boolean = false,
val challengeInfoUiModel: ChallengeInfoUiModel = ChallengeInfoUiModel(),
val historyItemUiModel: List<HistoryItemUiModel> = emptyList(),
val ownerNickNamesUiModel: OwnerNickNamesUiModel = OwnerNickNamesUiModel(),
val historyDetailInfoUiModel: HistoryDetailInfoUiModel = HistoryDetailInfoUiModel(),
val homeGoalAchievePartnerAndMeUiModel: HomeGoalAchievePartnerAndMeUiModel? = null,
val navigateToChallengeDetail : Boolean = true,
) {
companion object {
val default = HistoryState(
Expand All @@ -25,35 +21,5 @@ data class HistoryState(
ownerNickNamesUiModel = OwnerNickNamesUiModel.default,
historyDetailInfoUiModel = HistoryDetailInfoUiModel.default,
)

fun ChallengeDetailResponseDomainModel.mapHomeGoalAchievePartnerAndMeUiModel(userNo: Int): HomeGoalAchievePartnerAndMeUiModel = with(this) {
return if (userNo == this.challengeResponseDomainModel.user1.userNo) {
HomeGoalAchievePartnerAndMeUiModel(
me = HomeGoalAchieveUiModel(
name = this.challengeResponseDomainModel.user1.nickname,
type = UserType.ME,
progress = this.myCommitResponseDomainModel.size.toProgress(),
),
partner = HomeGoalAchieveUiModel(
name = this.challengeResponseDomainModel.user2.nickname,
type = UserType.PARTNER,
progress = this.partnerCommitResponseDomainModel.size.toProgress(),
),
)
} else {
HomeGoalAchievePartnerAndMeUiModel(
me = HomeGoalAchieveUiModel(
name = this.challengeResponseDomainModel.user2.nickname,
type = UserType.ME,
progress = this.partnerCommitResponseDomainModel.size.toProgress(),
),
partner = HomeGoalAchieveUiModel(
name = this.challengeResponseDomainModel.user1.nickname,
type = UserType.PARTNER,
progress = this.myCommitResponseDomainModel.size.toProgress(),
),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.util.Log
import androidx.lifecycle.ViewModel
import com.mashup.twotoo.presenter.constant.TAG
import com.mashup.twotoo.presenter.designsystem.component.bottomsheet.BottomSheetData
import com.mashup.twotoo.presenter.history.HistoryState.Companion.mapHomeGoalAchievePartnerAndMeUiModel
import com.mashup.twotoo.presenter.history.datail.model.HistoryDetailInfoUiModel
import com.mashup.twotoo.presenter.history.model.ChallengeInfoUiModel
import com.mashup.twotoo.presenter.history.model.HistoryInfoUiModel
Expand Down Expand Up @@ -39,6 +38,13 @@ class HistoryViewModel @Inject constructor(
HistoryState(),
)

fun setNavigateToChallengeDetail(value: Boolean) = intent {
reduce {
state.copy(
navigateToChallengeDetail = false,
)
}
}
fun onClickBottomSheetDataButton(bottomSheetData: BottomSheetData) = intent {
val bottomSheetAuthenticateData = (bottomSheetData as BottomSheetData.AuthenticateData)
createCommitUseCase(
Expand Down Expand Up @@ -118,7 +124,6 @@ class HistoryViewModel @Inject constructor(
challengeInfoUiModel = newChallengeInfoUiModel,
historyItemUiModel = newHistoryItemUiModels,
ownerNickNamesUiModel = newOwnerNickNamesUiModel,
homeGoalAchievePartnerAndMeUiModel = challengeDetailResponseDomainModel.mapHomeGoalAchievePartnerAndMeUiModel(userNo),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,15 @@ import com.mashup.twotoo.presenter.history.HistoryRoute
import com.mashup.twotoo.presenter.history.datail.HistoryDetailRoute
import com.mashup.twotoo.presenter.history.detailImage.DetailImageRoute
import com.mashup.twotoo.presenter.history.di.HistoryComponentProvider
import com.mashup.twotoo.presenter.home.model.HomeGoalAchievePartnerAndMeUiModel
import com.mashup.twotoo.presenter.navigation.NavigationRoute
import com.mashup.twotoo.presenter.util.MoshiUtils
import com.mashup.twotoo.presenter.util.componentProvider
import com.mashup.twotoo.presenter.util.toIncodeUrl

fun NavController.navigateToHistory(challengeNo: Int, homeGoalAchievePartnerAndMeUiModel: HomeGoalAchievePartnerAndMeUiModel? = null) {
if (homeGoalAchievePartnerAndMeUiModel != null) {
val progress = MoshiUtils.toJson(homeGoalAchievePartnerAndMeUiModel)
this.navigate(route = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/$challengeNo?progress=$progress")
} else {
this.navigate(route = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/$challengeNo")
}
fun NavController.navigateToHistory(challengeNo: Int, from: String = "twotoo") {
this.navigate(route = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/$challengeNo/$from")
}

private fun NavController.navigateToHistoryDetail(commitNo: Int) {
fun NavController.navigateToHistoryDetail(commitNo: Int) {
this.navigate(route = "${NavigationRoute.HistoryGraph.HistoryDetailScreen.route}/$commitNo")
}

Expand All @@ -37,33 +30,30 @@ private fun NavController.navigateToDetailImageScreen(url: String) {

@SuppressLint("UnrememberedGetBackStackEntry")
fun NavGraphBuilder.historyGraph(navController: NavController) {
navigation(startDestination = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/{challengeNo}?progress={progress}", route = NavigationRoute.HistoryGraph.route) {
navigation(startDestination = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/{challengeNo}/{from}", route = NavigationRoute.HistoryGraph.route) {
composable(
route = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/{challengeNo}?progress={progress}",
route = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/{challengeNo}/{from}",
arguments = listOf(
navArgument("challengeNo") { type = NavType.IntType },
navArgument("progress") {
nullable = true
defaultValue = null
navArgument("from") {
defaultValue = "twotoo"
type = NavType.StringType
},

),
) {
navBackStackEntry ->
val challengeNo = navBackStackEntry.arguments?.getInt("challengeNo") ?: 0
val progress = navBackStackEntry.arguments?.getString("progress")
val homeGoalAchievePartnerAndMeUiModel = progress?.let {
MoshiUtils.fromJson<HomeGoalAchievePartnerAndMeUiModel>(progress)
}
val from = navBackStackEntry.arguments?.getString("from") ?: "twotoo"

val historyComponent = componentProvider<HistoryComponentProvider>().provideHistoryComponent()
val historyViewModel = daggerViewModel {
historyComponent.getViewModel()
}
HistoryRoute(
from = from,
challengeNo = challengeNo,
historyViewModel = historyViewModel,
homeGoalAchievePartnerAndMeUiModel = homeGoalAchievePartnerAndMeUiModel,
onClickBackButton = { navController.popBackStack() },
navigateToHistoryDetail = { commitNo ->
navController.navigateToHistoryDetail(commitNo)
Expand All @@ -78,7 +68,7 @@ fun NavGraphBuilder.historyGraph(navController: NavController) {
),
) {
navBackStackEntry ->
val parentRoute = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/{challengeNo}?progress={progress}"
val parentRoute = "${NavigationRoute.HistoryGraph.HistoryScreen.route}/{challengeNo}/{from}"
val parentEntry = remember {
navController.getBackStackEntry(parentRoute)
}
Expand Down
Loading