-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMainActivity.kt
300 lines (252 loc) · 11.6 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
@file:Suppress("NOTHING_TO_INLINE")
package uk.whitecrescent.waqti.frontend
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.Point
import android.graphics.PointF
import android.graphics.RectF
import android.os.Bundle
import android.provider.Settings
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
import androidx.core.view.isVisible
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.blank_activity.*
import kotlinx.android.synthetic.main.navigation_header.view.*
import org.jetbrains.anko.collections.forEachReversedByIndex
import org.jetbrains.anko.configuration
import org.jetbrains.anko.displayMetrics
import org.jetbrains.anko.textColor
import uk.whitecrescent.waqti.BuildConfig
import uk.whitecrescent.waqti.R
import uk.whitecrescent.waqti.backend.persistence.Caches
import uk.whitecrescent.waqti.backend.persistence.Database
import uk.whitecrescent.waqti.backend.task.ID
import uk.whitecrescent.waqti.extensions.F
import uk.whitecrescent.waqti.extensions.I
import uk.whitecrescent.waqti.extensions.addOnBackPressedCallback
import uk.whitecrescent.waqti.extensions.allChildren
import uk.whitecrescent.waqti.extensions.clearFocusAndHideKeyboard
import uk.whitecrescent.waqti.extensions.doInBackground
import uk.whitecrescent.waqti.extensions.doInBackgroundDelayed
import uk.whitecrescent.waqti.extensions.getViewModel
import uk.whitecrescent.waqti.extensions.globalVisibleRect
import uk.whitecrescent.waqti.extensions.invoke
import uk.whitecrescent.waqti.extensions.log
import uk.whitecrescent.waqti.extensions.onTouchOutside
import uk.whitecrescent.waqti.extensions.rootViewGroup
import uk.whitecrescent.waqti.frontend.appearance.ColorScheme
import uk.whitecrescent.waqti.frontend.appearance.WaqtiColor
import uk.whitecrescent.waqti.frontend.customview.AppBar
import uk.whitecrescent.waqti.frontend.customview.recyclerviews.BoardAdapter
import uk.whitecrescent.waqti.frontend.customview.recyclerviews.BoardListView
import uk.whitecrescent.waqti.frontend.fragments.other.AboutFragment
import uk.whitecrescent.waqti.frontend.fragments.other.SettingsFragment
import uk.whitecrescent.waqti.frontend.fragments.view.ViewBoardListFragment
import kotlin.math.roundToInt
const val DRAWER_DELAY_MILLIS = 250L
class MainActivity : AppCompatActivity() {
lateinit var viewModel: MainActivityViewModel
lateinit var preferences: WaqtiPreferences
val touchPoint = PointF()
val onTouchOutSideListeners = HashMap<View, (View) -> Unit>()
override fun onCreate(savedInstanceState: Bundle?) {
preferences = WaqtiPreferences(this)
setTheme(preferences.appTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.blank_activity)
viewModel = getViewModel()
setUpViews()
if (BuildConfig.DEBUG && Database.boards.isEmpty) {
log("Database is empty in Debug mode! Seeding Database!!!")
Caches.seed(boards = 20, lists = 20, tasks = 100)
}
}
private inline fun setUpViews() {
if (supportFragmentManager.fragments.isEmpty()) {
ViewBoardListFragment.show(this)
navigationView?.setCheckedItem(R.id.allBoards_navDrawerItem)
}
addOnBackPressedCallback {
if (drawerLayout.isDrawerOpen(GravityCompat.START) ||
drawerLayout.isDrawerOpen(GravityCompat.END)) {
drawerLayout.closeDrawers()
return@addOnBackPressedCallback
}
if (supportFragmentManager.backStackEntryCount > 0) {
@FragmentNavigation(from = ANY_FRAGMENT, to = PREVIOUS_FRAGMENT)
supportFragmentManager.popBackStack()
} else this@MainActivity.supportFinishAfterTransition()
}
// TODO: 20-Nov-19 Below is getting too messy! We should make an
// onFragmentChangedListener of some sort to fix this
drawerLayout {
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END)
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.START)
useCustomBehavior(GravityCompat.END)
setViewScrimColor(GravityCompat.END, Color.TRANSPARENT)
setViewElevation(GravityCompat.END, 0F)
addDrawerListener(object : DrawerLayout.SimpleDrawerListener() {
private val DEFAULT_SCRIM_COLOR = -0x67000000
private var currentColor: Int = DEFAULT_SCRIM_COLOR
set(value) {
field = value
setScrimColor(value)
}
override fun onDrawerOpened(drawerView: View) {
if (isDrawerOpen(GravityCompat.END)) {
setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.END)
}
if (isDrawerOpen(GravityCompat.START)) {
setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START)
}
}
override fun onDrawerClosed(drawerView: View) {
if (!isDrawerOpen(GravityCompat.END)) {
currentColor = DEFAULT_SCRIM_COLOR
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END)
}
if (!isDrawerOpen(GravityCompat.START)) {
setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.START)
}
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
if (isDrawerVisible(GravityCompat.END) && currentColor != Color.TRANSPARENT) {
currentColor = Color.TRANSPARENT
}
}
})
}
navigationView {
setNavigationItemSelectedListener {
drawerLayout.closeDrawers()
when (it.itemId) {
R.id.allBoards_navDrawerItem -> {
doInBackgroundDelayed(DRAWER_DELAY_MILLIS) {
popAllFragmentsInBackStack()
}
}
R.id.settings_navDrawerItem -> {
doInBackgroundDelayed(DRAWER_DELAY_MILLIS) {
if (currentFragment.tag != SETTINGS_FRAGMENT)
SettingsFragment.show(this@MainActivity)
}
}
R.id.about_navDrawerItem -> {
doInBackgroundDelayed(DRAWER_DELAY_MILLIS) {
if (currentFragment.tag != ABOUT_FRAGMENT)
AboutFragment.show(this@MainActivity)
}
}
}
true
}
}
supportFragmentManager.addOnBackStackChangedListener {
when (currentFragment.tag) {
SETTINGS_FRAGMENT -> navigationView.setCheckedItem(R.id.settings_navDrawerItem)
ABOUT_FRAGMENT -> navigationView.setCheckedItem(R.id.about_navDrawerItem)
else -> navigationView.setCheckedItem(R.id.allBoards_navDrawerItem)
}
}
appBar.editTextView.onTouchOutside {
it.clearFocusAndHideKeyboard()
}
}
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
val spr = super.dispatchTouchEvent(event)
touchPoint.set(event.rawX, event.rawY)
if (event.action == MotionEvent.ACTION_DOWN) {
onTouchOutSideListeners.forEach {
val (view, onClick) = it
if (view.isVisible) {
if (!view.globalVisibleRect.contains(event.rawX.I, event.rawY.I)) {
onClick(view)
}
}
}
}
return spr
}
fun setColorScheme(colorScheme: ColorScheme) {
appBar.setColorScheme(colorScheme)
window.statusBarColor = colorScheme.dark.toAndroidColor
if (preferences.changeNavBarColor)
window.navigationBarColor = colorScheme.main.toAndroidColor
else window.navigationBarColor = WaqtiColor.BLACK.toAndroidColor
(navigationView.getHeaderView(0)) {
setBackgroundColor(colorScheme.main.toAndroidColor)
navigation_header_title?.textColor = colorScheme.text.toAndroidColor
}
}
fun setTheme(appTheme: AppTheme) {
when (appTheme) {
AppTheme.LIGHT -> setTheme(R.style.AppTheme_Light)
AppTheme.DARK -> setTheme(R.style.AppTheme_Dark)
AppTheme.BLACK -> setTheme(R.style.AppTheme_Black)
}
val needsRecreate = appTheme != preferences.appTheme
preferences.appTheme = appTheme
if (needsRecreate) recreate()
}
inline fun resetColorScheme() {
setColorScheme(ColorScheme.WAQTI_DEFAULT)
}
inline fun popAllFragmentsInBackStack() {
supportFragmentManager.doInBackground {
while (backStackEntryCount > 0) {
@FragmentNavigation(from = ANY_FRAGMENT, to = VIEW_BOARD_LIST_FRAGMENT)
popBackStackImmediate() // TODO: 09-Jul-19 Look into changing this to something more efficient
}
}
}
inline fun findViewUnder(pointF: PointF): View? {
drawerLayout.rootViewGroup?.allChildren?.forEachReversedByIndex {
if (it.globalVisibleRect.contains(pointF.x.roundToInt(), pointF.y.roundToInt())) {
return it
}
}
return null
}
inline fun bottomHorizontalRect(y: Float) = RectF(0F, y, realScreenWidth.F, realScreenHeight.F)
inline fun topHorizontalRect(y: Float) = RectF(0F, 0F, realScreenWidth.F, y)
inline fun leftVerticalRect(x: Float) = RectF(0F, 0F, x, realScreenHeight.F)
inline fun rightVerticalRect(x: Float) = RectF(x, 0F, realScreenWidth.F, realScreenHeight.F)
inline val appBar: AppBar
get() = activity_appBar
inline val usableScreenWidth: Int get() = displayMetrics.widthPixels
inline val usableScreenHeight: Int get() = displayMetrics.heightPixels
inline val realScreenWidth: Int
get() = Point().also {
windowManager.defaultDisplay.getRealSize(it)
}.x
inline val realScreenHeight: Int
get() = Point().also {
windowManager.defaultDisplay.getRealSize(it)
}.y
inline val millisPerFrame: Float get() = 1000F / windowManager.defaultDisplay.refreshRate
inline val currentFragment: Fragment
get() = supportFragmentManager.fragments.last()
inline val isDarkTheme: Boolean
get() = preferences.appTheme == AppTheme.DARK || preferences.appTheme == AppTheme.BLACK
inline val isNightMode
get() = (configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
inline val animatorDurationScale: Float
get() = Settings.Global.getFloat(contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1.0F)
}
class MainActivityViewModel : ViewModel() {
var boardID: ID = 0L
var listID: ID = 0L
var taskID: ID = 0L
var boardListState: RecyclerView.SavedState? = null
var onInflateBoardListView: (BoardListView) -> Unit = { }
// TODO: 27-Jun-19 We need to get rid of this guy sometime
var boardPosition = ChangedPositionPair()
var boardAdapter: BoardAdapter? = null
var settingsChanged = false
}