Skip to content

Commit

Permalink
Merge pull request #93 from keepalivedev/AddFeature-TestAlertWarning2
Browse files Browse the repository at this point in the history
fix message count logic and add scroll view to test alert dialog
  • Loading branch information
keepalivedev authored Sep 9, 2024
2 parents 7f48a1d + 05b8165 commit ca8df3f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 61 deletions.
48 changes: 36 additions & 12 deletions app/src/main/java/io/keepalive/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,7 @@ class MainActivity : AppCompatActivity() {
// Initially set visibility based on checkbox state
warningMessageLayout.visibility = if (switchSendWarning.isChecked) View.VISIBLE else View.GONE

// function to calculate the # of SMS that will be sent so it can be
// communicated to the user
fun buildProceedSMSCountMessage(phoneNumberCount: Int): Spanned {
var messageCount = phoneNumberCount

// if we are sending a warning message then double the count
if (switchSendWarning.isChecked) {
messageCount *= 2
}
fun buildProceedSMSCountMessage(messageCount: Int): Spanned {

val proceedMessage = getString(R.string.test_alert_confirmation_message_with_count, messageCount)

Expand All @@ -292,8 +284,8 @@ class MainActivity : AppCompatActivity() {
val phoneNumberStr = getSMSContactString()
messageTextView.text = getString(R.string.test_sms_configured_contacts_message, phoneNumberStr)

// get the message count using the csv string
val messageCount = phoneNumberStr.split(",").size
// get the message count based on the enabled contacts and whether they have location
val messageCount = getSMSMessageCount(switchSendWarning.isChecked)

// set the message for the initial dialog load
proceedMessageTextView.text = buildProceedSMSCountMessage(messageCount)
Expand All @@ -302,8 +294,11 @@ class MainActivity : AppCompatActivity() {
switchSendWarning.setOnCheckedChangeListener { _, isChecked ->
warningMessageLayout.visibility = if (isChecked) View.VISIBLE else View.GONE

// get the message count based on the enabled contacts and whether they have location
val msgCount = getSMSMessageCount(switchSendWarning.isChecked)

// recalculate the message count when the warning message is enabled/disabled
proceedMessageTextView.text = buildProceedSMSCountMessage(messageCount)
proceedMessageTextView.text = buildProceedSMSCountMessage(msgCount)
}

AlertDialog.Builder(this, R.style.AlertDialogTheme)
Expand Down Expand Up @@ -641,6 +636,35 @@ class MainActivity : AppCompatActivity() {
return smsPhoneNumbers.dropLast(2)
}

// get the number of SMS messages that will be sent when the test alert is triggered
private fun getSMSMessageCount(includeWarning: Boolean): Int {
val smsContacts: MutableList<SMSEmergencyContactSetting> = loadJSONSharedPreference(sharedPrefs,
"PHONE_NUMBER_SETTINGS")

var messageCount = 0

// loop through the SMS contacts and create a csv string of the enabled contacts
for (contact in smsContacts) {

if (contact.isEnabled && contact.phoneNumber != "") {

// add one for the alert message
messageCount++

// add one for the location message
if (contact.includeLocation) {
messageCount++
}

// add one for the warning message
if (includeWarning) {
messageCount++
}
}
}
return messageCount
}

// check to see if any data was passed to the activity
private fun checkExtras(extras: Bundle?) {

Expand Down
106 changes: 57 additions & 49 deletions app/src/main/res/layout/dialog_test_alert_confirmation.xml
Original file line number Diff line number Diff line change
@@ -1,67 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">

<TextView
android:id="@+id/testConfirmationDialogMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/default_text_size"
android:padding="8dp"
/>
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="8dp" >
android:orientation="vertical"
android:padding="8dp">

<TextView
android:layout_width="0dp"
android:id="@+id/testConfirmationDialogMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/default_text_size"
android:text="@string/test_alert_switch_include_warning_text"/>
android:padding="8dp"
/>

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchSendWarning"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"/>
</LinearLayout>
<LinearLayout
android:id="@+id/warningMessageLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:textSize="@dimen/default_text_size"
android:visibility="visible">
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="8dp" >

<EditText
android:id="@+id/editTextWarningMessage"
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="@dimen/default_text_size"
android:textAlignment="textEnd"
android:text="@string/test_alert_switch_include_warning_text"/>

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchSendWarning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:checked="false"/>
</LinearLayout>
<LinearLayout
android:id="@+id/warningMessageLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/test_alert_sms_default_message"
android:autofillHints="none"
android:inputType="textMultiLine"
android:orientation="vertical"
android:padding="8dp"
android:textSize="@dimen/default_text_size"
android:minLines="2"
android:maxLines="4"/>
</LinearLayout>
android:visibility="visible">

<TextView
android:id="@+id/testProceedConfirmationDialogMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/default_text_size"
android:padding="8dp"
android:layout_marginTop="8dp"
android:textAlignment="textStart"
/>
<EditText
android:id="@+id/editTextWarningMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/test_alert_sms_default_message"
android:autofillHints="none"
android:inputType="textMultiLine"
android:textSize="@dimen/default_text_size"
android:minLines="2"
android:maxLines="4"/>
</LinearLayout>

<TextView
android:id="@+id/testProceedConfirmationDialogMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/default_text_size"
android:padding="8dp"
android:layout_marginTop="8dp"
android:textAlignment="textEnd"
/>

</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

0 comments on commit ca8df3f

Please sign in to comment.