Favorites?
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.example.ichwillheim
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
@@ -25,6 +28,7 @@ import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.example.ichwillheim.ui.theme.IchWillHeimTheme
|
||||
import androidx.core.net.toUri
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@@ -43,14 +47,21 @@ class MainActivity : ComponentActivity() {
|
||||
@Composable
|
||||
fun LauncherScreen(modifier: Modifier = Modifier) {
|
||||
val context = LocalContext.current
|
||||
val favorites = listOf(
|
||||
FavoriteContact(
|
||||
name = "Sandro",
|
||||
phone = "+1-555-0100"
|
||||
),
|
||||
FavoriteContact(
|
||||
name = "Paulo",
|
||||
phone = "+1-555-0111"
|
||||
)
|
||||
)
|
||||
|
||||
val apps = listOf(
|
||||
AppEntry(
|
||||
label = "Phone",
|
||||
packageName = "com.android.dialer"
|
||||
),
|
||||
AppEntry(
|
||||
label = "Messages",
|
||||
packageName = "com.google.android.apps.messaging"
|
||||
action = { openMessages(context) }
|
||||
)
|
||||
)
|
||||
|
||||
@@ -61,14 +72,25 @@ fun LauncherScreen(modifier: Modifier = Modifier) {
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text(
|
||||
text = "Hallo Renate!",
|
||||
style = MaterialTheme.typography.headlineSmall
|
||||
)
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
Text(text = "Favorites", style = MaterialTheme.typography.headlineSmall)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
favorites.forEach { favorite ->
|
||||
Button(
|
||||
onClick = { openDialerNumber(context, favorite.phone) },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(96.dp)
|
||||
) {
|
||||
Text(text = favorite.name, fontSize = 24.sp)
|
||||
}
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
}
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Text(text = "Apps", style = MaterialTheme.typography.headlineSmall)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
apps.forEach { app ->
|
||||
Button(
|
||||
onClick = { launchApp(context, app) },
|
||||
onClick = { app.action() },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(96.dp)
|
||||
@@ -91,18 +113,33 @@ fun LauncherPreview() {
|
||||
@Immutable
|
||||
data class AppEntry(
|
||||
val label: String,
|
||||
val packageName: String
|
||||
val action: () -> Unit
|
||||
)
|
||||
|
||||
private fun launchApp(context: android.content.Context, app: AppEntry) {
|
||||
val intent = context.packageManager.getLaunchIntentForPackage(app.packageName)
|
||||
if (intent != null) {
|
||||
@Immutable
|
||||
data class FavoriteContact(
|
||||
val name: String,
|
||||
val phone: String
|
||||
)
|
||||
|
||||
private fun openDialerNumber(context: Context, phone: String) {
|
||||
val intent = Intent(Intent.ACTION_DIAL).apply {
|
||||
data = "tel:${Uri.encode(phone)}".toUri()
|
||||
}
|
||||
startIntentOrToast(context, intent, "Phone app not found")
|
||||
}
|
||||
|
||||
private fun openMessages(context: Context) {
|
||||
val intent = Intent(Intent.ACTION_SENDTO).apply {
|
||||
data = "smsto:".toUri()
|
||||
}
|
||||
startIntentOrToast(context, intent, "Messages app not found")
|
||||
}
|
||||
|
||||
private fun startIntentOrToast(context: Context, intent: Intent, error: String) {
|
||||
try {
|
||||
context.startActivity(intent)
|
||||
} else {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"App not found: ${app.label}",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
} catch (_: Exception) {
|
||||
Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user