MainSubscriptionFragment.kt 5.66 KB
Newer Older
1 2 3
package fr.chenry.android.freshrss.components.subscriptions

import android.os.Bundle
4
import android.util.TypedValue
5
import android.view.*
6 7
import android.widget.LinearLayout
import androidx.core.view.updateLayoutParams
8
import androidx.fragment.app.Fragment
9
import androidx.fragment.app.FragmentPagerAdapter
10
import androidx.lifecycle.Observer
11
import androidx.viewpager.widget.ViewPager.SimpleOnPageChangeListener
12
import com.google.android.material.bottomnavigation.BottomNavigationItemView
13
import com.google.android.material.bottomnavigation.BottomNavigationView
14
import fr.chenry.android.freshrss.*
15
import fr.chenry.android.freshrss.store.Store
16
import fr.chenry.android.freshrss.store.database.models.VoidCategory
17
import fr.chenry.android.freshrss.utils.whenNotNull
18
import kotlinx.android.synthetic.main.fragment_main_subscription.*
19
import kotlinx.android.synthetic.main.fragment_subscriptions.*
20
import kotlinx.android.synthetic.main.menu_badge.*
21
import nl.komponents.kovenant.ui.alwaysUi
22

23 24
class MainSubscriptionFragment: Fragment(), BottomNavigationView.OnNavigationItemSelectedListener {
    private val adapter by lazy {MainSubscriptionPagerAdapter()}
Christophe Henry's avatar
Christophe Henry committed
25

26 27
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
        inflater.inflate(R.layout.fragment_main_subscription, container, false)
28 29 30 31

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

32 33
        setupBadgeCount()

34 35
        subcription_fragment_container.adapter = adapter
        subcription_fragment_container.offscreenPageLimit = SubscriptionSection.values().size
36

37 38 39
        Store.subscriptionsSection.value!!.let {
            subcription_bottom_navigation.selectedItemId = it.navigationButtonId
            subcription_fragment_container.currentItem = it.ordinal
40
        }
41

42 43 44
        subcription_fragment_container.addOnPageChangeListener(object: SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) = this@MainSubscriptionFragment.onPageSelected(position)
        })
45
        subcription_bottom_navigation.setOnNavigationItemSelectedListener(this)
46
    }
Christophe Henry's avatar
Christophe Henry committed
47

48 49 50
    override fun onNavigationItemSelected(it: MenuItem): Boolean {
        SubscriptionSection.fromNavigationButton(it.itemId).let {
            Store.subscriptionsSection.value = it
51
            setupBadgeStyle(it)
52 53
            subcription_fragment_container.currentItem = it.ordinal
        }
54

55 56
        return true
    }
57

58 59 60 61
    fun onPageSelected(position: Int) = SubscriptionSection.byPosition(position).let {
        Store.subscriptionsSection.value = it
        subcription_bottom_navigation.selectedItemId = it.navigationButtonId
    }
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    private fun setupBadgeCount() {
        val menuItems = subcription_bottom_navigation.getChildAt(0)
        menuItems.findViewById<BottomNavigationItemView>(R.id.subscriptions_bottom_navigation_unread).apply {
            addView(layoutInflater.inflate(R.layout.menu_badge, menuItems as ViewGroup, false))
        }

        val observer = Observer<Int?> {count ->
            menu_counter_badge_count.visibility = if((count ?: 0) > 0) View.VISIBLE else View.INVISIBLE
            menu_counter_badge_count.text = (count ?: 0).let {if(it > 99) "99+" else it.toString()}
        }

        setupBadgeStyle(Store.subscriptionsSection.value!!)
        observer.onChanged(Store.totalUnreadCount.value)
        Store.totalUnreadCount.observe(this, observer)
    }

    private fun setupBadgeStyle(activeSubscriptionSection: SubscriptionSection) {
80 81 82
        val offset =
            (resources.getDimensionPixelSize(R.dimen.badge_active_text_size) -
                resources.getDimensionPixelSize(R.dimen.badge_inactive_text_size))
83

84
        if(activeSubscriptionSection == SubscriptionSection.UNREAD) {
85
            menu_counter_badge_count.updateLayoutParams<LinearLayout.LayoutParams> {
86 87
                setMargins(0, 0, 0, resources.getDimensionPixelSize(R.dimen.badge_margin_bottom))
                marginStart = resources.getDimensionPixelSize(R.dimen.badge_margin_start) + (offset / 2)
88 89 90 91
            }

            menu_counter_badge_count
                .setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.badge_active_text_size))
92 93
            menu_counter_badge_count.background =
                resources.getDrawable(R.drawable.red_circle_background, activity!!.theme)
94 95
        } else {
            menu_counter_badge_count.updateLayoutParams<LinearLayout.LayoutParams> {
96
                setMargins(0, 0, 0, resources.getDimensionPixelSize(R.dimen.badge_margin_bottom) - (offset * 2))
97 98 99 100 101
                marginStart = resources.getDimensionPixelSize(R.dimen.badge_margin_start)
            }

            menu_counter_badge_count
                .setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.badge_inactive_text_size))
102 103
            menu_counter_badge_count.background =
                resources.getDrawable(R.drawable.grey_circle_background, activity!!.theme)
104 105 106
        }
    }

107 108
    inner class MainSubscriptionPagerAdapter: FragmentPagerAdapter(childFragmentManager) {
        override fun getItem(position: Int) = instantiateFragment(SubscriptionSection.byPosition(position))
109
        override fun getCount() = SubscriptionSection.values().size
110

111 112 113 114
        private fun instantiateFragment(subscriptionSection: SubscriptionSection): Fragment {
            val arguments = MainNavDirections
                .actionGlobalSubscriptionsFragment(VoidCategory, subscriptionSection)
                .arguments
Christophe Henry's avatar
Christophe Henry committed
115

116 117
            return childFragmentManager
                .fragmentFactory
Christophe Henry's avatar
Christophe Henry committed
118 119
                .instantiate(activity!!.classLoader, SubscriptionsFragment::class.qualifiedName!!)
                .apply {setArguments(arguments)}
120
        }
121
    }
122
}