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

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

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

39
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
40
        disableShiftingOfNabBarItems()
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

        object : AsyncTask<Void, Void, Void>() {

            override fun doInBackground(vararg p0: Void?): Void? {
                val installManager = installManagerGetter.connectAndGet(this@MainActivity)
                initialiseFragments(installManager)
                return null
            }

            override fun onPostExecute(result: Void?) {
                // Show the home fragment by default
                if (savedInstanceState != null && savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
                    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
                    }
                }
            }
        }.executeOnExecutor(Common.EXECUTOR)
Nihar Thakkar's avatar
Nihar Thakkar committed
63
    }
64

65
    private fun initialiseFragments(installManager: InstallManager) {
66
        homeFragment.initialise(installManager)
67
68
69
70
        searchFragment.initialise(installManager)
        updatesFragment.initialise(installManager)
    }

71
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
72
73
74
75
76
77
78
79
80
81
        if (selectFragment(item.itemId)) {
            currentFragmentId = item.itemId
            return true
        }
        return false
    }

    private fun selectFragment(fragmentId: Int): Boolean {
        when (fragmentId) {
            R.id.menu_home -> {
82
83
84
                showFragment(homeFragment)
                return true
            }
85
            R.id.menu_categories -> {
86
87
88
                showFragment(categoriesFragment)
                return true
            }
89
            R.id.menu_search -> {
90
91
92
                showFragment(searchFragment)
                return true
            }
93
            R.id.menu_updates -> {
94
95
96
                showFragment(updatesFragment)
                return true
            }
97
            R.id.menu_settings -> {
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                showFragment(settingsFragment)
                return true
            }
        }
        return false
    }

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

112
    @SuppressLint("RestrictedApi")
113
114
    private fun disableShiftingOfNabBarItems() {
        val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        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)
        }
    }
132
133
134
135
136
137

    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()
        }
    }
138

139
140
141
142
143
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
    }

144
145
    override fun onDestroy() {
        super.onDestroy()
146
        installManagerGetter.disconnect(this)
147
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
148
}