MainActivity.kt 4.95 KB
Newer Older
Nihar Thakkar's avatar
Nihar Thakkar committed
1
2
package io.eelo.appinstaller

3
import android.annotation.SuppressLint
Nihar Thakkar's avatar
Nihar Thakkar committed
4
import android.os.Bundle
5
6
7
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
8
import android.support.v4.app.Fragment
9
import android.support.v7.app.AppCompatActivity
10
import android.view.MenuItem
11
12
import io.eelo.appinstaller.applicationmanager.ApplicationManager
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnection
13
import io.eelo.appinstaller.applicationmanager.ApplicationManagerServiceConnectionCallback
14
15
16
17
18
import io.eelo.appinstaller.categories.CategoriesFragment
import io.eelo.appinstaller.home.HomeFragment
import io.eelo.appinstaller.search.SearchFragment
import io.eelo.appinstaller.settings.SettingsFragment
import io.eelo.appinstaller.updates.UpdatesFragment
19
import io.eelo.appinstaller.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
20
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
21

22
23
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
        ApplicationManagerServiceConnectionCallback {
24
    private var currentFragmentId = 0
25
    private val homeFragment = HomeFragment()
26
    private val searchFragment = SearchFragment()
27
    private val updatesFragment = UpdatesFragment()
28
29
    private val applicationManagerServiceConnection =
            ApplicationManagerServiceConnection(this)
Nihar Thakkar's avatar
Nihar Thakkar committed
30
31
32
33

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
34

35
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
36
        disableShiftingOfNabBarItems()
37

38
39
40
41
42
43
44
        // Show the home fragment by default
        currentFragmentId = if (savedInstanceState != null &&
                savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
            savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
        } else {
            R.id.menu_home
        }
45

46
47
        applicationManagerServiceConnection.bindService(this)
    }
48

49
50
51
    override fun onServiceBind(applicationManager: ApplicationManager) {
        initialiseFragments(applicationManager)
        selectFragment(currentFragmentId)
Nihar Thakkar's avatar
Nihar Thakkar committed
52
    }
53

54
55
56
57
    private fun initialiseFragments(applicationManager: ApplicationManager) {
        homeFragment.initialise(applicationManager)
        searchFragment.initialise(applicationManager)
        updatesFragment.initialise(applicationManager)
58
59
    }

60
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
61
62
63
64
65
66
67
68
69
70
        if (selectFragment(item.itemId)) {
            currentFragmentId = item.itemId
            return true
        }
        return false
    }

    private fun selectFragment(fragmentId: Int): Boolean {
        when (fragmentId) {
            R.id.menu_home -> {
71
72
73
                showFragment(homeFragment)
                return true
            }
74
            R.id.menu_categories -> {
Nihar Thakkar's avatar
Nihar Thakkar committed
75
                showFragment(CategoriesFragment())
76
77
                return true
            }
78
            R.id.menu_search -> {
79
80
81
                showFragment(searchFragment)
                return true
            }
82
83
            // TODO Enable once updates screen is implemented
            /*R.id.menu_updates -> {
84
85
                showFragment(updatesFragment)
                return true
86
            }*/
87
            R.id.menu_settings -> {
Nihar Thakkar's avatar
Nihar Thakkar committed
88
                showFragment(SettingsFragment())
89
90
91
92
93
94
95
96
97
98
99
100
101
                return true
            }
        }
        return false
    }

    private fun showFragment(fragment: Fragment) {
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
                .commit()
    }

102
    @SuppressLint("RestrictedApi")
103
104
    private fun disableShiftingOfNabBarItems() {
        val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        try {
            val mShiftingMode = menuView.javaClass.getDeclaredField("mShiftingMode")
            mShiftingMode.isAccessible = true
            mShiftingMode.setBoolean(menuView, false)
            mShiftingMode.isAccessible = false
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }

        for (i in 0 until menuView.childCount) {
            val itemView = menuView.getChildAt(i) as BottomNavigationItemView
            itemView.setShiftingMode(false)
            itemView.setChecked(itemView.itemData.isChecked)
        }
    }
122

123
124
125
126
127
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
    }

128
129
    override fun onDestroy() {
        super.onDestroy()
130
131
132
        homeFragment.decrementApplicationUses()
        searchFragment.decrementApplicationUses()
        updatesFragment.decrementApplicationUses()
133
        applicationManagerServiceConnection.unbindService(this)
134
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
135
}