-
Notifications
You must be signed in to change notification settings - Fork 12
/
SkeletonActivity.kt
382 lines (323 loc) · 12.4 KB
/
SkeletonActivity.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package me.shkschneider.skeleton.android.app
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.text.InputType
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import androidx.annotation.AnimRes
import androidx.annotation.ColorInt
import androidx.annotation.LayoutRes
import androidx.annotation.UiThread
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import me.shkschneider.skeleton.android.R
import me.shkschneider.skeleton.android.content.SkeletonReceiver
import me.shkschneider.skeleton.android.content.IntentHelper
import me.shkschneider.skeleton.android.helper.KeyboardHelper
import me.shkschneider.skeleton.android.os.ThreadHelper
import me.shkschneider.skeleton.android.view.OverlayLoader
import me.shkschneider.skeleton.kotlin.log.Logger
/**
* https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
* https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html
*
* +---------+----------+-------+------------+
* | Created | Inflated | Alive | Foreground |
* +---------+----------+-------+------------+
* | x | | | | onCreate()
* | x | x | x | | onStart()
* | x | x | x | | onPostCreate()
* | x | x | x | x | onResume()
* | x | x | x | x | onResumeFragments()
* | x | x | x | x | onPostResume()
* | x | x | x | x | onAttachedToWindow()
* +---------+----------+-------+------------+
* | x | x | x | x | onPause()
* | x | x | x | | onStop()
* | x | x | | | onDestroy()
* +---------+----------+-------+------------+
*/
abstract class SkeletonActivity : AppCompatActivity() {
private var enterAnim: Int? = null
private var exitAnim: Int? = null
override fun attachBaseContext(newBase: Context?) {
// LocaleHelper.Application.switch()
super.attachBaseContext(newBase)
}
override fun onCreate(savedInstanceState: Bundle?) {
if (enterAnim != null && exitAnim != null) {
// <https://stackoverflow.com/a/28989946/603270>
window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
}
super.onCreate(savedInstanceState)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
setContentView(R.layout.sk_activity)
statusBarColor(window, ContextCompat.getColor(applicationContext, R.color.statusBarColor))
}
fun overridePendingTransitions(@AnimRes enterAnim: Int, @AnimRes exitAnim: Int) {
this.enterAnim = enterAnim
this.exitAnim = exitAnim
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
Logger.verbose("onNewIntent ${intent.action.orEmpty().toUpperCase()}" + if (intent.extras != null) " (has extras)" else "")
// This part is necessary to ensure that getIntent returns the latest intent when
// this activity is started. By default, getIntent() returns the initial intent
// that was set from another activity that started this activity.
setIntent(intent)
}
// <https://stackoverflow.com/a/27635961/603270>
override fun onResumeFragments() {
super.onResumeFragments()
}
override fun setContentView(view: View) {
super.setContentView(view)
onViewCreated()
}
override fun setContentView(@LayoutRes id: Int) {
super.setContentView(id)
onViewCreated()
}
override fun setContentView(view: View, layoutParams: ViewGroup.LayoutParams) {
super.setContentView(view, layoutParams)
onViewCreated()
}
// Lifecycle
var alive = false
private set
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
// BundleHelper.unpack()
}
protected fun onViewCreated() {
toolbar = findViewById(R.id.toolbar)
toolbar?.let { toolbar ->
setSupportActionBar(toolbar)
title(ApplicationHelper.name())
}
}
override fun onStart() {
super.onStart()
alive = true
if (enterAnim != null && exitAnim != null) {
overridePendingTransition(enterAnim!!, exitAnim!!)
}
}
override fun onStop() {
alive = false
super.onStop()
skeletonReceiver = null
loading(false)
if (enterAnim != null && exitAnim != null) {
overridePendingTransition(enterAnim!!, exitAnim!!)
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
// BundleHelper.pack()
}
override fun recreate() {
super.recreate()
}
// StatusBar
fun statusBarColor(window: Window): Int =
window.statusBarColor
fun statusBarColor(window: Window, @ColorInt color: Int) {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = color
}
// Toolbar
protected var toolbar: Toolbar? = null
fun toolbarColor(@ColorInt color: Int): Boolean? {
toolbar?.setBackgroundColor(color) ?: run {
Logger.warning("Toolbar was NULL")
return false
}
return true
}
override fun setSupportActionBar(toolbar: Toolbar?) {
try {
super.setSupportActionBar(toolbar)
} catch (e: IllegalStateException) {
/**
* java.lang.RuntimeException: Unable to start activity
* java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor.
* Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
*/
e.printStackTrace()
}
this.toolbar = toolbar
}
fun toolbar(): Toolbar? =
toolbar
fun toolbar(show: Boolean = true, home: Boolean = false, logo: Drawable? = null, title: String? = null, subtitle: String? = null) {
toolbar(show)
home(home)
logo(logo)
title(title)
subtitle(subtitle)
}
fun toolbar(b: Boolean) {
toolbar?.visibility = if (b) View.VISIBLE else View.GONE
}
fun home(b: Boolean) {
supportActionBar?.let { actionBar ->
actionBar.setDisplayShowHomeEnabled(b)
actionBar.setDisplayHomeAsUpEnabled(b)
}
}
@Deprecated("NavigationIcon is probably NOT what you want.", ReplaceWith("logo(drawable)"))
fun icon(drawable: Drawable?) {
logo(drawable)
}
fun logo(drawable: Drawable?) {
supportActionBar?.let { actionBar ->
actionBar.setDisplayShowHomeEnabled(drawable != null)
actionBar.setDisplayUseLogoEnabled(drawable != null)
actionBar.setLogo(drawable)
}
}
fun title(title: String?) {
toolbar?.let { toolbar ->
toolbar.title = title
}
supportActionBar?.let { actionBar ->
actionBar.setDisplayShowTitleEnabled(!title.isNullOrEmpty())
actionBar.title = title
}
}
fun title(): String? =
toolbar?.title?.toString() ?: supportActionBar?.title?.toString()
fun subtitle(subtitle: String?) {
toolbar?.let { toolbar ->
toolbar.subtitle = subtitle
}
supportActionBar?.let { actionBar ->
actionBar.setDisplayShowTitleEnabled(!(title.isNullOrEmpty() && subtitle.isNullOrEmpty()))
actionBar.subtitle = subtitle
}
}
fun subtitle(): String? =
toolbar?.subtitle?.toString() ?: supportActionBar?.subtitle?.toString()
// Loading
// <https://gist.github.com/antoniolg/9837398>
private var loading = 0
private var overlayLoader: OverlayLoader? = null
@UiThread
fun loading(): Int =
loading
@UiThread
fun loading(i: Int) {
loading += i
loading(loading > 0)
}
@UiThread
fun loading(b: Boolean) {
if (!ThreadHelper.mainThread()) {
Logger.debug("Not on MainThread!")
}
if (b) {
runOnUiThread {
overlayLoader ?: run {
overlayLoader = OverlayLoader.show(this)
}
}
} else {
runOnUiThread {
overlayLoader?.hide(this)?.also { loading = 0 }
overlayLoader = null
}
}
}
// Search
// <http://stackoverflow.com/q/18438890>
private var searchHint: String? = null
private var skeletonReceiver: SkeletonReceiver? = null
private var searchMenuItem: MenuItem? = null
private var searchView: SearchView? = null
fun searchable(): Boolean {
return skeletonReceiver != null
}
fun searchable(hint: String, skeletonReceiver: SkeletonReceiver?) {
searchHint = hint
this.skeletonReceiver = skeletonReceiver
// AVOID supportInvalidateOptionsMenu()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val skeletonReceiver = this.skeletonReceiver ?: run {
return super.onCreateOptionsMenu(menu)
}
menuInflater.inflate(R.menu.sk_search, menu)
searchMenuItem = menu.findItem(R.id.menu_search)
searchView = (searchMenuItem?.actionView as SearchView?)?.apply {
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
imeOptions = EditorInfo.IME_ACTION_SEARCH
setIconifiedByDefault(true)
queryHint = searchHint
setOnCloseListener {
skeletonReceiver.post(RESULT_SEARCH_CHANGE, "")
return@setOnCloseListener false
}
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(q: String): Boolean {
stopSearch()
skeletonReceiver.post(RESULT_SEARCH_SUBMIT, q)
return true
}
override fun onQueryTextChange(q: String): Boolean {
skeletonReceiver.post(RESULT_SEARCH_CHANGE, q)
return true
}
})
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
skeletonReceiver ?: return super.onOptionsItemSelected(item)
(item.actionView as SearchView?)?.run {
if (item.itemId == R.id.menu_search) {
isIconified = false
return true
}
}
return super.onOptionsItemSelected(item)
}
@UiThread
private fun stopSearch() {
searchMenuItem?.collapseActionView()
KeyboardHelper.hide(window)
searchView?.clearFocus()
}
// Navigation
override fun onSupportNavigateUp(): Boolean {
startActivity(IntentHelper.main())
return true
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
// if (hasFocus) window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
}
override fun onBackPressed() {
if (searchView?.isIconified == true) {
stopSearch()
return
}
finish()
}
// Runtime Permissions
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
companion object {
const val RESULT_SEARCH_CHANGE = "onQueryTextChange"
const val RESULT_SEARCH_SUBMIT = "onQueryTextSubmit"
}
}