Skip to content

Commit f4271db

Browse files
Improved navigation controller usage, renamed directions and actions
1 parent 975a80e commit f4271db

File tree

3 files changed

+59
-88
lines changed

3 files changed

+59
-88
lines changed
Lines changed: 55 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package com.developerbreach.developerbreach.controller
22

3+
import android.view.View
4+
import android.widget.ImageView
35
import android.widget.TextView
46
import androidx.navigation.NavController
7+
import androidx.navigation.NavDirections
8+
import androidx.navigation.fragment.FragmentNavigator
59
import androidx.navigation.fragment.FragmentNavigatorExtras
6-
import com.developerbreach.developerbreach.R
710
import com.developerbreach.developerbreach.view.detail.ArticleDetailFragmentDirections
8-
import com.developerbreach.developerbreach.view.favorites.FavoritesFragmentDirections
11+
import com.developerbreach.developerbreach.view.detail.ArticleDetailFragment
912
import com.developerbreach.developerbreach.view.home.HomeFragmentDirections
13+
import com.developerbreach.developerbreach.view.home.HomeFragment
14+
import com.developerbreach.developerbreach.view.list.ArticleListFragment
15+
import com.developerbreach.developerbreach.view.favorites.FavoritesFragment
1016
import com.developerbreach.developerbreach.view.intro.IntroFragmentDirections
11-
import com.developerbreach.developerbreach.view.list.ArticleListFragmentDirections
17+
import com.developerbreach.developerbreach.view.network.NetworkFragmentDirections
1218
import com.developerbreach.developerbreach.view.options.OptionsFragmentDirections
13-
import com.developerbreach.developerbreach.view.search.SearchFragmentDirections
1419
import com.developerbreach.developerbreach.view.settings.SettingsFragmentDirections
1520
import com.google.android.material.card.MaterialCardView
1621

@@ -19,106 +24,73 @@ class AppNavDirections(
1924
private val navController: NavController,
2025
) {
2126

22-
/** IntroFragment **/
23-
24-
fun introToHome() {
25-
navController.navigate(IntroFragmentDirections.introToHome())
27+
private fun navigate(directions: NavDirections) {
28+
navController.navigate(directions)
2629
}
2730

28-
/** HomeFragment **/
29-
30-
fun homeToSearch() {
31-
navController.navigate(HomeFragmentDirections.homeToSearch())
31+
private fun navigateWithExtras(
32+
directions: NavDirections,
33+
extras: FragmentNavigator.Extras
34+
) {
35+
navController.navigate(directions, extras)
3236
}
3337

34-
fun homeToDetail(articleId: Int, view: MaterialCardView) {
35-
navController.navigate(
36-
HomeFragmentDirections.homeToDetail(articleId),
37-
FragmentNavigatorExtras(view to articleId.toString())
38-
)
39-
}
38+
/** IntroFragment */
39+
fun introToHome() = navigate(IntroFragmentDirections.introToHome())
4040

41-
fun homeToOptions() {
42-
navController.navigate(HomeFragmentDirections.homeToOptions())
43-
}
44-
45-
fun homeToIntro() {
46-
navController.navigate(R.id.introFragment)
47-
}
41+
/** HomeFragment */
42+
fun homeToSearch() = navigate(HomeFragmentDirections.homeToSearch())
43+
fun homeToOptions() = navigate(OptionsFragmentDirections.toOptionsFragmentGlobal())
44+
fun homeToIntro() = navigate(IntroFragmentDirections.toIntroFragmentGlobal())
45+
fun homeToArticleList() = navigate(HomeFragmentDirections.homeToArticleList())
4846

49-
fun homeToArticleList() {
50-
navController.navigate(HomeFragmentDirections.homeToArticleList())
51-
}
47+
/** OptionsFragment */
48+
fun optionsToAuthors() = navigate(OptionsFragmentDirections.optionsToAuthors())
49+
fun optionsToFavorites() = navigate(OptionsFragmentDirections.optionsToFavorites())
50+
fun optionsToSearch() = navigate(OptionsFragmentDirections.optionsToSearch())
51+
fun optionsToSettings() = navigate(OptionsFragmentDirections.optionsToSettings())
52+
fun optionsToIntro() = navigate(IntroFragmentDirections.toIntroFragmentGlobal())
5253

53-
/** SearchFragment **/
54+
/** NetworkFragment */
55+
fun toNetworkFragment() = navigate(NetworkFragmentDirections.toNetworkFragmentGlobal())
5456

57+
/** SearchFragment */
5558
fun searchToDetail(articleId: Int, view: TextView) {
56-
navController.navigate(
57-
SearchFragmentDirections.searchToDetail(articleId),
59+
navigateWithExtras(
60+
ArticleDetailFragmentDirections.toDetailFragmentGlobal(articleId),
5861
FragmentNavigatorExtras(view to articleId.toString())
5962
)
6063
}
6164

62-
/** ArticleListFragment **/
63-
64-
fun articlesListToDetail(articleId: Int, view: MaterialCardView) {
65-
navController.navigate(
66-
ArticleListFragmentDirections.articleListToDetail(articleId),
67-
FragmentNavigatorExtras(view to articleId.toString())
68-
)
69-
}
70-
71-
/** FavoritesFragment **/
72-
73-
fun favoritesToDetail(articleId: Int, view: MaterialCardView) {
74-
navController.navigate(
75-
FavoritesFragmentDirections.favoritesToDetail(articleId),
65+
/**
66+
* [HomeFragment] [FavoritesFragment] [ArticleListFragment]
67+
* to
68+
* [ArticleDetailFragment]
69+
*/
70+
fun fragmentsToDetailFragment(
71+
articleId: Int,
72+
view: MaterialCardView
73+
) {
74+
navigateWithExtras(
75+
ArticleDetailFragmentDirections.toDetailFragmentGlobal(articleId),
7676
FragmentNavigatorExtras(view to articleId.toString())
7777
)
7878
}
7979

80-
/** DetailFragment **/
81-
80+
/** DetailFragment */
8281
fun detailToArticleWebView(articleUrlLink: String?) {
83-
navController.navigate(ArticleDetailFragmentDirections.detailToArticleWebView(articleUrlLink))
82+
navigate(ArticleDetailFragmentDirections.detailToArticleWebView(articleUrlLink))
8483
}
8584

86-
fun detailToBanner(bannerUrl: String?) {
87-
navController.navigate(ArticleDetailFragmentDirections.detailToBanner(bannerUrl))
85+
fun detailToBanner(bannerUrl: String?, view: ImageView) {
86+
navigateWithExtras(
87+
ArticleDetailFragmentDirections.detailToBanner(bannerUrl),
88+
FragmentNavigatorExtras(view as View to bannerUrl.toString())
89+
)
8890
}
8991

90-
/** SettingsFragment **/
91-
92+
/** SettingsFragment */
9293
fun settingsToCommonWebView(webUrl: String) {
93-
navController.navigate(SettingsFragmentDirections.settingsToCommonWebView(webUrl))
94-
}
95-
96-
/** OptionsFragment **/
97-
98-
fun optionsToAuthors() {
99-
navController.navigate(OptionsFragmentDirections.optionsToAuthors())
100-
}
101-
102-
fun optionsToFavorites() {
103-
navController.navigate(OptionsFragmentDirections.optionsToFavorites())
104-
}
105-
106-
fun optionsToSearch() {
107-
navController.navigate(OptionsFragmentDirections.optionsToSearch())
94+
navigate(SettingsFragmentDirections.settingsToCommonWebView(webUrl))
10895
}
109-
110-
fun optionsToSettings() {
111-
navController.navigate(OptionsFragmentDirections.optionsToSettings())
112-
}
113-
114-
fun optionsToIntro() {
115-
navController.navigate(OptionsFragmentDirections.optionsToIntro())
116-
}
117-
118-
/** NetworkFragment **/
119-
120-
fun toNetworkFragment() {
121-
navController.navigate(R.id.networkFragment)
122-
}
123-
}
124-
96+
}

app/src/main/java/com/developerbreach/developerbreach/view/favorites/FavoritesBindingAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fun MaterialCardView.setFavoriteToDetailClickListener(
6161
TransitionManager.beginDelayedTransition(this, Fade())
6262
title.visibility = View.GONE
6363

64-
AppNavDirections(findNavController()).favoritesToDetail(articleId, this)
64+
AppNavDirections(findNavController()).fragmentsToDetailFragment(articleId, this)
6565
}
6666
}
6767

app/src/main/java/com/developerbreach/developerbreach/view/home/HomeBindingAdapter.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import com.bumptech.glide.load.resource.bitmap.CenterCrop
1313
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
1414
import com.bumptech.glide.request.RequestOptions
1515
import com.developerbreach.developerbreach.R
16+
import com.developerbreach.developerbreach.controller.AppNavDirections
1617
import com.developerbreach.developerbreach.model.Article
1718
import com.developerbreach.developerbreach.utils.capitalizeWord
18-
import com.developerbreach.developerbreach.controller.AppNavDirections
1919
import com.developerbreach.developerbreach.utils.itemViewAnimation
2020
import com.developerbreach.developerbreach.view.list.ArticleAdapter
2121
import com.developerbreach.developerbreach.view.list.ArticleListFragment
@@ -29,10 +29,9 @@ fun RecyclerView.setRecentArticleFragmentListData(
2929
val adapter = RecentArticlesAdapter()
3030
adapter.submitList(list)
3131
this.adapter = adapter
32-
itemViewAnimation(context, this, 1000L, R.anim.fade_enter_anim)
32+
itemViewAnimation(context, this, 750L, R.anim.fade_enter_anim)
3333
}
3434

35-
3635
/**
3736
* BindingAdapters for fragment class [ArticleListFragment].
3837
* This values will be called as attributes in fragment layout [R.layout.item_article].
@@ -77,6 +76,6 @@ fun MaterialCardView.setRecentArticleToDetailClickListener(
7776
TransitionManager.beginDelayedTransition(this, Fade())
7877
title.visibility = View.GONE
7978

80-
AppNavDirections(findNavController()).homeToDetail(articleId, this)
79+
AppNavDirections(findNavController()).fragmentsToDetailFragment(articleId, this)
8180
}
8281
}

0 commit comments

Comments
 (0)