Skip to content

Commit

Permalink
feat(presentation): 네비게이션 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
re4rk committed Feb 25, 2024
1 parent 3c508c6 commit 924436c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 121 deletions.
1 change: 1 addition & 0 deletions android/festago/presentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies {

// Feature module Support
implementation("androidx.navigation:navigation-dynamic-features-fragment:2.7.7")
implementation("androidx.navigation:navigation-ui-ktx:2.7.7")

// Testing Navigation
androidTestImplementation("androidx.navigation:navigation-testing:2.7.7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.fragment.app.commit
import androidx.fragment.app.viewModels
import com.festago.festago.presentation.R
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.festago.festago.presentation.databinding.FragmentArtistDetailBinding
import com.festago.festago.presentation.databinding.ItemMediaBinding
import com.festago.festago.presentation.ui.artistdetail.adapter.festival.ArtistDetailAdapter
Expand All @@ -25,13 +24,12 @@ class ArtistDetailFragment : Fragment() {

private val vm: ArtistDetailViewModel by viewModels()

private val args: ArtistDetailFragmentArgs by navArgs()

private val adapter = ArtistDetailAdapter { artistId ->
// TODO: Navigation으로 변경
requireActivity().supportFragmentManager.commit {
add(R.id.fcvHomeContainer, newInstance(artistId))
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_MATCH_ACTIVITY_OPEN)
addToBackStack(null)
}
findNavController().navigate(
ArtistDetailFragmentDirections.actionArtistDetailFragmentSelf(artistId),
)
}

override fun onCreateView(
Expand All @@ -45,8 +43,7 @@ class ArtistDetailFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val id = requireArguments().getLong(KEY_ID)
initView(id)
initView(args.artistId)
initObserve()
}

Expand Down Expand Up @@ -105,16 +102,4 @@ class ArtistDetailFragment : Fragment() {
_binding = null
super.onDestroyView()
}

companion object {
private const val KEY_ID = "ID"

fun newInstance(
id: Long,
) = ArtistDetailFragment().apply {
arguments = Bundle().apply {
putLong(KEY_ID, id)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupWithNavController
import com.festago.festago.presentation.R
import com.festago.festago.presentation.databinding.ActivityHomeBinding
import com.festago.festago.presentation.ui.artistdetail.ArtistDetailFragment
import com.festago.festago.presentation.ui.home.bookmarklist.BookmarkListFragment
import com.festago.festago.presentation.ui.home.festivallist.FestivalListFragment
import com.festago.festago.presentation.ui.home.mypage.MyPageFragment
import com.festago.festago.presentation.ui.home.ticketlist.TicketListFragment
import com.festago.festago.presentation.util.setOnApplyWindowInsetsCompatListener
import com.festago.festago.presentation.util.setStatusBarMode
import com.google.android.material.bottomnavigation.BottomNavigationView
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -28,10 +29,16 @@ class HomeActivity : AppCompatActivity() {

private val vm: HomeViewModel by viewModels()

private lateinit var navController: NavController

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initBinding()
initView()
navController =
(supportFragmentManager.findFragmentById(R.id.fcvHomeContainer) as NavHostFragment).navController
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.nvHome)
bottomNavigationView.setupWithNavController(navController)

initBackPressedDispatcher()
initBackStackListener()
WindowCompat.setDecorFitsSystemWindows(window, false)
Expand All @@ -53,14 +60,6 @@ class HomeActivity : AppCompatActivity() {
setContentView(binding.root)
}

private fun initView() {
binding.nvHome.setOnItemSelectedListener {
selectView(it.itemId)
true
}
changeFragment<FestivalListFragment>()
}

private fun initBackStackListener() {
supportFragmentManager.addOnBackStackChangedListener {
val fragment = supportFragmentManager.findFragmentById(R.id.fcvHomeContainer)
Expand All @@ -72,52 +71,6 @@ class HomeActivity : AppCompatActivity() {
}
}

private fun selectView(menuItemId: Int) {
return when (menuItemId) {
R.id.itemFestival -> showFestivalList()
R.id.itemTicket -> showTicketList()
R.id.itemBookmark -> showBookmarkList()
R.id.itemMyPage -> showMyPage()
else -> throw IllegalArgumentException("menu item id not found")
}
}

private fun showFestivalList() {
changeFragment<FestivalListFragment>()
}

private fun showTicketList() {
changeFragment<TicketListFragment>()
}

private fun showBookmarkList() {
changeFragment<BookmarkListFragment>()
}

private fun showMyPage() {
changeFragment<MyPageFragment>()
}

private inline fun <reified T : Fragment> changeFragment() {
val tag = T::class.java.name
val fragmentTransaction = supportFragmentManager.beginTransaction()

supportFragmentManager.fragments.forEach { fragment ->
fragmentTransaction.hide(fragment)
}

var targetFragment = supportFragmentManager.findFragmentByTag(tag)

if (targetFragment == null) {
targetFragment = supportFragmentManager.fragmentFactory.instantiate(classLoader, tag)
fragmentTransaction.add(R.id.fcvHomeContainer, targetFragment, tag)
} else {
fragmentTransaction.show(targetFragment)
}

fragmentTransaction.commit()
}

private fun initBackPressedDispatcher() {
var backPressedTime = START_BACK_PRESSED_TIME
onBackPressedDispatcher.addCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,21 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.fragment.app.commit
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.festago.festago.presentation.R
import com.festago.festago.presentation.databinding.FragmentFestivalListBinding
import com.festago.festago.presentation.ui.artistdetail.ArtistDetailFragment
import com.festago.festago.presentation.ui.home.festivallist.FestivalListFragmentDirections.actionFestivalListFragmentToSchoolDetailFragment
import com.festago.festago.presentation.ui.home.festivallist.festival.FestivalListAdapter
import com.festago.festago.presentation.ui.home.festivallist.uistate.FestivalFilterUiState
import com.festago.festago.presentation.ui.home.festivallist.uistate.FestivalListUiState
import com.festago.festago.presentation.ui.home.festivallist.uistate.FestivalTabUiState
import com.festago.festago.presentation.ui.schooldetail.SchoolDetailFragment
import com.festago.festago.presentation.util.repeatOnStarted
import com.festago.festago.presentation.util.setOnApplyWindowInsetsCompatListener
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class FestivalListFragment : Fragment() {

private var _binding: FragmentFestivalListBinding? = null
private val binding get() = _binding!!

Expand Down Expand Up @@ -77,20 +73,19 @@ class FestivalListFragment : Fragment() {
vm.loadFestivals()
binding.srlFestivalList.isRefreshing = false
}
binding.ivSearch.setOnClickListener { // 임시 연결
binding.ivSearch.setOnClickListener {
showSchoolDetail()
}
}

private fun initViewPager() {
festivalListAdapter = FestivalListAdapter(
// TODO: Navigation으로 변경
onArtistClick = { artistId ->
requireActivity().supportFragmentManager.commit {
add(R.id.fcvHomeContainer, ArtistDetailFragment.newInstance(artistId))
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_MATCH_ACTIVITY_OPEN)
addToBackStack(null)
}
findNavController().navigate(
FestivalListFragmentDirections.actionFestivalListFragmentToArtistDetailFragment(
artistId,
),
)
},
)
binding.rvFestivalList.adapter = festivalListAdapter
Expand Down Expand Up @@ -146,10 +141,7 @@ class FestivalListFragment : Fragment() {
}

private fun showSchoolDetail() {
activity?.supportFragmentManager!!.beginTransaction()
.replace(R.id.fcvHomeContainer, SchoolDetailFragment.newInstance(0))
.addToBackStack(null)
.commit()
findNavController().navigate(actionFestivalListFragmentToSchoolDetailFragment(0))
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,35 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import com.festago.festago.presentation.databinding.FragmentSchoolDetailBinding
import com.festago.festago.presentation.ui.schooldetail.uistate.SchoolDetailUiState
import com.festago.festago.presentation.util.repeatOnStarted
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SchoolDetailFragment : Fragment() {

private var _binding: FragmentSchoolDetailBinding? = null
private val binding get() = _binding!!

private val vm: SchoolDetailViewModel by viewModels()

private lateinit var adapter: SchoolFestivalListAdapter

private val args: SchoolDetailFragmentArgs by navArgs()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View {
_binding = FragmentSchoolDetailBinding.inflate(inflater)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val schoolId = requireArguments().getLong(SCHOOL_ID_KEY)
initView(schoolId)
initView(args.schoolId)
initObserve()
}

Expand Down Expand Up @@ -76,7 +77,7 @@ class SchoolDetailFragment : Fragment() {
binding.llcSchoolSocialMedia.removeAllViews()
uiState.schoolInfo.socialMedia.forEach { sm ->
binding.llcSchoolSocialMedia.addView(
SocialMediaView(requireActivity(), null, sm.logoUrl, sm.url)
SocialMediaView(requireActivity(), null, sm.logoUrl, sm.url),
)
}
}
Expand All @@ -85,14 +86,4 @@ class SchoolDetailFragment : Fragment() {
_binding = null
super.onDestroyView()
}

companion object {
private const val SCHOOL_ID_KEY = "SCHOOL_ID_KEY"

fun newInstance(id: Long) = SchoolDetailFragment().apply {
arguments = Bundle().apply {
putLong(SCHOOL_ID_KEY, id)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
tools:layout="@layout/fragment_festival_list">
<action
android:id="@+id/action_festivalListFragment_to_schoolDetailFragment"
app:destination="@id/schoolDetailFragment" />
app:destination="@id/schoolDetailFragment"
app:enterAnim="@android:anim/slide_in_left"
app:popExitAnim="@android:anim/slide_out_right" />
<action
android:id="@+id/action_festivalListFragment_to_artistDetailFragment"
app:destination="@id/artistDetailFragment" />
app:destination="@id/artistDetailFragment"
app:enterAnim="@android:anim/slide_in_left"
app:popExitAnim="@android:anim/slide_out_right" />
</fragment>
<fragment
android:id="@+id/artistDetailFragment"
Expand All @@ -27,19 +31,27 @@
app:argType="long" />
<action
android:id="@+id/action_artistDetailFragment_to_schoolDetailFragment"
app:destination="@id/schoolDetailFragment" />
app:destination="@id/schoolDetailFragment"
app:enterAnim="@android:anim/slide_in_left"
app:popExitAnim="@android:anim/slide_out_right" />
<action
android:id="@+id/action_artistDetailFragment_self"
app:destination="@id/artistDetailFragment" />
app:destination="@id/artistDetailFragment"
app:enterAnim="@android:anim/slide_in_left"
app:popExitAnim="@android:anim/slide_out_right"
app:popUpToSaveState="true"
app:restoreState="true" />
</fragment>
<fragment
android:id="@+id/schoolDetailFragment"
android:name="com.festago.festago.presentation.ui.schooldetail.SchoolDetailFragment"
android:label="SchoolDetailFragment"
tools:layout="@layout/fragment_school_detail" >
tools:layout="@layout/fragment_school_detail">
<action
android:id="@+id/action_schoolDetailFragment_to_artistDetailFragment"
app:destination="@id/artistDetailFragment" />
app:destination="@id/artistDetailFragment"
app:enterAnim="@android:anim/slide_in_left"
app:popExitAnim="@android:anim/slide_out_right" />
<argument
android:name="schoolId"
app:argType="long" />
Expand Down

0 comments on commit 924436c

Please sign in to comment.