Compare commits
No commits in common. "8a0bef9d0dcb71d706317ba74807eafa8f2cea9a" and "9807c900dd68e986cfedb48c28e0ab43696fda89" have entirely different histories.
8a0bef9d0d
...
9807c900dd
@ -42,26 +42,17 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// OkHttp untuk HTTP request
|
|
||||||
implementation("com.squareup.okhttp3:okhttp:4.11.0")
|
implementation("com.squareup.okhttp3:okhttp:4.11.0")
|
||||||
|
implementation("androidx.compose.material3:material3:1.1.1")
|
||||||
// Coroutines untuk manajemen thread, penting untuk OkHttp
|
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
|
|
||||||
|
|
||||||
// Core AndroidX dan Compose BOM
|
|
||||||
implementation(libs.androidx.core.ktx)
|
implementation(libs.androidx.core.ktx)
|
||||||
implementation(libs.androidx.lifecycle.runtime.ktx)
|
implementation(libs.androidx.lifecycle.runtime.ktx)
|
||||||
implementation(libs.androidx.activity.compose)
|
implementation(libs.androidx.activity.compose)
|
||||||
implementation(platform(libs.androidx.compose.bom))
|
implementation(platform(libs.androidx.compose.bom))
|
||||||
|
|
||||||
// Compose UI & Material3
|
|
||||||
implementation(libs.androidx.compose.ui)
|
implementation(libs.androidx.compose.ui)
|
||||||
implementation(libs.androidx.compose.ui.graphics)
|
implementation(libs.androidx.compose.ui.graphics)
|
||||||
implementation(libs.androidx.compose.material3) // Diambil dari BOM
|
|
||||||
implementation(libs.androidx.compose.foundation)
|
|
||||||
|
|
||||||
// Tools dan Testing
|
|
||||||
implementation(libs.androidx.compose.ui.tooling.preview)
|
implementation(libs.androidx.compose.ui.tooling.preview)
|
||||||
|
implementation(libs.androidx.compose.material3)
|
||||||
|
implementation(libs.androidx.compose.foundation)
|
||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
androidTestImplementation(libs.androidx.junit)
|
androidTestImplementation(libs.androidx.junit)
|
||||||
androidTestImplementation(libs.androidx.espresso.core)
|
androidTestImplementation(libs.androidx.espresso.core)
|
||||||
@ -70,4 +61,7 @@ dependencies {
|
|||||||
debugImplementation(libs.androidx.compose.ui.tooling)
|
debugImplementation(libs.androidx.compose.ui.tooling)
|
||||||
debugImplementation(libs.androidx.compose.ui.test.manifest)
|
debugImplementation(libs.androidx.compose.ui.test.manifest)
|
||||||
|
|
||||||
|
implementation("com.squareup.okhttp3:okhttp:4.11.0")
|
||||||
|
implementation("androidx.compose.material3:material3:1.1.1")
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,9 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
@ -12,9 +14,7 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.PanicButton"
|
android:theme="@style/Theme.PanicButton">
|
||||||
android:usesCleartextTraffic="true"
|
|
||||||
>
|
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name="id.ac.ubharajaya.panicbutton.MainActivity"
|
android:name="id.ac.ubharajaya.panicbutton.MainActivity"
|
||||||
@ -24,16 +24,13 @@
|
|||||||
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity android:name=".JalurEvakuasiActivity"
|
<activity android:name=".JalurEvakuasiActivity" />
|
||||||
android:exported="false" />
|
<activity android:name=".AlertActivity" />
|
||||||
|
|
||||||
<activity android:name=".AlertActivity"
|
|
||||||
android:exported="false" />
|
|
||||||
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
@ -5,7 +5,6 @@ import android.content.Intent
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.compose.BackHandler // ⬅️ Tambahkan impor ini
|
|
||||||
import androidx.compose.foundation.border
|
import androidx.compose.foundation.border
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.*
|
import androidx.compose.foundation.layout.*
|
||||||
@ -28,47 +27,6 @@ import okhttp3.MediaType.Companion.toMediaType
|
|||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.RequestBody
|
import okhttp3.RequestBody
|
||||||
import androidx.compose.material.icons.Icons
|
|
||||||
import androidx.compose.material.icons.filled.Settings
|
|
||||||
import androidx.compose.material.icons.filled.ArrowBack
|
|
||||||
import androidx.compose.ui.text.TextStyle
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// 1. CONSTANTS & HELPER FUNCTIONS FOR SHAREDPREFERENCES
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
const val PREFS_NAME = "AppPrefs"
|
|
||||||
const val KEY_NTFY_SERVER = "ntfyServer"
|
|
||||||
const val KEY_NTFY_TOPIC = "ntfyTopic"
|
|
||||||
|
|
||||||
const val DEFAULT_NTFY_SERVER = "https://ntfy.ubharajaya.ac.id"
|
|
||||||
const val DEFAULT_NTFY_TOPIC = "panic-button"
|
|
||||||
|
|
||||||
|
|
||||||
fun getPrefs(context: Context) = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
|
||||||
|
|
||||||
fun saveNtfyConfig(context: Context, server: String, topic: String) {
|
|
||||||
getPrefs(context).edit().apply {
|
|
||||||
putString(KEY_NTFY_SERVER, server)
|
|
||||||
putString(KEY_NTFY_TOPIC, topic)
|
|
||||||
apply()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getNtfyUrl(context: Context): String {
|
|
||||||
val prefs = getPrefs(context)
|
|
||||||
val server = prefs.getString(KEY_NTFY_SERVER, DEFAULT_NTFY_SERVER)
|
|
||||||
val topic = prefs.getString(KEY_NTFY_TOPIC, DEFAULT_NTFY_TOPIC)
|
|
||||||
|
|
||||||
val cleanServer = server?.trimEnd('/') ?: DEFAULT_NTFY_SERVER.trimEnd('/')
|
|
||||||
val cleanTopic = topic?.trimStart('/') ?: DEFAULT_NTFY_TOPIC.trimStart('/')
|
|
||||||
|
|
||||||
return "$cleanServer/$cleanTopic"
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// 2. MAIN ACTIVITY CLASS
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
@ -79,96 +37,133 @@ class MainActivity : ComponentActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// 3. MY APP (NAVIGATOR)
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
sealed class ScreenState {
|
|
||||||
object Main : ScreenState()
|
|
||||||
object Settings : ScreenState()
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
@Composable
|
||||||
fun MyApp() {
|
fun MyApp() {
|
||||||
var currentScreen by remember { mutableStateOf<ScreenState>(ScreenState.Main) }
|
|
||||||
val context = LocalContext.current
|
|
||||||
|
|
||||||
Scaffold(
|
|
||||||
topBar = {
|
|
||||||
TopAppBar(
|
|
||||||
title = { Text("Panic Button") },
|
|
||||||
// Logika TopBar hanya muncul jika di MainScreen,
|
|
||||||
// SettingsScreen memiliki TopBar sendiri
|
|
||||||
actions = {
|
|
||||||
if (currentScreen is ScreenState.Main) {
|
|
||||||
IconButton(onClick = {
|
|
||||||
currentScreen = ScreenState.Settings
|
|
||||||
}) {
|
|
||||||
Icon(Icons.Filled.Settings, contentDescription = "Settings")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
) { paddingValues ->
|
|
||||||
when (currentScreen) {
|
|
||||||
is ScreenState.Main -> MainScreen(
|
|
||||||
paddingValues = paddingValues,
|
|
||||||
onSendNotification = { conditions, report, onResult ->
|
|
||||||
sendNotification(context, conditions, report, onResult)
|
|
||||||
},
|
|
||||||
onNavigateEvakuasi = {
|
|
||||||
val intent = Intent(context, JalurEvakuasiActivity::class.java)
|
|
||||||
context.startActivity(intent)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
is ScreenState.Settings -> SettingsScreen(
|
|
||||||
// Melewatkan fungsi untuk kembali
|
|
||||||
onBack = { currentScreen = ScreenState.Main }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// 4. MAIN SCREEN
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun MainScreen(
|
|
||||||
paddingValues: PaddingValues,
|
|
||||||
onSendNotification: (String, String, (String) -> Unit) -> Unit,
|
|
||||||
onNavigateEvakuasi: () -> Unit
|
|
||||||
) {
|
|
||||||
val focusManager = LocalFocusManager.current
|
val focusManager = LocalFocusManager.current
|
||||||
|
val context = LocalContext.current
|
||||||
|
val sharedPreferences = context.getSharedPreferences("PanicButtonPrefs", Context.MODE_PRIVATE)
|
||||||
|
|
||||||
var message by remember { mutableStateOf("Klik tombol untuk mengirim notifikasi") }
|
var message by remember { mutableStateOf("Klik tombol untuk mengirim notifikasi") }
|
||||||
var selectedConditions by remember { mutableStateOf(mutableSetOf<String>()) }
|
var selectedConditions by remember { mutableStateOf(mutableSetOf<String>()) }
|
||||||
var additionalNotes by remember { mutableStateOf(TextFieldValue("")) }
|
var additionalNotes by remember { mutableStateOf(TextFieldValue("")) }
|
||||||
|
var showDialog by remember { mutableStateOf(false) }
|
||||||
|
var dialogMessage by remember { mutableStateOf("") }
|
||||||
|
var showSettingsDialog by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
// Load saved configuration
|
||||||
|
var ntfyServer by remember {
|
||||||
|
mutableStateOf(sharedPreferences.getString("ntfyServer", "https://ntfy.sh") ?: "https://ntfy.sh")
|
||||||
|
}
|
||||||
|
var ntfyTopic by remember {
|
||||||
|
mutableStateOf(sharedPreferences.getString("ntfyTopic", "ubahara-panic-button") ?: "ubahara-panic-button")
|
||||||
|
}
|
||||||
|
|
||||||
val scrollState = rememberScrollState()
|
val scrollState = rememberScrollState()
|
||||||
|
|
||||||
|
// Dialog konfirmasi
|
||||||
|
if (showDialog) {
|
||||||
|
AlertDialog(
|
||||||
|
onDismissRequest = { showDialog = false },
|
||||||
|
title = { Text("Konfirmasi") },
|
||||||
|
text = { Text(dialogMessage) },
|
||||||
|
confirmButton = {
|
||||||
|
Button(onClick = { showDialog = false }) {
|
||||||
|
Text("OK")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dialog Settings
|
||||||
|
if (showSettingsDialog) {
|
||||||
|
var tempServer by remember { mutableStateOf(ntfyServer) }
|
||||||
|
var tempTopic by remember { mutableStateOf(ntfyTopic) }
|
||||||
|
|
||||||
|
AlertDialog(
|
||||||
|
onDismissRequest = { showSettingsDialog = false },
|
||||||
|
title = { Text("Pengaturan NTFY") },
|
||||||
|
text = {
|
||||||
|
Column {
|
||||||
|
Text("Server NTFY:")
|
||||||
|
OutlinedTextField(
|
||||||
|
value = tempServer,
|
||||||
|
onValueChange = { tempServer = it },
|
||||||
|
modifier = Modifier.fillMaxWidth()
|
||||||
|
)
|
||||||
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
|
Text("Topic:")
|
||||||
|
OutlinedTextField(
|
||||||
|
value = tempTopic,
|
||||||
|
onValueChange = { tempTopic = it },
|
||||||
|
modifier = Modifier.fillMaxWidth()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
confirmButton = {
|
||||||
|
Button(onClick = {
|
||||||
|
ntfyServer = tempServer
|
||||||
|
ntfyTopic = tempTopic
|
||||||
|
// Save to SharedPreferences
|
||||||
|
with(sharedPreferences.edit()) {
|
||||||
|
putString("ntfyServer", tempServer)
|
||||||
|
putString("ntfyTopic", tempTopic)
|
||||||
|
// Gabungkan untuk kompatibilitas dengan AlertActivity
|
||||||
|
putString("urlKey", "$tempServer/$tempTopic")
|
||||||
|
apply()
|
||||||
|
}
|
||||||
|
showSettingsDialog = false
|
||||||
|
message = "Pengaturan berhasil disimpan"
|
||||||
|
}) {
|
||||||
|
Text("Simpan")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
dismissButton = {
|
||||||
|
TextButton(onClick = { showSettingsDialog = false }) {
|
||||||
|
Text("Batal")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.verticalScroll(scrollState)
|
.verticalScroll(scrollState)
|
||||||
.padding(paddingValues)
|
|
||||||
.padding(16.dp),
|
.padding(16.dp),
|
||||||
verticalArrangement = Arrangement.Top,
|
verticalArrangement = Arrangement.Top,
|
||||||
horizontalAlignment = Alignment.Start
|
horizontalAlignment = Alignment.Start
|
||||||
) {
|
) {
|
||||||
|
// Header dengan tombol settings
|
||||||
|
Row(
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
horizontalArrangement = Arrangement.SpaceBetween,
|
||||||
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = "Terjadi Kondisi Darurat",
|
||||||
|
fontSize = 20.sp,
|
||||||
|
color = Color.Red
|
||||||
|
)
|
||||||
|
Button(
|
||||||
|
onClick = { showSettingsDialog = true },
|
||||||
|
colors = ButtonDefaults.buttonColors(containerColor = Color.Gray)
|
||||||
|
) {
|
||||||
|
Text("⚙️", fontSize = 18.sp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
Text(
|
Text(
|
||||||
text = "Terjadi Kondisi Darurat",
|
text = "Server: $ntfyServer\nTopic: $ntfyTopic",
|
||||||
fontSize = 20.sp,
|
fontSize = 12.sp,
|
||||||
color = Color.Red,
|
color = Color.Gray,
|
||||||
modifier = Modifier.padding(bottom = 16.dp)
|
modifier = Modifier.padding(bottom = 16.dp)
|
||||||
)
|
)
|
||||||
|
|
||||||
listOf(
|
listOf(
|
||||||
"\uD83D\uDD25 Kebakaran", "⛈\uFE0F Banjir", "\uD83C\uDF0A Tsunami", "\uD83C\uDF0B Gunung Meletus",
|
"🔥 Kebakaran", "⛈️ Banjir", "🌊 Tsunami", "🌋 Gunung Meletus",
|
||||||
"\uD83C\uDF0F Gempa Bumi", "\uD83D\uDC7F Huru hara", "\uD83D\uDC0D Binatang Buas",
|
"🌏 Gempa Bumi", "👿 Huru hara", "🐍 Binatang Buas",
|
||||||
"☢\uFE0F Radiasi Nuklir", "☣\uFE0F Biohazard"
|
"☢️ Radiasi Nuklir", "☣️ Biohazard"
|
||||||
).forEach { condition ->
|
).forEach { condition ->
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -217,18 +212,23 @@ fun MainScreen(
|
|||||||
val notes = additionalNotes.text
|
val notes = additionalNotes.text
|
||||||
val conditions = selectedConditions.joinToString(", ")
|
val conditions = selectedConditions.joinToString(", ")
|
||||||
val report = "Kondisi: $conditions\nCatatan: $notes"
|
val report = "Kondisi: $conditions\nCatatan: $notes"
|
||||||
onSendNotification(conditions, report) { response ->
|
val fullUrl = "$ntfyServer/$ntfyTopic"
|
||||||
|
|
||||||
|
sendNotification(fullUrl, conditions, report) { response ->
|
||||||
message = response
|
message = response
|
||||||
|
dialogMessage = response
|
||||||
|
showDialog = true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
colors = ButtonDefaults.buttonColors(containerColor = Color.Red)
|
colors = ButtonDefaults.buttonColors(containerColor = Color.Red),
|
||||||
|
enabled = selectedConditions.isNotEmpty()
|
||||||
) {
|
) {
|
||||||
Text(text = "Kirim Laporan", color = Color.White)
|
Text(text = "Kirim Laporan", color = Color.White)
|
||||||
}
|
}
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
Text(
|
Text(
|
||||||
text = "“JANGAN PANIK! SEGERA EVAKUASI\nDIRI ANDA KE TITIK KUMPUL”",
|
text = "JANGAN PANIK! SEGERA EVAKUASI\nDIRI ANDA KE TITIK KUMPUL",
|
||||||
color = Color.Red,
|
color = Color.Red,
|
||||||
fontSize = 15.sp
|
fontSize = 15.sp
|
||||||
)
|
)
|
||||||
@ -236,7 +236,10 @@ fun MainScreen(
|
|||||||
Spacer(modifier = Modifier.height(16.dp))
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
Text(text = message, Modifier.padding(top = 16.dp))
|
Text(text = message, Modifier.padding(top = 16.dp))
|
||||||
Button(
|
Button(
|
||||||
onClick = onNavigateEvakuasi,
|
onClick = {
|
||||||
|
val intent = Intent(context, JalurEvakuasiActivity::class.java)
|
||||||
|
context.startActivity(intent)
|
||||||
|
},
|
||||||
colors = ButtonDefaults.buttonColors(containerColor = Color.Green)
|
colors = ButtonDefaults.buttonColors(containerColor = Color.Green)
|
||||||
) {
|
) {
|
||||||
Text(text = "Lihat Jalur Evakuasi", color = Color.White)
|
Text(text = "Lihat Jalur Evakuasi", color = Color.White)
|
||||||
@ -244,122 +247,19 @@ fun MainScreen(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun sendNotification(url: String, condition: String, report: String, onResult: (String) -> Unit) {
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// 5. SETTINGS SCREEN (DIPERBAIKI)
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
|
||||||
@Composable
|
|
||||||
fun SettingsScreen(onBack: () -> Unit) {
|
|
||||||
val context = LocalContext.current
|
|
||||||
val prefs = remember { getPrefs(context) }
|
|
||||||
|
|
||||||
// Inisialisasi state dari SharedPreferences
|
|
||||||
var serverText by remember {
|
|
||||||
mutableStateOf(TextFieldValue(prefs.getString(KEY_NTFY_SERVER, DEFAULT_NTFY_SERVER) ?: DEFAULT_NTFY_SERVER))
|
|
||||||
}
|
|
||||||
var topicText by remember {
|
|
||||||
mutableStateOf(TextFieldValue(prefs.getString(KEY_NTFY_TOPIC, DEFAULT_NTFY_TOPIC) ?: DEFAULT_NTFY_TOPIC))
|
|
||||||
}
|
|
||||||
|
|
||||||
var saveMessage by remember { mutableStateOf("") }
|
|
||||||
|
|
||||||
// ⬅️ TAMBAHAN: Menangani tombol kembali pada perangkat
|
|
||||||
BackHandler(onBack = onBack)
|
|
||||||
|
|
||||||
Scaffold(
|
|
||||||
topBar = {
|
|
||||||
TopAppBar(
|
|
||||||
title = { Text("Pengaturan NTFY Endpoint") },
|
|
||||||
navigationIcon = {
|
|
||||||
IconButton(onClick = onBack) { // ⬅️ onBack menangani navigasi kembali ke MainScreen
|
|
||||||
Icon(Icons.Filled.ArrowBack, contentDescription = "Kembali")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
) { innerPadding ->
|
|
||||||
Column(
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxSize()
|
|
||||||
.padding(innerPadding)
|
|
||||||
.padding(16.dp)
|
|
||||||
.verticalScroll(rememberScrollState()),
|
|
||||||
horizontalAlignment = Alignment.Start
|
|
||||||
) {
|
|
||||||
Text(
|
|
||||||
text = "Konfigurasi Server Notifikasi",
|
|
||||||
fontSize = 18.sp,
|
|
||||||
modifier = Modifier.padding(bottom = 16.dp)
|
|
||||||
)
|
|
||||||
|
|
||||||
OutlinedTextField(
|
|
||||||
value = serverText,
|
|
||||||
onValueChange = { serverText = it },
|
|
||||||
label = { Text("Server NTFY (contoh: https://ntfy.sh)") },
|
|
||||||
modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp),
|
|
||||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
|
|
||||||
singleLine = true
|
|
||||||
)
|
|
||||||
|
|
||||||
OutlinedTextField(
|
|
||||||
value = topicText,
|
|
||||||
onValueChange = { topicText = it },
|
|
||||||
label = { Text("Topic (contoh: panic-button)") },
|
|
||||||
modifier = Modifier.fillMaxWidth().padding(bottom = 16.dp),
|
|
||||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
|
|
||||||
singleLine = true
|
|
||||||
)
|
|
||||||
|
|
||||||
Button(
|
|
||||||
onClick = {
|
|
||||||
saveNtfyConfig(
|
|
||||||
context,
|
|
||||||
serverText.text.trim(),
|
|
||||||
topicText.text.trim()
|
|
||||||
)
|
|
||||||
saveMessage = "Konfigurasi berhasil disimpan! URL: ${getNtfyUrl(context)}"
|
|
||||||
},
|
|
||||||
modifier = Modifier.fillMaxWidth()
|
|
||||||
) {
|
|
||||||
Text("Simpan Konfigurasi")
|
|
||||||
}
|
|
||||||
|
|
||||||
Text(
|
|
||||||
text = saveMessage,
|
|
||||||
color = if (saveMessage.contains("berhasil")) Color.Green else Color.Black,
|
|
||||||
modifier = Modifier.padding(top = 16.dp)
|
|
||||||
)
|
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
|
||||||
Text(
|
|
||||||
text = "URL saat ini: ${getNtfyUrl(context)}",
|
|
||||||
style = TextStyle(color = Color.Gray, fontSize = 12.sp)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
// 6. SEND NOTIFICATION FUNCTION
|
|
||||||
// ----------------------------------------------------------------------
|
|
||||||
|
|
||||||
fun sendNotification(context: Context, condition: String, report: String, onResult: (String) -> Unit) {
|
|
||||||
val client = OkHttpClient()
|
val client = OkHttpClient()
|
||||||
val url = getNtfyUrl(context)
|
|
||||||
|
|
||||||
val tagMapping = mapOf(
|
val tagMapping = mapOf(
|
||||||
"\uD83D\uDD25 Kebakaran" to "fire",
|
"🔥 Kebakaran" to "fire",
|
||||||
"⛈\uFE0F Banjir" to "cloud_with_lightning_and_rain",
|
"⛈️ Banjir" to "cloud_with_lightning_and_rain",
|
||||||
"\uD83C\uDF0A Tsunami" to "ocean",
|
"🌊 Tsunami" to "ocean",
|
||||||
"\uD83C\uDF0B Gunung Meletus" to "volcano",
|
"🌋 Gunung Meletus" to "volcano",
|
||||||
"\uD83C\uDF0F Gempa Bumi" to "earth_asia",
|
"🌏 Gempa Bumi" to "earth_asia",
|
||||||
"\uD83D\uDC7F Huru hara" to "imp",
|
"👿 Huru hara" to "imp",
|
||||||
"\uD83D\uDC0D Binatang Buas" to "snake",
|
"🐍 Binatang Buas" to "snake",
|
||||||
"☢\uFE0F Radiasi Nuklir" to "radioactive",
|
"☢️ Radiasi Nuklir" to "radioactive",
|
||||||
"☣\uFE0F Biohazard" to "biohazard"
|
"☣️ Biohazard" to "biohazard"
|
||||||
)
|
)
|
||||||
|
|
||||||
val selectedList = condition
|
val selectedList = condition
|
||||||
@ -369,8 +269,7 @@ fun sendNotification(context: Context, condition: String, report: String, onResu
|
|||||||
|
|
||||||
val cleanConditionText = selectedList.joinToString(", ")
|
val cleanConditionText = selectedList.joinToString(", ")
|
||||||
val emojiTags = selectedList.mapNotNull { tagMapping[it] }
|
val emojiTags = selectedList.mapNotNull { tagMapping[it] }
|
||||||
|
val finalTags = listOf("alert", "warning") + emojiTags
|
||||||
val finalTags = listOf("Alert") + emojiTags
|
|
||||||
val finalReport = "Kondisi: $cleanConditionText\nCatatan: ${report.substringAfter("Catatan:")}"
|
val finalReport = "Kondisi: $cleanConditionText\nCatatan: ${report.substringAfter("Catatan:")}"
|
||||||
|
|
||||||
val requestBody = RequestBody.create(
|
val requestBody = RequestBody.create(
|
||||||
@ -379,7 +278,7 @@ fun sendNotification(context: Context, condition: String, report: String, onResu
|
|||||||
)
|
)
|
||||||
val request = Request.Builder()
|
val request = Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.addHeader("Title", "Alert")
|
.addHeader("Title", "⚠️ ALERT - Kondisi Darurat")
|
||||||
.addHeader("Priority", "urgent")
|
.addHeader("Priority", "urgent")
|
||||||
.addHeader("Tags", finalTags.joinToString(","))
|
.addHeader("Tags", finalTags.joinToString(","))
|
||||||
.post(requestBody)
|
.post(requestBody)
|
||||||
@ -389,12 +288,12 @@ fun sendNotification(context: Context, condition: String, report: String, onResu
|
|||||||
try {
|
try {
|
||||||
val response = client.newCall(request).execute()
|
val response = client.newCall(request).execute()
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
onResult("Notifikasi berhasil dikirim ke $url!")
|
onResult("✅ Notifikasi berhasil dikirim!\nLaporan telah terkirim ke sistem.")
|
||||||
} else {
|
} else {
|
||||||
onResult("Gagal Mengirim notifikasi ke $url: ${response.code}")
|
onResult("❌ Gagal mengirim notifikasi\nKode error: ${response.code}")
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
onResult("Error: ${e.message}")
|
onResult("❌ Error: ${e.message}")
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user