Skip to content

Commit 91daa7d

Browse files
committed
build: resolve merge conflict
2 parents 3aea9c4 + 1776fe3 commit 91daa7d

6 files changed

Lines changed: 139 additions & 90 deletions

File tree

Lines changed: 106 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.into.websoso.ui.main
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.content.Intent
56
import android.os.Bundle
6-
import android.view.MenuItem
77
import android.view.View
88
import androidx.activity.OnBackPressedCallback
99
import androidx.activity.viewModels
10-
import androidx.annotation.DrawableRes
10+
import androidx.annotation.IntegerRes
1111
import androidx.fragment.app.Fragment
12-
import androidx.fragment.app.commit
13-
import androidx.fragment.app.replace
1412
import com.into.websoso.R.id.fcv_main
1513
import com.into.websoso.R.id.menu_explore
1614
import com.into.websoso.R.id.menu_feed
@@ -40,12 +38,22 @@ import dagger.hilt.android.AndroidEntryPoint
4038
class MainActivity : BaseActivity<ActivityMainBinding>(activity_main) {
4139
private val mainViewModel: MainViewModel by viewModels()
4240
private var backPressedTime: Long = 0L
41+
private var currentFragment: Fragment? = null
42+
private var currentSelectedItemId: Int = menu_home
43+
44+
private val fragmentTags = mapOf(
45+
menu_home to HomeFragment::class.java.name,
46+
menu_explore to ExploreFragment::class.java.name,
47+
menu_feed to FeedFragment::class.java.name,
48+
menu_library to LibraryFragment::class.java.name,
49+
menu_my_page to MyPageFragment::class.java.name,
50+
)
4351

4452
override fun onCreate(savedInstanceState: Bundle?) {
4553
super.onCreate(savedInstanceState)
4654

4755
setupBackButtonListener()
48-
setBottomNavigationView()
56+
setupBottomNavigationView()
4957
setupObserver()
5058
onViewGuestClick()
5159
handleNavigation(intent.getSerializableExtra(DESTINATION_KEY) as? FragmentType)
@@ -75,13 +83,74 @@ class MainActivity : BaseActivity<ActivityMainBinding>(activity_main) {
7583
}
7684
}
7785

78-
private fun setBottomNavigationView() {
86+
@SuppressLint("CommitTransaction")
87+
private fun setupBottomNavigationView() {
88+
setupInitialFragment()
89+
setupBottomNavListener()
7990
binding.bnvMain.selectedItemId = menu_home
80-
replaceFragment<HomeFragment>()
91+
}
92+
93+
private fun setupInitialFragment() {
94+
val initialItemId = menu_home
95+
val initialTag = fragmentTags[initialItemId]!!
96+
val initialFragment = findOrCreateFragment(initialTag)
97+
98+
if (!initialFragment.isAdded) {
99+
supportFragmentManager
100+
.beginTransaction()
101+
.add(fcv_main, initialFragment, initialTag)
102+
.commit()
103+
}
104+
105+
currentFragment = initialFragment
106+
}
107+
108+
private fun setupBottomNavListener() {
109+
binding.bnvMain.setOnItemSelectedListener { item ->
110+
if (item.itemId == currentSelectedItemId && item.itemId == menu_library) {
111+
val libraryFragment = supportFragmentManager.findFragmentByTag(
112+
LibraryFragment::class.java.name,
113+
) as? LibraryFragment
114+
115+
libraryFragment?.resetScrollPosition()
116+
} else {
117+
replaceCurrentFragment(item.itemId)
118+
currentSelectedItemId = item.itemId
119+
}
81120

82-
binding.bnvMain.setOnItemSelectedListener(::replaceFragment)
121+
true
122+
}
83123
}
84124

125+
private fun replaceCurrentFragment(itemId: Int) {
126+
val tag = fragmentTags[itemId]!!
127+
val targetFragment = findOrCreateFragment(tag)
128+
129+
val transaction = supportFragmentManager.beginTransaction()
130+
131+
currentFragment?.let { transaction.hide(it) }
132+
133+
if (!targetFragment.isAdded) {
134+
transaction.add(fcv_main, targetFragment, tag)
135+
} else {
136+
transaction.show(targetFragment)
137+
}
138+
139+
transaction.commit()
140+
currentFragment = targetFragment
141+
}
142+
143+
private fun findOrCreateFragment(tag: String): Fragment =
144+
supportFragmentManager.findFragmentByTag(tag)
145+
?: when (tag) {
146+
HomeFragment::class.java.name -> HomeFragment()
147+
ExploreFragment::class.java.name -> ExploreFragment()
148+
FeedFragment::class.java.name -> FeedFragment()
149+
LibraryFragment::class.java.name -> LibraryFragment()
150+
MyPageFragment::class.java.name -> MyPageFragment()
151+
else -> throw IllegalArgumentException("Unknown fragment tag: $tag")
152+
}
153+
85154
private fun setupObserver() {
86155
mainViewModel.isLogin.observe(this) { isLogin ->
87156
when (isLogin) {
@@ -101,87 +170,22 @@ class MainActivity : BaseActivity<ActivityMainBinding>(activity_main) {
101170
}
102171
}
103172

104-
private fun replaceFragment(item: MenuItem): Boolean {
105-
when (FragmentType.valueOf(item.itemId)) {
106-
HOME -> replaceFragment<HomeFragment>()
107-
EXPLORE -> replaceFragment<ExploreFragment>()
108-
FEED -> replaceFragment<FeedFragment>()
109-
LIBRARY -> replaceFragment<LibraryFragment>()
110-
MY_PAGE -> replaceFragment<MyPageFragment>()
111-
}
112-
return true
113-
}
114-
115-
private inline fun <reified T : Fragment> replaceFragment() {
116-
supportFragmentManager.commit {
117-
replace<T>(fcv_main)
118-
setReorderingAllowed(true)
119-
}
120-
}
121-
122-
enum class FragmentType(
123-
@DrawableRes private val resId: Int,
124-
) {
125-
LIBRARY(menu_library),
126-
HOME(menu_home),
127-
EXPLORE(menu_explore),
128-
FEED(menu_feed),
129-
MY_PAGE(menu_my_page),
130-
;
131-
132-
companion object {
133-
fun valueOf(id: Int): FragmentType =
134-
entries.find { fragmentType -> fragmentType.resId == id }
135-
?: throw IllegalArgumentException()
136-
137-
fun valueOf(fragmentName: String): FragmentType =
138-
entries.find { fragmentType -> fragmentType.name == fragmentName }
139-
?: throw IllegalArgumentException()
140-
}
141-
}
142-
143173
private fun showLoginRequestDialog() {
144174
val dialog = LoginRequestDialogFragment.newInstance()
145175
dialog.show(supportFragmentManager, LoginRequestDialogFragment.TAG)
146176
}
147177

148178
private fun handleNavigation(destination: FragmentType?) {
149-
when (destination) {
150-
EXPLORE -> selectFragment(EXPLORE)
151-
MY_PAGE -> selectFragment(MY_PAGE)
152-
FEED -> selectFragment(FEED)
153-
LIBRARY -> selectFragment(LIBRARY)
154-
HOME, null -> selectFragment(HOME)
179+
val menuId = when (destination) {
180+
EXPLORE -> menu_explore
181+
MY_PAGE -> menu_my_page
182+
FEED -> menu_feed
183+
LIBRARY -> menu_library
184+
HOME, null -> menu_home
155185
}
156-
}
157-
158-
private fun selectFragment(fragmentType: FragmentType) {
159-
when (fragmentType) {
160-
HOME -> {
161-
binding.bnvMain.selectedItemId = menu_home
162-
replaceFragment<HomeFragment>()
163-
}
164-
165-
EXPLORE -> {
166-
binding.bnvMain.selectedItemId = menu_explore
167-
replaceFragment<ExploreFragment>()
168-
}
169-
170-
FEED -> {
171-
binding.bnvMain.selectedItemId = menu_feed
172-
replaceFragment<FeedFragment>()
173-
}
174186

175-
LIBRARY -> {
176-
binding.bnvMain.selectedItemId = menu_library
177-
replaceFragment<LibraryFragment>()
178-
}
179-
180-
MY_PAGE -> {
181-
binding.bnvMain.selectedItemId = menu_my_page
182-
replaceFragment<MyPageFragment>()
183-
}
184-
}
187+
binding.bnvMain.selectedItemId = menuId
188+
replaceCurrentFragment(menuId)
185189
}
186190

187191
companion object {
@@ -210,4 +214,25 @@ class MainActivity : BaseActivity<ActivityMainBinding>(activity_main) {
210214
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
211215
}
212216
}
217+
218+
enum class FragmentType(
219+
@IntegerRes val resId: Int,
220+
) {
221+
LIBRARY(menu_library),
222+
HOME(menu_home),
223+
EXPLORE(menu_explore),
224+
FEED(menu_feed),
225+
MY_PAGE(menu_my_page),
226+
;
227+
228+
companion object {
229+
fun valueOf(id: Int): FragmentType =
230+
entries.find { it.resId == id }
231+
?: throw IllegalArgumentException()
232+
233+
fun valueOf(fragmentName: String): FragmentType =
234+
entries.find { it.name == fragmentName }
235+
?: throw IllegalArgumentException()
236+
}
237+
}
213238
}

app/src/main/java/com/into/websoso/ui/main/library/LibraryFragment.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import android.view.ViewGroup
77
import androidx.compose.ui.platform.ComposeView
88
import androidx.compose.ui.platform.ViewCompositionStrategy
99
import androidx.fragment.app.Fragment
10+
import androidx.fragment.app.viewModels
1011
import com.into.websoso.R
1112
import com.into.websoso.core.common.navigator.NavigatorProvider
1213
import com.into.websoso.core.designsystem.theme.WebsosoTheme
1314
import com.into.websoso.feature.library.LibraryScreen
15+
import com.into.websoso.feature.library.LibraryViewModel
1416
import dagger.hilt.android.AndroidEntryPoint
1517
import javax.inject.Inject
1618

@@ -43,4 +45,9 @@ class LibraryFragment : Fragment() {
4345
}
4446
return view
4547
}
48+
49+
fun resetScrollPosition() {
50+
val viewModel: LibraryViewModel by viewModels()
51+
viewModel.resetScrollPosition()
52+
}
4653
}

feature/library/src/main/java/com/into/websoso/feature/library/LibraryScreen.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import androidx.compose.foundation.layout.fillMaxSize
66
import androidx.compose.foundation.layout.height
77
import androidx.compose.foundation.lazy.LazyListState
88
import androidx.compose.foundation.lazy.grid.LazyGridState
9-
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
10-
import androidx.compose.foundation.lazy.rememberLazyListState
119
import androidx.compose.material3.ExperimentalMaterial3Api
1210
import androidx.compose.material3.SheetState
1311
import androidx.compose.material3.rememberModalBottomSheetState
@@ -54,8 +52,8 @@ fun LibraryScreen(
5452
.map { it.map(NovelEntity::toUiModel) }
5553
.collectAsLazyPagingItems()
5654
var isShowBottomSheet by remember { mutableStateOf(false) }
57-
val listState = rememberLazyListState()
58-
val gridState = rememberLazyGridState()
55+
val listState = libraryViewModel.listState
56+
val gridState = libraryViewModel.gridState
5957
val bottomSheetState = rememberModalBottomSheetState(
6058
skipPartiallyExpanded = true,
6159
confirmValueChange = { false },

feature/library/src/main/java/com/into/websoso/feature/library/LibraryViewModel.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.into.websoso.feature.library
22

3+
import androidx.compose.foundation.lazy.LazyListState
4+
import androidx.compose.foundation.lazy.grid.LazyGridState
5+
import androidx.compose.runtime.getValue
6+
import androidx.compose.runtime.mutableStateOf
7+
import androidx.compose.runtime.setValue
38
import androidx.lifecycle.ViewModel
49
import androidx.lifecycle.viewModelScope
510
import androidx.paging.cachedIn
@@ -45,6 +50,12 @@ class LibraryViewModel
4550
)
4651
}.cachedIn(viewModelScope)
4752

53+
var listState by mutableStateOf(LazyListState())
54+
private set
55+
56+
var gridState by mutableStateOf(LazyGridState())
57+
private set
58+
4859
init {
4960
updateMyLibraryFilter()
5061
}
@@ -99,4 +110,14 @@ class LibraryViewModel
99110
)
100111
}
101112
}
113+
114+
fun resetScrollPosition() {
115+
viewModelScope.launch {
116+
if (uiState.value.isGrid) {
117+
gridState.scrollToItem(0)
118+
} else {
119+
listState.scrollToItem(0)
120+
}
121+
}
122+
}
102123
}

feature/library/src/main/java/com/into/websoso/feature/library/component/LibraryListItem.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ private fun ReadStatusBadge(
166166
.background(
167167
color = readStatus.backgroundColor,
168168
shape = RoundedCornerShape(8.dp),
169-
)
170-
.padding(vertical = 4.dp),
169+
).padding(vertical = 4.dp),
171170
contentAlignment = Alignment.Center,
172171
) {
173172
Text(

feature/library/src/main/java/com/into/websoso/feature/library/mapper/AttractivePointsMapper.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package com.into.websoso.feature.library.mapper
33
import com.into.websoso.domain.library.model.AttractivePoints
44
import com.into.websoso.feature.library.model.AttractivePointUiModel
55

6-
internal fun AttractivePoints.toUiModel(): AttractivePointUiModel {
7-
return AttractivePointUiModel(
6+
internal fun AttractivePoints.toUiModel(): AttractivePointUiModel =
7+
AttractivePointUiModel(
88
type = this,
99
label = this.label,
1010
key = this.key,
1111
)
12-
}

0 commit comments

Comments
 (0)