-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor admin contact into reusable component
- Loading branch information
Showing
12 changed files
with
298 additions
and
238 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
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
112 changes: 112 additions & 0 deletions
112
mage/src/main/java/mil/nga/giat/mage/ui/setup/AccountStateScreen.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,112 @@ | ||
package mil.nga.giat.mage.ui.setup | ||
|
||
import android.content.Context | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.LocalContentColor | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import mil.nga.giat.mage.R | ||
|
||
sealed class AccountState( | ||
val title: String, | ||
val message: String | ||
) { | ||
|
||
class Inactive(context: Context): AccountState( | ||
title = context.getString(R.string.account_inactive_title), | ||
message = context.getString(R.string.account_inactive_message) | ||
) | ||
|
||
class Disabled(context: Context): AccountState( | ||
title = context.getString(R.string.account_disabled_title), | ||
message = context.getString(R.string.account_disabled_message) | ||
) | ||
|
||
class Unknown(context: Context): AccountState( | ||
title = context.getString(R.string.account_unknown_title), | ||
message = context.getString(R.string.account_unknown_message) | ||
) | ||
} | ||
|
||
@Composable | ||
fun AccountStateScreen( | ||
accountState: AccountState, | ||
onDone: () -> Unit, | ||
viewModel: AccountStateViewModel = hiltViewModel() | ||
) { | ||
val contact = viewModel.contact | ||
|
||
Column( | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(horizontal = 16.dp) | ||
|
||
) { | ||
|
||
Column( | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
modifier = Modifier.weight(1f) | ||
) { | ||
Icon( | ||
painter = painterResource(R.drawable.ic_wand_white_50dp), | ||
contentDescription = "wand", | ||
tint = MaterialTheme.colorScheme.tertiary, | ||
modifier = Modifier | ||
.padding(bottom = 16.dp) | ||
.size(72.dp) | ||
) | ||
|
||
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colorScheme.onSurface) { | ||
Text( | ||
text = accountState.title, | ||
style = MaterialTheme.typography.displaySmall, | ||
modifier = Modifier.padding(bottom = 4.dp) | ||
) | ||
} | ||
} | ||
|
||
Column( | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
modifier = Modifier.weight(2f) | ||
) { | ||
|
||
AdminContact( | ||
text = accountState.message, | ||
contact = contact, | ||
style = MaterialTheme.typography.bodyLarge.copy( | ||
color = LocalContentColor.current.copy(alpha = .87f) | ||
).toSpanStyle(), | ||
emailState = EmailState( | ||
subject = "MAGE Account", | ||
body = accountState.message | ||
) | ||
) | ||
|
||
Button( | ||
onClick = { onDone() }, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(top = 48.dp) | ||
) { | ||
Text("OK") | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
mage/src/main/java/mil/nga/giat/mage/ui/setup/AccountStateViewModel.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,10 @@ | ||
package mil.nga.giat.mage.ui.setup | ||
|
||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class AccountStateViewModel @Inject constructor( | ||
val contact: AdminContact | ||
): ViewModel() |
132 changes: 132 additions & 0 deletions
132
mage/src/main/java/mil/nga/giat/mage/ui/setup/AdminContact.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,132 @@ | ||
package mil.nga.giat.mage.ui.setup | ||
|
||
import android.content.Intent | ||
import android.content.SharedPreferences | ||
import android.net.Uri | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.text.ClickableText | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.SecurityUpdateWarning | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.LocalContentColor | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.text.SpanStyle | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.buildAnnotatedString | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.text.withStyle | ||
import androidx.compose.ui.unit.dp | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
data class AdminContact @Inject constructor( | ||
val preferences: SharedPreferences | ||
) { | ||
val email = preferences.getString(ADMIN_EMAIL_PREFERENCE_KEY, null) | ||
val phone = preferences.getString(ADMIN_PHONE_PREFERENCE_KEY, null) | ||
|
||
companion object { | ||
const val ADMIN_EMAIL_PREFERENCE_KEY = "gContactinfoEmail" | ||
const val ADMIN_PHONE_PREFERENCE_KEY = "gContactinfoPhone" | ||
} | ||
} | ||
|
||
data class EmailState( | ||
val subject: String, | ||
val body: String | ||
) | ||
|
||
@Composable | ||
fun AdminContact( | ||
text: String, | ||
contact: AdminContact, | ||
style: SpanStyle, | ||
emailState: EmailState | ||
) { | ||
val context = LocalContext.current | ||
|
||
Column( | ||
Modifier.padding(vertical = 16.dp) | ||
) { | ||
val split = text.split("administrator") | ||
val annotatedString = buildAnnotatedString { | ||
withStyle(style) { | ||
append("${split.first()}administrator") | ||
} | ||
|
||
if (contact.phone != null || contact.email != null) { | ||
withStyle(style) { append(" at ") } | ||
} | ||
|
||
if (contact.phone != null) { | ||
pushStringAnnotation( | ||
tag = "phone", | ||
annotation = contact.phone | ||
) | ||
withStyle( | ||
style = MaterialTheme.typography.bodyLarge.copy( | ||
color = MaterialTheme.colorScheme.tertiary | ||
).toSpanStyle() | ||
) { | ||
append(contact.phone) | ||
} | ||
pop() | ||
} | ||
|
||
if (contact.phone != null && contact.email != null) { | ||
withStyle(style) { append(" or ") } | ||
} | ||
|
||
if (contact.email != null) { | ||
pushStringAnnotation( | ||
tag = "email", | ||
annotation = contact.email | ||
) | ||
withStyle( | ||
style = MaterialTheme.typography.bodyLarge.copy( | ||
color = MaterialTheme.colorScheme.tertiary | ||
).toSpanStyle() | ||
) { | ||
append(contact.email) | ||
} | ||
pop() | ||
} | ||
|
||
withStyle(style) { append(split.last()) } | ||
} | ||
|
||
ClickableText( | ||
text = annotatedString, | ||
style = TextStyle(textAlign = TextAlign.Center), | ||
onClick = { offset -> | ||
annotatedString.getStringAnnotations( | ||
tag = "email", start = offset, end = offset | ||
).firstOrNull()?.let { | ||
val uri = Uri.Builder() | ||
.scheme("mailto") | ||
.opaquePart(contact.email) | ||
.appendQueryParameter("subject", emailState.subject) | ||
.appendQueryParameter("body", emailState.body) | ||
.build() | ||
|
||
val intent = Intent(Intent.ACTION_SENDTO, uri) | ||
context.startActivity(intent) | ||
} | ||
|
||
annotatedString.getStringAnnotations( | ||
tag = "phone", start = offset, end = offset | ||
).firstOrNull()?.let { | ||
val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:${contact.phone}")) | ||
context.startActivity(intent) | ||
} | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.