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

3
import android.annotation.SuppressLint
4
import android.content.pm.PackageManager
Nihar Thakkar's avatar
Nihar Thakkar committed
5
import android.os.Bundle
6
7
8
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
9
import android.support.v4.app.Fragment
10
import android.support.v7.app.AppCompatActivity
11
import android.view.MenuItem
12
import android.widget.Toast
13
import io.eelo.appinstaller.application.model.InstallManager
14
import io.eelo.appinstaller.application.model.InstallManagerGetter
15
16
17
18
19
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
20
import io.eelo.appinstaller.utils.Common
21
import io.eelo.appinstaller.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
Nihar Thakkar's avatar
Nihar Thakkar committed
22
import io.eelo.appinstaller.utils.Constants.STORAGE_PERMISSION_REQUEST_CODE
23
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
24

25
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener {
26
    private var currentFragmentId = 0
27
28
    private val homeFragment = HomeFragment()
    private val categoriesFragment = CategoriesFragment()
29
    private val searchFragment = SearchFragment()
30
31
    private val updatesFragment = UpdatesFragment()
    private val settingsFragment = SettingsFragment()
32
    private val installManagerGetter = InstallManagerGetter()
Nihar Thakkar's avatar
Nihar Thakkar committed
33
34
35
36

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

38
39
        Common.EXECUTOR.submit {
            val installManager = installManagerGetter.connectAndGet(this)
40
            initialiseFragments(installManager)
41
            // Show the home fragment by default
42
            if (savedInstanceState != null && savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
43
44
45
46
47
48
49
50
                if (selectFragment(savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY))) {
                    currentFragmentId = savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
                }
            } else {
                if (selectFragment(R.id.menu_home)) {
                    currentFragmentId = R.id.menu_home
                }
            }
51
        }
52

53
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
54
        disableShiftingOfNabBarItems()
Nihar Thakkar's avatar
Nihar Thakkar committed
55
    }
56

57
    private fun initialiseFragments(installManager: InstallManager) {
58
        homeFragment.initialise(installManager)
59
60
61
62
        searchFragment.initialise(installManager)
        updatesFragment.initialise(installManager)
    }

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

    private fun selectFragment(fragmentId: Int): Boolean {
        when (fragmentId) {
            R.id.menu_home -> {
74
75
76
                showFragment(homeFragment)
                return true
            }
77
            R.id.menu_categories -> {
78
79
80
                showFragment(categoriesFragment)
                return true
            }
81
            R.id.menu_search -> {
82
83
84
                showFragment(searchFragment)
                return true
            }
85
            R.id.menu_updates -> {
86
87
88
                showFragment(updatesFragment)
                return true
            }
89
            R.id.menu_settings -> {
90
91
92
93
94
95
96
97
98
99
100
101
102
103
                showFragment(settingsFragment)
                return true
            }
        }
        return false
    }

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

104
    @SuppressLint("RestrictedApi")
105
106
    private fun disableShiftingOfNabBarItems() {
        val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        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)
        }
    }
124
125
126
127
128
129

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        if (requestCode == STORAGE_PERMISSION_REQUEST_CODE && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_DENIED) {
            Toast.makeText(this, resources.getString(R.string.error_storage_permission_denied), Toast.LENGTH_LONG).show()
        }
    }
130

131
132
133
134
135
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
    }

136
137
    override fun onDestroy() {
        super.onDestroy()
138
        installManagerGetter.disconnect(this)
139
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
140
}