MainActivity.kt 4.53 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
Nihar Thakkar's avatar
Nihar Thakkar committed
21
import io.eelo.appinstaller.utils.Constants.STORAGE_PERMISSION_REQUEST_CODE
22
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
23

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

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

37
38
        Common.EXECUTOR.submit {
            val installManager = installManagerGetter.connectAndGet(this)
39
            initialiseFragments(installManager)
40
            // Show the home fragment by default
41
            showFragment(homeFragment)
42
        }
43

44
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
45
        disableShiftingOfNabBarItems()
Nihar Thakkar's avatar
Nihar Thakkar committed
46
    }
47

48
    private fun initialiseFragments(installManager: InstallManager) {
49
        homeFragment.initialise(installManager)
50
51
52
53
        searchFragment.initialise(installManager)
        updatesFragment.initialise(installManager)
    }

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when {
            item.itemId == R.id.menu_home -> {
                showFragment(homeFragment)
                return true
            }
            item.itemId == R.id.menu_categories -> {
                showFragment(categoriesFragment)
                return true
            }
            item.itemId == R.id.menu_search -> {
                showFragment(searchFragment)
                return true
            }
            item.itemId == R.id.menu_updates -> {
                showFragment(updatesFragment)
                return true
            }
            item.itemId == R.id.menu_settings -> {
                showFragment(settingsFragment)
                return true
            }
        }
        return false
    }

    private fun showFragment(fragment: Fragment) {
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
                .commit()
85
        currentFragment = fragment
86
87
    }

88
    @SuppressLint("RestrictedApi")
89
90
    private fun disableShiftingOfNabBarItems() {
        val menuView = bottom_navigation_view.getChildAt(0) as BottomNavigationMenuView
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        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)
        }
    }
108
109
110
111
112
113

    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()
        }
    }
114
115
116

    override fun onDestroy() {
        super.onDestroy()
117
        installManagerGetter.disconnect(this)
118
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
119
}