Skip to content

Commit 4e1db0c

Browse files
add view model to post list dynamic feature
1 parent cb56ebd commit 4e1db0c

20 files changed

+315
-189
lines changed

app/src/main/java/com/smarttoolfactory/postdynamichilt/ui/BottomNavigationFragmentStateAdapter.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.smarttoolfactory.postdynamichilt.ui
22

33
import androidx.fragment.app.Fragment
4+
import androidx.fragment.app.FragmentContainerView
45
import androidx.fragment.app.FragmentManager
56
import androidx.lifecycle.Lifecycle
67
import com.smarttoolfactory.core.ui.adapter.NavigableFragmentStateAdapter
78
import com.smarttoolfactory.core.ui.fragment.navhost.NavHostContainerFragment
89
import com.smarttoolfactory.postdynamichilt.R
910

1011
/**
11-
* FragmentStateAdapter to contain ViewPager2 fragments inside another fragment.
12+
* FragmentStateAdapter to contain ViewPager2 fragments inside another fragment which uses
13+
* wrapper layouts that contain [FragmentContainerView]
1214
*
1315
* * 🔥 Create FragmentStateAdapter with viewLifeCycleOwner instead of Fragment to make sure
1416
* that it lives between [Fragment.onCreateView] and [Fragment.onDestroyView] while [View] is alive
@@ -22,7 +24,6 @@ class BottomNavigationFragmentStateAdapter(fragmentManager: FragmentManager, lif
2224

2325
override fun createFragment(position: Int): Fragment {
2426
return when (position) {
25-
2627
0 -> NavHostContainerFragment.createNavHostContainerFragment(
2728
R.layout.fragment_navhost_home,
2829
R.id.nested_nav_host_fragment_home

app/src/main/res/menu/menu_bottom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
android:title="Dashboard" />
1313

1414
<item
15-
android:id="@+id/nav_graph_notification_start"
15+
android:id="@+id/nav_graph_dfm_notification_start"
1616
android:icon="@drawable/ic_baseline_notifications_24"
1717
android:title="Notification" />
1818

app/src/main/res/navigation/nav_graph_main.xml

+13
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,17 @@
2525
android:name="com.smarttoolfactory.postdynamichilt.main.MainFragmentViewPager2"
2626
android:label="MainFragment"
2727
tools:layout="@layout/fragment_main_viewpager2" />
28+
29+
<!-- Post Detail dynamic feature module -->
30+
<include-dynamic
31+
android:id="@+id/nav_graph_post_detail"
32+
android:name="com.smarttoolfactory.post_detail"
33+
app:graphResName="nav_graph_post_detail"
34+
app:moduleName="post_detail">
35+
36+
<argument
37+
android:name="post"
38+
app:argType="com.smarttoolfactory.domain.model.Post" />
39+
40+
</include-dynamic>
2841
</navigation>

features/home/src/main/java/com/smarttoolfactory/home/HomeFragment.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import com.google.android.material.tabs.TabLayoutMediator
1515
import com.smarttoolfactory.core.ui.fragment.DynamicNavigationFragment
1616
import com.smarttoolfactory.core.util.Event
1717
import com.smarttoolfactory.core.viewmodel.NavControllerViewModel
18+
import com.smarttoolfactory.home.adapter.HomeViewPager2FragmentStateAdapter
1819
import com.smarttoolfactory.home.databinding.FragmentHomeBinding
19-
import com.smarttoolfactory.home.postlist.adapter.PostPagesFragmentStateAdapter
2020

2121
/**
2222
* Fragment that contains [ViewPager2] and [TabLayout].
@@ -57,7 +57,7 @@ class HomeFragment : DynamicNavigationFragment<FragmentHomeBinding>() {
5757
https://stackoverflow.com/questions/61779776/leak-canary-detects-memory-leaks-for-tablayout-with-viewpager2
5858
*/
5959
viewPager.adapter =
60-
PostPagesFragmentStateAdapter(childFragmentManager, viewLifecycleOwner.lifecycle)
60+
HomeViewPager2FragmentStateAdapter(childFragmentManager, viewLifecycleOwner.lifecycle)
6161

6262
// Bind tabs and viewpager
6363
TabLayoutMediator(tabLayout, viewPager, tabConfigurationStrategy).attach()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.smarttoolfactory.home.adapter
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.fragment.app.FragmentManager
5+
import androidx.lifecycle.Lifecycle
6+
import com.smarttoolfactory.core.ui.adapter.NavigableFragmentStateAdapter
7+
import com.smarttoolfactory.core.ui.fragment.navhost.NavHostContainerFragment
8+
import com.smarttoolfactory.home.R
9+
10+
/**
11+
* FragmentStateAdapter to contain ViewPager2 fragments inside another fragment.
12+
*
13+
* * 🔥 Create FragmentStateAdapter with viewLifeCycleOwner instead of Fragment to make sure
14+
* that it lives between [Fragment.onCreateView] and [Fragment.onDestroyView] while [View] is alive
15+
*
16+
* * https://stackoverflow.com/questions/61779776/leak-canary-detects-memory-leaks-for-tablayout-with-viewpager2
17+
*/
18+
class HomeViewPager2FragmentStateAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
19+
NavigableFragmentStateAdapter(fragmentManager, lifecycle) {
20+
21+
override fun getItemCount(): Int = 3
22+
23+
override fun createFragment(position: Int): Fragment {
24+
25+
return when (position) {
26+
27+
// Post List with Rxjava3
28+
0 -> NavHostContainerFragment.createNavHostContainerFragment(
29+
R.layout.fragment_navhost_post_list,
30+
R.id.nested_nav_host_fragment_post_list
31+
)
32+
33+
// Post List with Flow
34+
1 -> NavHostContainerFragment.createNavHostContainerFragment(
35+
R.layout.fragment_navhost_post_list,
36+
R.id.nested_nav_host_fragment_post_list
37+
)
38+
39+
// Post List with Status
40+
else -> NavHostContainerFragment.createNavHostContainerFragment(
41+
R.layout.fragment_navhost_post_list,
42+
R.id.nested_nav_host_fragment_post_list
43+
)
44+
}
45+
}
46+
}

features/home/src/main/java/com/smarttoolfactory/home/postlist/PostListAdapter.kt features/home/src/main/java/com/smarttoolfactory/home/adapter/PostListAdapter.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smarttoolfactory.home.postlist
1+
package com.smarttoolfactory.home.adapter
22

33
import android.view.LayoutInflater
44
import android.view.ViewGroup
@@ -10,6 +10,7 @@ import androidx.recyclerview.widget.DiffUtil
1010
import androidx.recyclerview.widget.ListAdapter
1111
import androidx.recyclerview.widget.RecyclerView
1212
import com.smarttoolfactory.domain.model.Post
13+
import com.smarttoolfactory.home.BR
1314
import com.smarttoolfactory.home.R
1415
import kotlinx.android.synthetic.main.row_post.view.*
1516

@@ -91,7 +92,7 @@ class PostListAdapter(
9192
item: T
9293
) {
9394
// Bind item to layout to dispatch data to layout
94-
// binding.setVariable(BR.item, item)
95+
binding.setVariable(BR.item, item)
9596
binding.executePendingBindings()
9697
}
9798
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.smarttoolfactory.home.di
2+
3+
import androidx.fragment.app.Fragment
4+
import com.smarttoolfactory.core.di.CoreModuleDependencies
5+
import com.smarttoolfactory.home.postlist.PostListWithStatusFragment
6+
import dagger.BindsInstance
7+
import dagger.Component
8+
9+
@Component(
10+
dependencies = [CoreModuleDependencies::class],
11+
modules = [PostListModule::class]
12+
)
13+
interface PostListComponent {
14+
15+
fun inject(postListWithStatusFragment: PostListWithStatusFragment)
16+
17+
@Component.Factory
18+
interface Factory {
19+
fun create(
20+
dependentModule: CoreModuleDependencies,
21+
@BindsInstance fragment: Fragment
22+
): PostListComponent
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.smarttoolfactory.home.di
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.lifecycle.ViewModelProvider
5+
import com.smarttoolfactory.home.viewmodel.PostStatusViewModel
6+
import com.smarttoolfactory.home.viewmodel.PostStatusViewModelFactory
7+
import dagger.Module
8+
import dagger.Provides
9+
import dagger.hilt.InstallIn
10+
import dagger.hilt.android.components.FragmentComponent
11+
import kotlinx.coroutines.CoroutineScope
12+
import kotlinx.coroutines.Dispatchers
13+
import kotlinx.coroutines.SupervisorJob
14+
15+
@InstallIn(FragmentComponent::class)
16+
@Module
17+
class PostListModule {
18+
19+
@Provides
20+
fun providePostListViewModel(fragment: Fragment, factory: PostStatusViewModelFactory) =
21+
ViewModelProvider(fragment, factory).get(PostStatusViewModel::class.java)
22+
23+
@Provides
24+
fun provideCoroutineScope() = CoroutineScope(Dispatchers.Main.immediate + SupervisorJob())
25+
}

features/home/src/main/java/com/smarttoolfactory/home/postlist/PostListFlowFragment.kt

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// import com.smarttoolfactory.home.databinding.FragmentPostListBinding
1212
// import dagger.hilt.android.AndroidEntryPoint
1313
//
14-
// @AndroidEntryPoint
1514
// class PostListFlowFragment : DynamicNavigationFragment<FragmentPostListBinding>() {
1615
//
1716
// override fun getLayoutRes(): Int = R.layout.fragment_post_list

features/home/src/main/java/com/smarttoolfactory/home/postlist/PostListViewModel.kt

-75
This file was deleted.

0 commit comments

Comments
 (0)