MainActivity.kt 5.77 KB
Newer Older
1
package foundation.e.apps
Nihar Thakkar's avatar
Nihar Thakkar committed
2

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.design.widget.Snackbar
10
import android.support.v4.app.Fragment
11
import android.support.v7.app.AppCompatActivity
12
import android.view.MenuItem
13
14
15
16
17
18
19
20
21
22
import foundation.e.apps.applicationmanager.ApplicationManager
import foundation.e.apps.applicationmanager.ApplicationManagerServiceConnection
import foundation.e.apps.applicationmanager.ApplicationManagerServiceConnectionCallback
import foundation.e.apps.categories.CategoriesFragment
import foundation.e.apps.home.HomeFragment
import foundation.e.apps.search.SearchFragment
import foundation.e.apps.settings.SettingsFragment
import foundation.e.apps.updates.UpdatesFragment
import foundation.e.apps.utils.Constants
import foundation.e.apps.utils.Constants.CURRENTLY_SELECTED_FRAGMENT_KEY
23
import kotlinx.android.synthetic.main.activity_main.*
Nihar Thakkar's avatar
Nihar Thakkar committed
24

25
26
class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener,
        ApplicationManagerServiceConnectionCallback {
27
    private var currentFragmentId = 0
28
    private val homeFragment = HomeFragment()
29
    private val searchFragment = SearchFragment()
30
    private val updatesFragment = UpdatesFragment()
31
32
    private val applicationManagerServiceConnection =
            ApplicationManagerServiceConnection(this)
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
        bottom_navigation_view.setOnNavigationItemSelectedListener(this)
39
        disableShiftingOfNabBarItems()
40

Nihar Thakkar's avatar
Nihar Thakkar committed
41
42
        initialiseUpdatesWorker()

43
44
45
46
        // Show the home fragment by default
        currentFragmentId = if (savedInstanceState != null &&
                savedInstanceState.containsKey(CURRENTLY_SELECTED_FRAGMENT_KEY)) {
            savedInstanceState.getInt(CURRENTLY_SELECTED_FRAGMENT_KEY)
47
48
        } else if (intent.hasExtra(Constants.UPDATES_NOTIFICATION_CLICK_EXTRA)) {
            R.id.menu_updates
49
50
51
        } else {
            R.id.menu_home
        }
52

53
54
        applicationManagerServiceConnection.bindService(this)
    }
55

Nihar Thakkar's avatar
Nihar Thakkar committed
56
    private fun initialiseUpdatesWorker() {
57
        UpdatesManager(applicationContext).startWorker()
Nihar Thakkar's avatar
Nihar Thakkar committed
58
59
    }

60
61
62
    override fun onServiceBind(applicationManager: ApplicationManager) {
        initialiseFragments(applicationManager)
        selectFragment(currentFragmentId)
Nihar Thakkar's avatar
Nihar Thakkar committed
63
    }
64

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

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 -> {
Nihar Thakkar's avatar
Nihar Thakkar committed
86
                showFragment(CategoriesFragment())
87
88
                return true
            }
89
            R.id.menu_search -> {
90
91
92
                showFragment(searchFragment)
                return true
            }
Nihar Thakkar's avatar
Nihar Thakkar committed
93
            R.id.menu_updates -> {
94
95
                showFragment(updatesFragment)
                return true
Nihar Thakkar's avatar
Nihar Thakkar committed
96
            }
97
            R.id.menu_settings -> {
Nihar Thakkar's avatar
Nihar Thakkar committed
98
                showFragment(SettingsFragment())
99
100
101
102
103
104
105
                return true
            }
        }
        return false
    }

    private fun showFragment(fragment: Fragment) {
106
        bottom_navigation_view.menu.findItem(currentFragmentId).isChecked = true
107
108
109
110
111
112
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.frame_layout, fragment)
                .commit()
    }

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

134
135
136
137
138
139
140
141
142
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
                                            grantResults: IntArray) {
        if (requestCode == Constants.STORAGE_PERMISSION_REQUEST_CODE &&
                grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_DENIED) {
            Snackbar.make(container, R.string.error_storage_permission_denied,
                    Snackbar.LENGTH_LONG).show()
        }
    }

143
144
145
146
147
    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putInt(CURRENTLY_SELECTED_FRAGMENT_KEY, currentFragmentId)
    }

148
149
    override fun onDestroy() {
        super.onDestroy()
150
151
152
        homeFragment.decrementApplicationUses()
        searchFragment.decrementApplicationUses()
        updatesFragment.decrementApplicationUses()
153
        applicationManagerServiceConnection.unbindService(this)
154
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
155
}