1
1
package org.wordpress.android.ui.main
2
2
3
- import android.annotation.SuppressLint
4
3
import android.content.Context
5
4
import android.graphics.ColorMatrix
6
5
import android.graphics.ColorMatrixColorFilter
@@ -10,6 +9,7 @@ import android.view.HapticFeedbackConstants
10
9
import android.view.LayoutInflater
11
10
import android.view.MenuItem
12
11
import android.view.View
12
+ import android.view.ViewGroup
13
13
import android.view.animation.AnimationUtils
14
14
import android.widget.FrameLayout
15
15
import android.widget.ImageView
@@ -22,8 +22,6 @@ import androidx.core.view.setPadding
22
22
import androidx.core.widget.ImageViewCompat
23
23
import androidx.fragment.app.Fragment
24
24
import androidx.fragment.app.FragmentManager
25
- import com.google.android.material.navigation.NavigationBarItemView
26
- import com.google.android.material.navigation.NavigationBarMenuView
27
25
import com.google.android.material.navigation.NavigationBarView
28
26
import com.google.android.material.navigation.NavigationBarView.OnItemReselectedListener
29
27
import com.google.android.material.navigation.NavigationBarView.OnItemSelectedListener
@@ -61,7 +59,6 @@ import com.google.android.material.R as MaterialR
61
59
* insert our own custom views so we have more control over their appearance
62
60
*/
63
61
@AndroidEntryPoint
64
- @SuppressLint(" RestrictedApi" ) // https://github.com/wordpress-mobile/WordPress-Android/issues/21079
65
62
class WPMainNavigationView @JvmOverloads constructor(
66
63
context : Context ,
67
64
attrs : AttributeSet ? = null ,
@@ -79,8 +76,8 @@ class WPMainNavigationView @JvmOverloads constructor(
79
76
)
80
77
private lateinit var navigationBarView: NavigationBarView
81
78
82
- val disabledColorFilter = ColorMatrixColorFilter (ColorMatrix ().apply { setSaturation(0f ) })
83
- val enabledColorFilter = ColorMatrixColorFilter (ColorMatrix ().apply { setSaturation(1f ) })
79
+ private val disabledColorFilter = ColorMatrixColorFilter (ColorMatrix ().apply { setSaturation(0f ) })
80
+ private val enabledColorFilter = ColorMatrixColorFilter (ColorMatrix ().apply { setSaturation(1f ) })
84
81
85
82
@Inject
86
83
lateinit var meGravatarLoader: MeGravatarLoader
@@ -119,30 +116,33 @@ class WPMainNavigationView @JvmOverloads constructor(
119
116
assignNavigationListeners(true )
120
117
disableShiftMode()
121
118
122
- // overlay each item with our custom view
123
- val menuView = navigationBarView.getChildAt(0 ) as NavigationBarMenuView
119
+ // This is a restricted NavigationBarMenuView
120
+ val menuView = navigationBarView.getChildAt(0 ) as ViewGroup
124
121
if (! BuildConfig .ENABLE_READER ) hideReaderTab()
125
122
123
+ // overlay each item with our custom view
126
124
for (i in 0 until navigationBarView.menu.size()) {
127
- val itemView = menuView.getChildAt(i) as NavigationBarItemView
128
- val customView: View = inflater.inflate(R .layout.navbar_item, menuView, false )
129
-
130
- val txtLabel = customView.findViewById<TextView >(R .id.nav_label)
131
- val imgIcon = customView.findViewById<ImageView >(R .id.nav_icon)
132
- txtLabel.text = getTitleForPosition(i)
133
- customView.contentDescription = getContentDescriptionForPosition(i)
134
- imgIcon.setImageResource(getDrawableResForPosition(i))
135
- if (i == getPosition(READER )) {
136
- customView.id = R .id.bottom_nav_reader_button // identify view for QuickStart
137
- }
138
- if (i == getPosition(NOTIFS )) {
139
- customView.id = R .id.bottom_nav_notifications_button // identify view for QuickStart
140
- }
125
+ // This is a restricted NavigationBarItemView
126
+ (menuView.getChildAt(i) as ? ViewGroup )?.let { itemView ->
127
+ val customView: View = inflater.inflate(R .layout.navbar_item, menuView, false )
128
+
129
+ val txtLabel = customView.findViewById<TextView >(R .id.nav_label)
130
+ val imgIcon = customView.findViewById<ImageView >(R .id.nav_icon)
131
+ txtLabel.text = getTitleForPosition(i)
132
+ customView.contentDescription = getContentDescriptionForPosition(i)
133
+ imgIcon.setImageResource(getDrawableResForPosition(i))
134
+ if (i == getPosition(READER )) {
135
+ customView.id = R .id.bottom_nav_reader_button // identify view for QuickStart
136
+ }
137
+ if (i == getPosition(NOTIFS )) {
138
+ customView.id = R .id.bottom_nav_notifications_button // identify view for QuickStart
139
+ }
141
140
142
- if (i == getPosition(ME )) {
143
- loadGravatar(imgIcon, accountStore.account?.avatarUrl.orEmpty())
141
+ if (i == getPosition(ME )) {
142
+ loadGravatar(imgIcon, accountStore.account?.avatarUrl.orEmpty())
143
+ }
144
+ itemView.addView(customView)
144
145
}
145
- itemView.addView(customView)
146
146
}
147
147
148
148
if (getMainPageIndex() != getPosition(ME )) {
@@ -380,18 +380,16 @@ class WPMainNavigationView @JvmOverloads constructor(
380
380
381
381
fun getFragment (pageType : PageType ) = navAdapter.getFragmentIfExists(getPosition(pageType))
382
382
383
- private fun getItemView (position : Int ): NavigationBarItemView ? {
383
+ private fun getItemView (position : Int ): View ? {
384
384
if (isValidPosition(position)) {
385
- val menuView = navigationBarView.getChildAt(0 ) as NavigationBarMenuView
386
- return menuView.getChildAt(position) as NavigationBarItemView
385
+ // This is a restricted NavigationBarMenuView
386
+ val menuView = navigationBarView.getChildAt(0 ) as ? ViewGroup
387
+ // This is a restricted NavigationBarItemView
388
+ return menuView?.getChildAt(position)
387
389
}
388
390
return null
389
391
}
390
392
391
- fun showReaderBadge (showBadge : Boolean ) {
392
- showBadge(getPosition(READER ), showBadge)
393
- }
394
-
395
393
fun showNoteBadge (showBadge : Boolean ) {
396
394
showBadge(getPosition(NOTIFS ), showBadge)
397
395
}
@@ -441,7 +439,7 @@ class WPMainNavigationView @JvmOverloads constructor(
441
439
}
442
440
443
441
444
- internal fun getFragment (position : Int ): Fragment ? {
442
+ fun getFragment (position : Int ): Fragment ? {
445
443
return pages().getOrNull(position)?.let { pageType ->
446
444
val currentFragment = fragmentManager?.findFragmentByTag(getTagForPageType(pageType))
447
445
return currentFragment?.let {
@@ -472,7 +470,7 @@ class WPMainNavigationView @JvmOverloads constructor(
472
470
}
473
471
}
474
472
475
- internal fun getFragmentIfExists (position : Int ): Fragment ? {
473
+ fun getFragmentIfExists (position : Int ): Fragment ? {
476
474
return pages().getOrNull(position)?.let { pageType ->
477
475
fragmentManager?.findFragmentByTag(getTagForPageType(pageType))
478
476
}
0 commit comments