Skip to content

Commit

Permalink
feat: #702 알림 화면이 부모 화면 위에 쌓이는 기능 추가 (#703)
Browse files Browse the repository at this point in the history
* feat: 알림 화면이 부모 화면 위에 쌓이는 기능 추가

* feat: 알림 TaskStack 생성 기능 추가
  • Loading branch information
ippnsj authored Oct 17, 2023
1 parent 4df27b7 commit 765d3ea
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.ddangddangddang.android.feature.mypage.MyPageFragment
import com.ddangddangddang.android.feature.search.SearchFragment
import com.ddangddangddang.android.global.screenViewLogEvent
import com.ddangddangddang.android.util.binding.BindingActivity
import com.ddangddangddang.android.util.compat.getSerializableExtraCompat
import com.ddangddangddang.android.util.view.BackKeyHandler
import com.ddangddangddang.android.util.view.showDialog
import com.ddangddangddang.android.util.view.showSnackbar
Expand Down Expand Up @@ -62,6 +63,7 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main
binding.viewModel = viewModel

setupViewModel()
setupFragment()
onBackPressedDispatcher.addCallback(this, callback)
}

Expand Down Expand Up @@ -166,9 +168,22 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main
startActivity(intent)
}

private fun setupFragment() {
if (viewModel.currentFragmentType.value == null) {
val type =
intent.getSerializableExtraCompat(KEY_MAIN_FRAGMENT_TYPE) ?: MainFragmentType.HOME
binding.bnvNavigation.selectedItemId = type.id
viewModel.setupFragmentType(type)
}
}

companion object {
fun getIntent(context: Context): Intent {
return Intent(context, MainActivity::class.java)
private const val KEY_MAIN_FRAGMENT_TYPE = "main_fragment_type"

fun getIntent(context: Context, type: MainFragmentType = MainFragmentType.HOME): Intent {
val intent = Intent(context, MainActivity::class.java)
intent.putExtra(KEY_MAIN_FRAGMENT_TYPE, type)
return intent
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package com.ddangddangddang.android.feature.main

import androidx.databinding.BindingAdapter
import com.ddangddangddang.android.R
import com.google.android.material.bottomnavigation.BottomNavigationView

@BindingAdapter("onNavigationItemSelected")
fun BottomNavigationView.bindOnNavigationItemSelectedListener(
onFragmentChange: (MainFragmentType) -> Unit,
) {
this.setOnItemSelectedListener { menuItem ->
val fragmentType = when (menuItem.itemId) {
R.id.menu_item_home -> MainFragmentType.HOME
R.id.menu_item_search -> MainFragmentType.SEARCH
R.id.menu_item_message -> MainFragmentType.MESSAGE
R.id.menu_item_my_page -> MainFragmentType.MY_PAGE
else -> throw IllegalArgumentException("Not found menu item")
}
val fragmentType = MainFragmentType.of(menuItem.itemId)
onFragmentChange(fragmentType)
true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package com.ddangddangddang.android.feature.main

enum class MainFragmentType(val tag: String) {
HOME("fragment_home_tag"),
SEARCH("fragment_search_tag"),
MESSAGE("fragment_message_tag"),
MY_PAGE("fragment_my_page_tag"),
import androidx.annotation.IdRes
import com.ddangddangddang.android.R

enum class MainFragmentType(@IdRes val id: Int, val tag: String) {
HOME(R.id.menu_item_home, "fragment_home_tag"),
SEARCH(R.id.menu_item_search, "fragment_search_tag"),
MESSAGE(R.id.menu_item_message, "fragment_message_tag"),
MY_PAGE(R.id.menu_item_my_page, "fragment_my_page_tag"), ;

companion object {
fun of(@IdRes menuId: Int): MainFragmentType {
return values().firstOrNull { it.id == menuId }
?: throw IllegalArgumentException("No match type for menu item")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor() : ViewModel() {
private val _currentFragmentType: MutableLiveData<MainFragmentType> =
MutableLiveData(MainFragmentType.HOME)
private val _currentFragmentType: MutableLiveData<MainFragmentType> = MutableLiveData()
val currentFragmentType: LiveData<MainFragmentType>
get() = _currentFragmentType
private val _event: SingleLiveEvent<MainEvent> = SingleLiveEvent()
Expand All @@ -25,6 +24,10 @@ class MainViewModel @Inject constructor() : ViewModel() {
_event.value = MainEvent.NotificationPermissionCheck
}

fun setupFragmentType(type: MainFragmentType) {
_currentFragmentType.value = type
}

private fun changeCurrentFragmentType(fragmentType: MainFragmentType) {
if (currentFragmentType.value == fragmentType) {
if (fragmentType == MainFragmentType.HOME) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.ddangddangddang.android.R
import com.ddangddangddang.android.feature.main.MainActivity
import com.ddangddangddang.android.feature.main.MainFragmentType
import com.ddangddangddang.android.feature.messageRoom.MessageRoomActivity
import com.ddangddangddang.android.global.DdangDdangDdang
import com.ddangddangddang.android.global.getBitmapFromUrl
Expand Down Expand Up @@ -66,7 +68,8 @@ object MessageType : NotificationType() {
}

private fun getMessageRoomPendingIntent(context: Context, id: Long): PendingIntent? {
val parentIntent = MainActivity.getIntent(context, MainFragmentType.MESSAGE)
val intent = MessageRoomActivity.getIntent(context.applicationContext, id)
return intent.getPendingIntent(context, id.toInt())
return intent.getPendingIntent(context, id.toInt(), parentIntent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.app.Notification
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.TaskStackBuilder
import com.ddangddangddang.android.feature.detail.AuctionDetailActivity
import com.ddangddangddang.android.feature.main.MainActivity
import com.google.firebase.messaging.RemoteMessage

internal fun String.toNotificationType(): NotificationType {
Expand All @@ -25,13 +27,24 @@ abstract class NotificationType {
remoteMessage: RemoteMessage,
): Notification

fun Intent.getPendingIntent(context: Context, requestCode: Int): PendingIntent? {
return PendingIntent.getActivity(
context.applicationContext,
requestCode,
this,
PendingIntent.FLAG_IMMUTABLE,
)
fun Intent.getPendingIntent(
context: Context,
requestCode: Int,
parentIntent: Intent? = null,
): PendingIntent? {
return TaskStackBuilder.create(context).run {
if (parentIntent != null) {
addNextIntentWithParentStack(parentIntent)
addNextIntent(this@getPendingIntent)
} else {
addNextIntentWithParentStack(this@getPendingIntent)
}

getPendingIntent(
requestCode,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
}
}
}

Expand All @@ -43,7 +56,8 @@ abstract class AuctionDetailType : NotificationType() {
): Notification

fun getAuctionDetailPendingIntent(context: Context, id: Long): PendingIntent? {
val parentIntent = MainActivity.getIntent(context)
val intent = AuctionDetailActivity.getIntent(context.applicationContext, id)
return intent.getPendingIntent(context, id.toInt())
return intent.getPendingIntent(context, id.toInt(), parentIntent)
}
}

0 comments on commit 765d3ea

Please sign in to comment.