11package com.mozzarelly.cbthelper
22
33import android.content.Intent
4+ import android.graphics.text.LineBreaker
5+ import android.os.Build
6+ import android.text.Editable
7+ import android.text.Layout
8+ import android.text.TextWatcher
49import android.view.View
510import android.widget.CheckBox
11+ import android.widget.RadioButton
612import android.widget.TextView
713import androidx.fragment.app.Fragment
814import androidx.lifecycle.LiveData
15+ import androidx.lifecycle.MutableLiveData
16+ import androidx.lifecycle.Observer
17+ import com.mozzarelly.cbthelper.databinding.FragmentBehaviorQuestion3RadiosBinding
918
10- open class CBTFragment : Fragment () {
19+
20+ abstract class CBTFragment : Fragment () {
1121
1222 val act by lazy {
1323 @Suppress(" UNCHECKED_CAST" )
14- requireActivity() as CBTActivity <PagingViewModel >
24+ requireActivity() as CBTActivity <InterviewViewModel >
25+ }
26+
27+ abstract val title: String
28+
29+ inline fun <reified T : Any ? > TextView.displayDatum (liveData : LiveData <T >){
30+ /*
31+ val drawable = TopGravityDrawable(resources, BitmapFactory.decodeResource(resources, R.drawable.ic_quote))
32+ drawable.setTintList(foregroundTintList)
33+ drawable.setTintMode(PorterDuff.Mode.SRC_IN)
34+ setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
35+ compoundDrawablePadding = 8
36+ compoundDrawableTintList = foregroundTintList
37+ */
38+
39+ /*
40+ viewLifecycleOwner.observe(liveData) {
41+ val imageSpan = ImageSpan(context, R.drawable.ic_quote);
42+ val ss = SpannableString(" " + it.toString());
43+ ss.setSpan(imageSpan, 0, 1, 0);
44+ text = ss
45+ }
46+ */
47+ display(liveData)
48+ }
49+
50+ inline fun <reified V : View , reified T : Any ? > V.bindTo (liveData : MutableLiveData <T >, text : Int? = null, value : Any? = null){
51+ when (this ) {
52+ is RadioButton -> {
53+ text?.let { this .text = getString(it) }
54+ this .setTag(value)
55+ this .setOnCheckedChangeListener { _, isChecked ->
56+ if (isChecked && value != liveData.value)
57+ liveData.value = (value as ? T ) ? : error(" Need value for radio" )
58+ }
59+ }
60+ is CheckBox -> {
61+ text?.let { this .text = getString(it) }
62+ this .setOnCheckedChangeListener { _, isChecked ->
63+ if (isChecked != liveData.value)
64+ liveData.value = isChecked as T
65+ }
66+ }
67+ is TextView -> {
68+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
69+ justificationMode = LineBreaker .JUSTIFICATION_MODE_INTER_WORD
70+ }
71+
72+ text?.let { this .text = getString(it) }
73+ this .addTextChangedListener(object : TextWatcher {
74+ override fun afterTextChanged (s : Editable ? ) {}
75+ override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
76+ override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {
77+ /* [email protected] ?*/ s?.toString()?.
takeIf { it
!= liveData.value }?.
let {
78+ liveData.value = it as T
79+ }
80+ }
81+ })
82+ }
83+ else -> error(" Can't handle ${this ::class .java.name} && ${T ::class .java.name} " )
84+ }
85+
86+ liveData.observe(viewLifecycleOwner, Observer { new ->
87+ when (this ){
88+ is RadioButton -> (new as ? Int ).let {
89+ if (this .getTag() == it)
90+ this .isChecked = true
91+ }
92+ is CheckBox -> new.toBoolean().let {
93+ if (this .isChecked != it)
94+ this .isChecked = it
95+ }
96+ is TextView -> new?.toString()?.let {
97+ if (it != this .text.toString())
98+ this .text = it
99+ }
100+ else -> error(" Can't support ${this ::class .qualifiedName} " )
101+ }
102+ })
15103 }
16104
17105 inline fun <reified V : View , reified T : Any ? > V.display (liveData : LiveData <T >, crossinline transform : (T ? ) -> String? = { it?.toString() }){
106+ if (this is TextView ) {
107+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
108+ justificationMode = LineBreaker .JUSTIFICATION_MODE_INTER_WORD
109+ }
110+ }
111+
18112 viewLifecycleOwner.observe(liveData) {
19113 when (this ){
20114 is TextView -> text = transform(it)
@@ -24,6 +118,10 @@ open class CBTFragment : Fragment() {
24118 }
25119 }
26120
121+ inline fun <reified V : View , reified T : Any ? > LiveData<T>.displayIn (view : V , crossinline transform : (T ? ) -> String? = { it?.toString() }){
122+ view.display(this )
123+ }
124+
27125 fun <T : Any ?> T?.toBoolean (): Boolean = when (this ){
28126 null -> false
29127 true -> true
@@ -43,14 +141,20 @@ open class CBTFragment : Fragment() {
43141 }
44142*/
45143
46- inline fun <reified A : CBTActivity <* >> start (requestCode : Int ) = act.startActivityForResult(Intent (requireContext(), A ::class .java), requestCode)
144+ inline fun <reified A : CBTActivity <* >> start () = act.startActivityForResult(Intent (requireContext(), A ::class .java), A ::class .requestCode())
145+
146+ inline fun <reified A : CBTActivity <* >> start (id : Int ) {
147+ act.startActivityForResult(Intent (act, A ::class .java).apply {
148+ putExtra(" id" , id.toString())
149+ }, A ::class .requestCode())
150+ }
47151
48- inline fun <reified A : CBTActivity <* >> start (requestCode : Int , vararg extras : Pair <String , String >) {
152+ inline fun <reified A : CBTActivity <* >> start (vararg extras : Pair <String , String >) {
49153 act.startActivityForResult(Intent (requireContext(), A ::class .java).apply {
50154 extras.forEach {
51155 putExtra(it.first, it.second)
52156 }
53- }, requestCode)
157+ }, A :: class . requestCode() )
54158 }
55159
56160 fun getIdExtra () = act.getIdExtra()
0 commit comments