Skip to content

Commit 648c70a

Browse files
author
mfelix
committed
Added replacement thoughts interview and behavior interview.
1 parent 5f50252 commit 648c70a

File tree

123 files changed

+4370
-1399
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+4370
-1399
lines changed

app/release/app-release.apk

-3.7 MB
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/>
3131

3232
<activity
33-
android:name=".analyze.AnalyzeEntryActivity"
33+
android:name=".analyze.AnalyzeActivity"
3434
android:label="@string/title_activity_analyze_entry"
3535
android:theme="@style/AppTheme.NoActionBar"
3636
/>
@@ -41,6 +41,18 @@
4141
android:theme="@style/AppTheme.NoActionBar"
4242
/>
4343

44+
<activity
45+
android:name=".replacement.ReplacementThoughtsActivity"
46+
android:label="@string/title_ratrep_activity"
47+
android:theme="@style/AppTheme.NoActionBar"
48+
/>
49+
50+
<activity
51+
android:name=".behavior.BehaviorActivity"
52+
android:label="@string/title_ratrep_activity"
53+
android:theme="@style/AppTheme.NoActionBar"
54+
/>
55+
4456
<activity android:name=".MainActivity"
4557
android:theme="@style/AppTheme.NoActionBar">
4658

app/src/main/java/com/mozzarelly/cbthelper/CBTActivity.kt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.mozzarelly.cbthelper
22

33
import android.app.Activity
4-
import android.app.RemoteInput
54
import android.content.Intent
65
import android.os.Bundle
76
import androidx.appcompat.app.AppCompatActivity
87
import androidx.lifecycle.ViewModel
98
import androidx.lifecycle.ViewModelProvider
10-
import com.mozzarelly.cbthelper.analyze.AnalyzeEntryIntroFragment
9+
import kotlin.reflect.KClass
1110

1211
abstract class CBTActivity<V : CBTViewModel> : AppCompatActivity() {
1312

@@ -35,18 +34,35 @@ abstract class CBTActivity<V : CBTViewModel> : AppCompatActivity() {
3534
}
3635
}
3736

37+
open val onReturnFrom = mapOf<KClass<*>, (Int) -> Unit>(
38+
39+
)
40+
41+
private val onReturn by lazy { onReturnFrom.mapKeys { it.key.requestCode() } }
42+
43+
final override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
44+
super.onActivityResult(requestCode, resultCode, data)
45+
onReturn[requestCode]?.invoke(resultCode)
46+
}
47+
3848
inline fun <reified V : ViewModel> ViewModelProvider.NewInstanceFactory.getAndInit(): V = create(V::class.java)
3949

40-
protected open fun V.setup(){ }
50+
abstract fun V.setup()
4151

42-
inline fun <reified A: CBTActivity<*>> start(requestCode: Int) = startActivityForResult(Intent(this@CBTActivity, A::class.java), requestCode)
52+
inline fun <reified A: CBTActivity<*>> start() = startActivityForResult(Intent(this@CBTActivity, A::class.java), A::class.requestCode())
4353

44-
inline fun <reified A: CBTActivity<*>> start(requestCode: Int, vararg extras: Pair<String, String>) {
54+
inline fun <reified A: CBTActivity<*>> start(id: Int) {
55+
startActivityForResult(Intent(this@CBTActivity, A::class.java).apply {
56+
putExtra("id", id.toString())
57+
}, A::class.requestCode())
58+
}
59+
60+
inline fun <reified A: CBTActivity<*>> start(vararg extras: Pair<String, String>) {
4561
startActivityForResult(Intent(this@CBTActivity, A::class.java).apply {
4662
extras.forEach {
4763
putExtra(it.first, it.second)
4864
}
49-
}, requestCode)
65+
}, A::class.requestCode())
5066
}
5167

5268
inline fun <reified T: Any> Activity.extra(key: String, default: T? = null) = lazy {
@@ -68,8 +84,14 @@ abstract class CBTActivity<V : CBTViewModel> : AppCompatActivity() {
6884

6985
inline fun <reified T: CBTFragment> showFragment() = supportFragmentManager.runTx {
7086
T::class.java.newInstance().let {
71-
add(R.id.contentFragment, it)
72-
show(it)
87+
replace(R.id.contentFragment, it)
88+
}
89+
}
90+
91+
fun show(fragment: CBTFragment){
92+
supportFragmentManager.runTx {
93+
replace(R.id.contentFragment, fragment)
94+
title = fragment.title
7395
}
7496
}
7597
}

app/src/main/java/com/mozzarelly/cbthelper/CBTFragment.kt

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,114 @@
11
package com.mozzarelly.cbthelper
22

33
import 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
49
import android.view.View
510
import android.widget.CheckBox
11+
import android.widget.RadioButton
612
import android.widget.TextView
713
import androidx.fragment.app.Fragment
814
import 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

Comments
 (0)