/
MainActivity.kt
192 lines (182 loc) · 7.4 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright © 2020 - present | Handwashing reminder by Javinator9889
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* Created by Javinator9889 on 15/03/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.activities
import android.os.Bundle
import android.util.SparseArray
import android.view.MenuItem
import androidx.annotation.IdRes
import androidx.core.util.forEach
import androidx.core.util.set
import androidx.core.view.forEach
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.perf.metrics.AddTrace
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.activities.support.ActionBarBase
import com.javinator9889.handwashingreminder.activities.views.fragments.diseases.DiseasesFragment
import com.javinator9889.handwashingreminder.activities.views.fragments.news.NewsFragment
import com.javinator9889.handwashingreminder.activities.views.fragments.settings.SettingsView
import com.javinator9889.handwashingreminder.activities.views.fragments.washinghands.WashingHandsFragment
import com.javinator9889.handwashingreminder.application.HandwashingApplication
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.mikepenz.iconics.typeface.library.ionicons.Ionicons
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.how_to_wash_hands_layout.*
import timber.log.Timber
import java.lang.ref.WeakReference
import kotlin.concurrent.thread
import kotlin.properties.Delegates
class MainActivity : ActionBarBase(),
BottomNavigationView.OnNavigationItemSelectedListener {
override val layoutId: Int = R.layout.activity_main
private val fragments: SparseArray<WeakReference<Fragment>> = SparseArray(4)
private var activeFragment by Delegates.notNull<@IdRes Int>()
private lateinit var app: HandwashingApplication
@AddTrace(name = "onCreateMainView")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
with(FirebaseAnalytics.getInstance(this)) {
setCurrentScreen(this@MainActivity, "Main view", null)
}
with(FirebaseRemoteConfig.getInstance()) {
fetchAndActivate()
}
delegateMenuIcons(menu)
val ids =
arrayOf(R.id.diseases, R.id.handwashing, R.id.news, R.id.settings)
for (id in ids)
createFragmentForId(id)
activeFragment = R.id.diseases
menu.setOnNavigationItemSelectedListener(this)
initFragmentView()
}
protected fun delegateMenuIcons(menu: BottomNavigationView) {
thread(start = true) {
menu.menu.forEach { item ->
val icon = when (item.itemId) {
R.id.diseases ->
IconicsDrawable(
this, Ionicons.Icon.ion_ios_medkit
)
R.id.news ->
IconicsDrawable(
this, GoogleMaterial.Icon.gmd_chrome_reader_mode
)
R.id.settings ->
IconicsDrawable(
this, Ionicons.Icon.ion_android_settings
)
else -> null
}
icon?.let { runOnUiThread { item.icon = it } }
}
}
}
override fun onBackPressed() {
if (activeFragment != R.id.diseases &&
activeFragment != R.id.handwashing
) {
menu.selectedItemId = R.id.diseases
onNavigationItemSelected(menu.menu.findItem(R.id.diseases))
} else {
if (activeFragment == R.id.diseases)
super.onBackPressed()
else {
val washingHandsFragment = fragments[activeFragment].get()
?: createFragmentForId(R.id.handwashing)
as WashingHandsFragment
if (washingHandsFragment.pager.currentItem != 0)
washingHandsFragment.pager.currentItem--
else {
menu.selectedItemId = R.id.diseases
onNavigationItemSelected(menu.menu.findItem(R.id.diseases))
}
}
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
val screenTitle = when (item.itemId) {
R.id.diseases -> "diseases"
R.id.handwashing -> "handwashing"
R.id.news -> "news"
R.id.settings -> "settings"
else -> "Main view"
}
with(FirebaseAnalytics.getInstance(this)) {
setCurrentScreen(this@MainActivity, screenTitle, null)
}
return onItemSelected(item.itemId)
}
protected fun onItemSelected(@IdRes id: Int): Boolean {
return try {
loadFragment(id)
if (id == R.id.handwashing)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
else
supportActionBar?.setDisplayHomeAsUpEnabled(false)
true
} catch (e: Exception) {
Timber.e(e, "Unexpected exception")
false
}
}
private fun initFragmentView() {
with(supportFragmentManager.beginTransaction()) {
fragments.forEach { id, reference ->
val fragment = reference.get() ?: createFragmentForId(id)
add(R.id.mainContent, fragment)
hide(fragment)
}
show(
fragments[activeFragment].get() ?: createFragmentForId(
activeFragment
)
)
commit()
}
}
private fun loadFragment(@IdRes id: Int) {
if (id == activeFragment)
return
val fragment = fragments[id].get() ?: return
val displayedFragment = fragments[activeFragment].get()!!
with(supportFragmentManager.beginTransaction()) {
show(fragment)
hide(displayedFragment)
activeFragment = id
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
disallowAddToBackStack()
}.commit()
}
private fun createFragmentForId(@IdRes id: Int): Fragment {
val fragment = when (id) {
R.id.diseases -> DiseasesFragment()
R.id.handwashing -> WashingHandsFragment()
R.id.news -> NewsFragment()
R.id.settings -> SettingsView()
else -> Fragment() // this should never happen
}
fragments[id] = WeakReference(fragment)
return fragment
}
}