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

[mod] 마이 피드 / 상세페이지에서 삭제시 화면 처리 #185

Merged
merged 13 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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 @@ -29,6 +29,7 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main
private val isUploadSuccess by lazy { intent.extras?.getBoolean(EXTRA_UPLOAD_KEY, false) }
private val isDeleteSuccess by lazy { intent.extras?.getBoolean(EXTRA_DELETE_KEY, false) }
private val isReportSuccess by lazy { intent.extras?.getBoolean(EXTRA_REPORT_KEY, false) }
private val prevScreenName by lazy { intent.extras?.getString(KEY_PREV_SCREEN, "") }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -45,17 +46,14 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main
}

private fun initFragment() {
if (intent.getBooleanExtra("navigateMypage", false)) {
val bundle = Bundle()
bundle.putString("fromNoti", "true")
val myPageFragment = MyPageFragment()
myPageFragment.arguments = bundle
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fcv_main, myPageFragment)
transaction.commit()
binding.bnvMain.selectedItemId = R.id.menu_mypage
if (intent.getBooleanExtra(KEY_TO_MYPAGE, false)) {
navigateToMyPageWithBundle(KEY_FROM_NOTI, true)
} else {
navigateTo<WineyFeedFragment>()
if (prevScreenName == MY_FEED_SCREEN) {
navigateToMyPageWithBundle(KEY_TO_MYFEED, true)
} else {
navigateTo<WineyFeedFragment>()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

번들의 key 값도 상수화 시켜주면 더 좋을 거 같아요 😊

}
}

Expand Down Expand Up @@ -131,9 +129,27 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main
}
}

private fun navigateToMyPageWithBundle(key: String, value: Boolean) {
supportFragmentManager.commit {
val bundle = Bundle()
bundle.putBoolean(key, value)
val myPageFragment = MyPageFragment()
myPageFragment.arguments = bundle
replace(R.id.fcv_main, myPageFragment)
binding.bnvMain.selectedItemId = R.id.menu_mypage
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fragment-ktx 를 이용하면 코드를 조금 더 단순화 할 수 있을 거 같아요!

The Fragment KTX module provides a number of extensions to simplify the fragment API.
With the Fragment KTX module, you can simplify fragment transactions with lambdas, for example:

fragmentManager().commit {
   addToBackStack("...")
   setCustomAnimations(
           R.anim.enter_anim,
           R.anim.exit_anim)
   add(fragment, "...")
}
/**
 * Run [body] in a [FragmentTransaction] which is automatically committed if it completes without
 * exception.
 *
 * The transaction will be completed by calling [FragmentTransaction.commit] unless [allowStateLoss]
 * is set to `true` in which case [FragmentTransaction.commitAllowingStateLoss] will be used.
 */
public inline fun FragmentManager.commit(
    allowStateLoss: Boolean = false,
    body: FragmentTransaction.() -> Unit
) {
    val transaction = beginTransaction()
    transaction.body()
    if (allowStateLoss) {
        transaction.commitAllowingStateLoss()
    } else {
        transaction.commit()
    }
}

commit 확장 함수 안에서 body 라는 람다식을 사용하기 때문에 코드의 가독성이 좀 더 올라가는 거 같습니다! :)

}

companion object {
private const val EXTRA_UPLOAD_KEY = "upload"
private const val EXTRA_DELETE_KEY = "delete"
private const val EXTRA_REPORT_KEY = "report"

private const val KEY_PREV_SCREEN = "PREV_SCREEN_NAME"
private const val KEY_FROM_NOTI = "fromNoti"
private const val KEY_TO_MYFEED = "toMyFeed"
private const val KEY_TO_MYPAGE = "navigateMypage"

private const val MY_FEED_SCREEN = "MyFeedFragment"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ class WineyFeedFragment :
val intent = Intent(requireContext(), DetailActivity::class.java)
intent.putExtra(KEY_FEED_ID, wineyFeed.feedId)
intent.putExtra(KEY_FEED_WRITER_ID, wineyFeed.userId)
intent.putExtra(KEY_PREV_SCREEN, WINEY_FEED_SCREEN)
startActivity(intent)
}

Expand Down Expand Up @@ -442,5 +443,7 @@ class WineyFeedFragment :

private const val KEY_FEED_ID = "feedId"
private const val KEY_FEED_WRITER_ID = "feedWriterId"
private const val KEY_PREV_SCREEN = "PREV_SCREEN_NAME"
private const val WINEY_FEED_SCREEN = "WineyFeedFragment"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_

private val feedId by lazy { intent.getIntExtra(KEY_FEED_ID, 0) }
private val feedWriterId by lazy { intent.getIntExtra(KEY_FEED_WRITER_ID, 0) }
private val prevScreenName by lazy { intent.extras?.getString(KEY_PREV_SCREEN, "") }

private var _detailFeedAdapter: DetailFeedAdapter? = null
private val detailFeedAdapter get() = requireNotNull(_detailFeedAdapter)
Expand Down Expand Up @@ -416,6 +417,7 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
Intent(this, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
putExtra(extraKey, true)
putExtra(KEY_PREV_SCREEN, prevScreenName)
startActivity(this)
}
}
Expand Down Expand Up @@ -454,9 +456,12 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
companion object {
private const val KEY_FEED_ID = "feedId"
private const val KEY_FEED_WRITER_ID = "feedWriterId"
private const val KEY_PREV_SCREEN = "PREV_SCREEN_NAME"

private const val TAG_FEED_DELETE_DIALOG = "FEED_DELETE_DIALOG"
private const val TAG_COMMENT_DELETE_DIALOG = "COMMENT_DELETE_DIALOG"
private const val TAG_REPORT_DIALOG = "REPORT_DIALOG"

private const val POPUP_MENU_POS_OFFSET = 65
private const val MSG_DETAIL_ERROR = "ERROR"
private const val EXTRA_DELETE_KEY = "delete"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MyPageFragment : BindingFragment<FragmentMyPageBinding>(R.layout.fragment_
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
amplitudeUtils.logEvent("view_mypage")

initNavigation()
init1On1ButtonClickListener()
initTermsButtonClickListener()
initLevelHelpButtonClickListener()
Expand All @@ -67,8 +67,8 @@ class MyPageFragment : BindingFragment<FragmentMyPageBinding>(R.layout.fragment_
override fun handleOnBackPressed() {
val receivedBundle = arguments
if (receivedBundle != null) {
val value = receivedBundle.getString("fromNoti")
if (value == "true") {
val value = receivedBundle.getBoolean(KEY_FROM_NOTI)
if (value) {
val intent = Intent(requireContext(), NotificationActivity::class.java)
startActivity(intent)
requireActivity().finish()
Expand Down Expand Up @@ -98,6 +98,17 @@ class MyPageFragment : BindingFragment<FragmentMyPageBinding>(R.layout.fragment_
}
}

private fun initNavigation() {
val receivedBundle = arguments
if (receivedBundle != null) {
val value = receivedBundle.getBoolean(KEY_TO_MYFEED)
if (value) {
navigateAndBackStack<MyFeedFragment>()
arguments = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arguments?.clear() 라는 함수도 따로 있는 거 같더라구요! 지금 코드와 동일하게 작동할 거 같아요!

}
}
}

private fun init1On1ButtonClickListener() {
binding.clMypageTo1on1.setOnClickListener {
val url = ONE_ON_ONE_URL
Expand Down Expand Up @@ -282,5 +293,8 @@ class MyPageFragment : BindingFragment<FragmentMyPageBinding>(R.layout.fragment_
private const val EXTRA_VALUE = "MyPageFragment"
private const val TAG_LOGOUT_DIALOG = "LOGOUT_DIALOG"
private const val TAGE_WITHDRAW_DIALOG = "WITHDRAW_DIALOG"

private const val KEY_FROM_NOTI = "fromNoti"
private const val KEY_TO_MYFEED = "toMyFeed"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ class MyFeedFragment : BindingFragment<FragmentMyfeedBinding>(R.layout.fragment_
viewModel.deleteMyFeedState.flowWithLifecycle(viewLifeCycle).onEach { state ->
when (state) {
is UiState.Success -> {
wineySnackbar(binding.root, true, stringOf(R.string.snackbar_feed_delete_success))
wineySnackbar(
binding.root,
true,
stringOf(R.string.snackbar_feed_delete_success)
)
viewModel.initDeleteFeedState()
}

Expand Down Expand Up @@ -183,7 +187,8 @@ class MyFeedFragment : BindingFragment<FragmentMyfeedBinding>(R.layout.fragment_

is LoadState.NotLoading -> {
binding.rvMyfeedPost.isVisible = myFeedAdapter.itemCount > 0
binding.clMyfeedEmpty.isVisible = myFeedAdapter.itemCount == 0
binding.clMyfeedEmpty.isVisible =
myFeedAdapter.itemCount == 0
restoreScrollPosition()
}

Expand Down Expand Up @@ -232,6 +237,7 @@ class MyFeedFragment : BindingFragment<FragmentMyfeedBinding>(R.layout.fragment_
val intent = Intent(requireContext(), DetailActivity::class.java)
intent.putExtra(KEY_FEED_ID, wineyFeed.feedId)
intent.putExtra(KEY_FEED_WRITER_ID, wineyFeed.userId)
intent.putExtra(KEY_PREV_SCREEN, MY_FEED_SCREEN)
startActivity(intent)
}

Expand All @@ -246,8 +252,12 @@ class MyFeedFragment : BindingFragment<FragmentMyfeedBinding>(R.layout.fragment_
companion object {
private const val KEY_FEED_ID = "feedId"
private const val KEY_FEED_WRITER_ID = "feedWriterId"
private const val KEY_PREV_SCREEN = "PREV_SCREEN_NAME"

private const val POPUP_MENU_OFFSET = 65
private const val MSG_MYFEED_ERROR = "ERROR"
private const val TAG_FEED_DELETE_DIALOG = "DELETE_DIALOG"

private const val MY_FEED_SCREEN = "MyFeedFragment"
}
}