This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLoaderFragment.kt
84 lines (68 loc) · 3.13 KB
/
LoaderFragment.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
package net.aquadc.advancedkotlinpatterns.feature.crossinlineOneLiner
import android.app.Fragment
import android.app.LoaderManager
import android.content.Loader
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
import net.aquadc.advancedkotlinpatterns.feature.sealed.unions.either.Either
import net.aquadc.advancedkotlinpatterns.feature.sealed.unions.either.let
import org.jetbrains.anko.*
class LoaderFragment : Fragment(), LoaderManager.LoaderCallbacks<Either<Throwable, CharSequence>> {
private lateinit var loaderTextView: TextView
private lateinit var watchedEditText: EditText
private lateinit var watchedTextView: TextView
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
loaderManager.initLoader(0, null, this)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?) = UI {
verticalLayout {
layoutParams = FrameLayout.LayoutParams(matchParent, matchParent)
padding = dip(16)
loaderTextView = textView("Loading...") {
textSize = 22f
movementMethod = LinkMovementMethod.getInstance()
setLineSpacing(0f, 1.2f)
}.lparams(width = matchParent)
watchedEditText = editText {
id = 1
hint = "Type anything..."
addTextChangedListener(AfterTextChangedListener { watchedTextView.text = it })
}.lparams(width = matchParent) {
topMargin = dip(16)
}
watchedTextView = textView("I'm watching you!") {
textSize = 14f
}.lparams(width = matchParent) {
topMargin = dip(16)
}
}
}.view
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Either<Throwable, CharSequence>> = when (id) {
0 -> CachedAsyncTaskLoader(activity) {
Thread.sleep(3_000)
SpannableStringBuilder("Content loaded! You may also ").apply {
var start = length
append("click here to see the power")
setSpan(ClickableSpan { it.context.toast("See the power!") }, start, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
append(", or ")
start = length
append("tap here to feel the force")
setSpan(ClickableSpan { it.context.vibrator.vibrate(500) }, start, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
append(".")
}
}
else -> throw IndexOutOfBoundsException()
}
override fun onLoadFinished(loader: Loader<Either<Throwable, CharSequence>>, data: Either<Throwable, CharSequence>) {
loaderTextView.text = data.let({ it.message ?: it.toString() }, { it })
}
override fun onLoaderReset(loader: Loader<Either<Throwable, CharSequence>>) = Unit
}