-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Siddharth Agarwal
committed
Dec 19, 2024
1 parent
5008054
commit be27c1f
Showing
6 changed files
with
360 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
app/src/main/java/org/simple/clinic/patientattribute/entry/BMIEntrySheet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package org.simple.clinic.patientattribute.entry | ||
|
||
import android.content.Context | ||
import android.os.Parcelable | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.view.inputmethod.EditorInfo | ||
import com.jakewharton.rxbinding3.view.clicks | ||
import com.jakewharton.rxbinding3.widget.editorActions | ||
import com.jakewharton.rxbinding3.widget.textChanges | ||
import com.spotify.mobius.functions.Consumer | ||
import io.reactivex.Observable | ||
import io.reactivex.rxkotlin.cast | ||
import io.reactivex.rxkotlin.toObservable | ||
import kotlinx.parcelize.Parcelize | ||
import org.simple.clinic.R | ||
import org.simple.clinic.ReportAnalyticsEvents | ||
import org.simple.clinic.databinding.SheetBmiReadingBinding | ||
import org.simple.clinic.di.injector | ||
import org.simple.clinic.mobius.DeferredEventSource | ||
import org.simple.clinic.navigation.v2.Router | ||
import org.simple.clinic.navigation.v2.ScreenKey | ||
import org.simple.clinic.navigation.v2.Succeeded | ||
import org.simple.clinic.navigation.v2.fragments.BaseBottomSheet | ||
import org.simple.clinic.widgets.UiEvent | ||
import java.util.UUID | ||
import javax.inject.Inject | ||
|
||
class BMIEntrySheet : BaseBottomSheet< | ||
BMIEntrySheet.Key, | ||
SheetBmiReadingBinding, | ||
BMIEntryModel, | ||
BMIEntryEvent, | ||
BMIEntryEffect, | ||
BMIEntryViewEffect>(), | ||
BMIEntryUi { | ||
|
||
@Inject | ||
lateinit var effectHandlerFactory: BMIEntryEffectHandler.Factory | ||
|
||
@Inject | ||
lateinit var router: Router | ||
|
||
private val additionalEvents = DeferredEventSource<BMIEntryEvent>() | ||
|
||
private val rootLayout | ||
get() = binding.rootLayout | ||
|
||
private val backImageButton | ||
get() = binding.backImageButton | ||
|
||
private val heightEditText | ||
get() = binding.heightEditText | ||
|
||
private val weightEditText | ||
get() = binding.weightEditText | ||
|
||
private val bmiTextView | ||
get() = binding.bmiTextView | ||
|
||
override fun defaultModel() = BMIEntryModel.default( | ||
patientUUID = screenKey.patientId, | ||
) | ||
|
||
override fun uiRenderer() = BMIEntryUiRenderer(this) | ||
|
||
override fun bindView(inflater: LayoutInflater, container: ViewGroup?) = | ||
SheetBmiReadingBinding.inflate(layoutInflater, container, false) | ||
|
||
override fun createUpdate() = BMIEntryUpdate() | ||
|
||
override fun createEffectHandler(viewEffectsConsumer: Consumer<BMIEntryViewEffect>) = | ||
effectHandlerFactory.create(this).build() | ||
|
||
override fun additionalEventSources() = listOf( | ||
additionalEvents | ||
) | ||
|
||
override fun events() = Observable | ||
.mergeArray( | ||
heightChanges(), | ||
weightChanges(), | ||
weightBackspaceClicks(), | ||
imeDoneClicks(), | ||
hardwareBackPresses(), | ||
backButtonClicks(), | ||
) | ||
.compose(ReportAnalyticsEvents()) | ||
.cast<BMIEntryEvent>() | ||
|
||
override fun onAttach(context: Context) { | ||
super.onAttach(context) | ||
context.injector<Injector>().inject(this) | ||
} | ||
|
||
private fun backButtonClicks(): Observable<UiEvent> { | ||
return backImageButton | ||
.clicks() | ||
.map { BackPressed } | ||
} | ||
|
||
private fun heightChanges() = heightEditText | ||
.textChanges() | ||
.map(CharSequence::toString) | ||
.map(::HeightChanged) | ||
|
||
private fun weightChanges() = weightEditText | ||
.textChanges() | ||
.map(CharSequence::toString) | ||
.map(::WeightChanged) | ||
|
||
private fun weightBackspaceClicks(): Observable<UiEvent> { | ||
return weightEditText | ||
.backspaceClicks | ||
.map { WeightBackspaceClicked } | ||
} | ||
|
||
private fun imeDoneClicks(): Observable<SaveClicked> { | ||
return listOf(heightEditText, weightEditText) | ||
.map { it.editorActions { actionId -> actionId == EditorInfo.IME_ACTION_DONE } } | ||
.toObservable() | ||
.flatMap { it } | ||
.map { SaveClicked } | ||
} | ||
|
||
private fun hardwareBackPresses(): Observable<UiEvent> { | ||
return Observable.create { emitter -> | ||
val interceptor = { | ||
emitter.onNext(BackPressed) | ||
} | ||
emitter.setCancellable { rootLayout.backKeyPressInterceptor = null } | ||
rootLayout.backKeyPressInterceptor = interceptor | ||
} | ||
} | ||
|
||
override fun closeSheet() { | ||
router.popWithResult(Succeeded(BMIAdded)) | ||
} | ||
|
||
override fun changeFocusToHeight() { | ||
heightEditText.requestFocus() | ||
} | ||
|
||
override fun changeFocusToWeight() { | ||
weightEditText.requestFocus() | ||
} | ||
|
||
override fun showBMI(bmi: String) { | ||
bmiTextView.text = getString(R.string.bmi_x, bmi) | ||
bmiTextView.visibility = View.VISIBLE | ||
} | ||
|
||
override fun hideBMI() { | ||
bmiTextView.visibility = View.GONE | ||
} | ||
|
||
@Parcelize | ||
data class Key( | ||
val patientId: UUID, | ||
) : ScreenKey() { | ||
|
||
override val analyticsName = "BMI Entry Sheet" | ||
|
||
override val type = ScreenType.Modal | ||
|
||
override fun instantiateFragment() = BMIEntrySheet() | ||
} | ||
|
||
interface Injector { | ||
fun inject(target: BMIEntrySheet) | ||
} | ||
|
||
@Parcelize | ||
object BMIAdded : Parcelable | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<solid android:color="@color/simple_light_grey" /> | ||
<corners android:radius="24dp" /> | ||
<padding | ||
android:bottom="4dp" | ||
android:left="72dp" | ||
android:right="72dp" | ||
android:top="4dp" /> | ||
</shape> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<org.simple.clinic.widgets.LinearLayoutWithPreImeKeyEventListener xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/rootLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:animateLayoutChanges="true" | ||
android:orientation="vertical" | ||
tools:background="?attr/colorSurface" | ||
tools:context=".bp.entry.BloodPressureEntrySheet" | ||
tools:layout_gravity="bottom"> | ||
|
||
<LinearLayout | ||
android:id="@+id/bloodpressureentry_flipper_bp_entry" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
android:paddingBottom="@dimen/spacing_12"> | ||
|
||
<RelativeLayout | ||
android:layout_width="match_parent" | ||
android:layout_marginTop="24dp" | ||
android:layout_height="wrap_content"> | ||
|
||
<ImageButton | ||
android:id="@+id/backImageButton" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="@dimen/spacing_8" | ||
android:layout_alignParentStart="true" | ||
android:layout_centerVertical="true" | ||
android:background="?selectableItemBackgroundBorderless" | ||
android:padding="@dimen/spacing_8" | ||
app:srcCompat="@drawable/ic_arrow_back_24dp" | ||
app:tint="?attr/colorPrimary" | ||
tools:ignore="ContentDescription" /> | ||
|
||
<TextView | ||
android:id="@+id/enterBMITitleTextView" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_centerInParent="true" | ||
android:gravity="center_horizontal" | ||
android:lines="1" | ||
android:text="@string/bmi_entry_sheet_title_enter_bmi" | ||
android:textAppearance="?attr/textAppearanceHeadline6" | ||
android:textColor="?attr/colorOnSurface" | ||
android:visibility="gone" | ||
tools:ignore="UnusedAttribute" | ||
tools:visibility="visible" /> | ||
</RelativeLayout> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:id="@+id/bmiEntryLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="@dimen/spacing_16" | ||
android:layout_marginBottom="@dimen/spacing_12" | ||
app:layout_optimizationLevel="direct|barrier"> | ||
|
||
<com.google.android.material.textfield.TextInputLayout | ||
android:id="@+id/heightTextFieldLayout" | ||
style="@style/Widget.Simple.TextField.Layout.Large.MeasurementInput" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintEnd_toStartOf="@+id/bmi_entry_height_weight_separator" | ||
app:layout_constraintHorizontal_chainStyle="packed" | ||
app:layout_constraintStart_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"> | ||
|
||
<com.google.android.material.textfield.TextInputEditText | ||
android:id="@+id/heightEditText" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:imeOptions="actionDone" | ||
android:importantForAutofill="no" | ||
android:inputType="number" | ||
tools:ignore="UnusedAttribute" | ||
tools:text="120"> | ||
|
||
<requestFocus /> | ||
|
||
</com.google.android.material.textfield.TextInputEditText> | ||
|
||
</com.google.android.material.textfield.TextInputLayout> | ||
|
||
<TextView | ||
android:id="@+id/bmi_entry_height_label" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="@dimen/spacing_4" | ||
android:gravity="center_horizontal" | ||
android:labelFor="@+id/systolicEditText" | ||
android:text="@string/bmi_entry_height_in_cm" | ||
android:textAppearance="?attr/textAppearanceBody2" | ||
android:textColor="@color/color_on_surface_67" | ||
app:layout_constraintEnd_toEndOf="@+id/heightTextFieldLayout" | ||
app:layout_constraintStart_toStartOf="@+id/heightTextFieldLayout" | ||
app:layout_constraintTop_toBottomOf="@+id/heightTextFieldLayout" /> | ||
|
||
<TextView | ||
android:id="@+id/bmi_entry_height_weight_separator" | ||
style="@style/Widget.Simple.MeasurementInputSeparator" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintBottom_toBottomOf="@+id/heightTextFieldLayout" | ||
app:layout_constraintEnd_toStartOf="@+id/weightTextFieldLayout" | ||
app:layout_constraintStart_toEndOf="@+id/heightTextFieldLayout" | ||
app:layout_constraintTop_toTopOf="@+id/heightTextFieldLayout" /> | ||
|
||
<com.google.android.material.textfield.TextInputLayout | ||
android:id="@+id/weightTextFieldLayout" | ||
style="@style/Widget.Simple.TextField.Layout.Large.MeasurementInput" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintEnd_toStartOf="parent" | ||
app:layout_constraintStart_toEndOf="@+id/bmi_entry_height_weight_separator" | ||
app:layout_constraintTop_toTopOf="parent"> | ||
|
||
<org.simple.clinic.widgets.EditTextWithBackspaceListener | ||
android:id="@+id/weightEditText" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:imeOptions="actionDone" | ||
android:importantForAutofill="no" | ||
android:inputType="number" | ||
tools:ignore="UnusedAttribute" | ||
tools:text="80" /> | ||
|
||
</com.google.android.material.textfield.TextInputLayout> | ||
|
||
<TextView | ||
android:id="@+id/bmi_entry_weight_label" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:gravity="center_horizontal" | ||
android:labelFor="@id/diastolicEditText" | ||
android:text="@string/bmi_entry_weight_in_kg" | ||
android:textAppearance="?attr/textAppearanceBody2" | ||
android:textColor="@color/color_on_surface_67" | ||
app:layout_constraintEnd_toEndOf="@+id/weightTextFieldLayout" | ||
app:layout_constraintStart_toStartOf="@+id/weightTextFieldLayout" | ||
app:layout_constraintTop_toBottomOf="@+id/weightTextFieldLayout" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
<com.google.android.material.textview.MaterialTextView | ||
android:id="@+id/bmiTextView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center_horizontal" | ||
android:textAppearance="?attr/textAppearanceBody1" | ||
android:textColor="@color/color_on_surface_67" | ||
android:background="@drawable/background_bmi" | ||
tools:text="BMI:32" /> | ||
|
||
</LinearLayout> | ||
|
||
</org.simple.clinic.widgets.LinearLayoutWithPreImeKeyEventListener> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters