Colota - Android GPS Location tracker
/**
* Copyright (C) 2026 Max Dietrich
* Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
*/
import { Alert } from "react-native"
import { t } from "../i18n/t"
export type AlertVariant = "info" | "error" | "warning" | "success"
export interface PromptInput {
placeholder?: string
initialValue?: string
multiline?: boolean
}
export interface ModalRequest {
title: string
message: string
variant: AlertVariant
input?: PromptInput
buttons: Array<{
text: string
style: "primary" | "secondary" | "destructive"
}>
resolve: (buttonIndex: number, value?: string) => void
}
type ModalHandler = (request: ModalRequest) => void
let _handler: ModalHandler | null = null
export function registerModalHandler(handler: ModalHandler) {
_handler = handler
}
export function showAlert(title: string, message: string, variant: AlertVariant = "info"): void {
if (!_handler) {
Alert.alert(title, message)
return
}
_handler({
title,
message,
variant,
buttons: [{ text: t("common.ok"), style: "primary" }],
resolve: () => {}
})
}
export function showChoice(options: {
title: string
message: string
variant?: AlertVariant
buttons: Array<{
text: string
style?: "primary" | "secondary" | "destructive"
}>
}): Promise<number> {
const { title, message, variant = "warning", buttons } = options
return new Promise((resolve) => {
if (!_handler) {
Alert.alert(
title,
message,
buttons.map((btn, i) => ({
text: btn.text,
style: btn.style === "destructive" ? "destructive" : btn.style === "secondary" ? "cancel" : "default",
onPress: () => resolve(i)
})),
{ cancelable: false }
)
return
}
_handler({
title,
message,
variant,
buttons: buttons.map((btn) => ({
text: btn.text,
style: btn.style ?? "secondary"
})),
resolve: (index) => resolve(index)
})
})
}
export function showConfirm(options: {
title: string
message: string
confirmText?: string
cancelText?: string
destructive?: boolean
}): Promise<boolean> {
const { title, message, confirmText = t("common.ok"), cancelText = t("common.cancel"), destructive = false } = options
return new Promise((resolve) => {
if (!_handler) {
Alert.alert(
title,
message,
[
{ text: cancelText, style: "cancel", onPress: () => resolve(false) },
{ text: confirmText, onPress: () => resolve(true) }
],
{ cancelable: false }
)
return
}
_handler({
title,
message,
variant: destructive ? "error" : "info",
buttons: [
{ text: cancelText, style: "secondary" },
{ text: confirmText, style: destructive ? "destructive" : "primary" }
],
resolve: (index) => resolve(index === 1)
})
})
}
export function showPrompt(options: {
title: string
message?: string
placeholder?: string
initialValue?: string
multiline?: boolean
confirmText?: string
cancelText?: string
}): Promise<string | null> {
const {
title,
message = "",
placeholder,
initialValue,
multiline,
confirmText = t("common.save"),
cancelText = t("common.cancel")
} = options
return new Promise((resolve) => {
if (!_handler) {
resolve(null)
return
}
_handler({
title,
message,
variant: "info",
input: { placeholder, initialValue, multiline },
buttons: [
{ text: cancelText, style: "secondary" },
{ text: confirmText, style: "primary" }
],
resolve: (index, value) => resolve(index === 1 ? (value ?? "").trim() : null)
})
})
}