From 0aabf94f1e20a536d4350d094e887ee1747d133f Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Mon, 1 Jul 2024 08:25:39 -0300 Subject: [PATCH] Add distributor selection to push notification server on free flavor --- .../service/PushNotificationUtils.kt | 35 ++++---- .../ui/SelectNotificationProvider.kt | 82 +++++++++++++++++++ .../nostrsigner/ui/SettingsScreen.kt | 17 +++- app/src/main/res/values/strings.xml | 15 ++++ .../ui/SelectNotificationProvider.kt | 6 ++ .../ui/SelectNotificationProvider.kt | 6 ++ 6 files changed, 144 insertions(+), 17 deletions(-) create mode 100644 app/src/free/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt create mode 100644 app/src/offline/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt create mode 100644 app/src/play/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt diff --git a/app/src/free/java/com/greenart7c3/nostrsigner/service/PushNotificationUtils.kt b/app/src/free/java/com/greenart7c3/nostrsigner/service/PushNotificationUtils.kt index 6b143476..0d1e1b5a 100644 --- a/app/src/free/java/com/greenart7c3/nostrsigner/service/PushNotificationUtils.kt +++ b/app/src/free/java/com/greenart7c3/nostrsigner/service/PushNotificationUtils.kt @@ -35,22 +35,25 @@ object PushNotificationUtils { return } - val distributions = pushHandler.getInstalledDistributors() - if (distributions.isNotEmpty()) { - distributions.firstOrNull { it.isNotBlank() }?.let { - pushHandler.saveDistributor(it) - } - } else { - accounts.forEach { - NostrSigner.instance.getDatabase(it.npub).applicationDao().insertLog( - LogEntity( - 0, - "Push server", - "Push server", - "No distributors found for push notifications", - System.currentTimeMillis(), - ), - ) + val savedDistributor = pushHandler.getSavedDistributor() + if (savedDistributor.isBlank()) { + val distributions = pushHandler.getInstalledDistributors() + if (distributions.isNotEmpty()) { + distributions.firstOrNull { it.isNotBlank() }?.let { + pushHandler.saveDistributor(it) + } + } else { + accounts.forEach { + NostrSigner.instance.getDatabase(it.npub).applicationDao().insertLog( + LogEntity( + 0, + "Push server", + "Push server", + "No distributors found for push notifications", + System.currentTimeMillis(), + ), + ) + } } } try { diff --git a/app/src/free/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt b/app/src/free/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt new file mode 100644 index 00000000..012790e9 --- /dev/null +++ b/app/src/free/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt @@ -0,0 +1,82 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.greenart7c3.nostrsigner.ui + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import com.greenart7c3.nostrsigner.R +import com.greenart7c3.nostrsigner.service.PushDistributorHandler +import com.greenart7c3.nostrsigner.ui.components.TitleExplainer +import kotlinx.collections.immutable.ImmutableList +import kotlinx.collections.immutable.toImmutableList + +@Composable +fun LoadDistributors(onInner: @Composable (String, ImmutableList, ImmutableList) -> Unit) { + val currentDistributor = PushDistributorHandler.getSavedDistributor().ifBlank { null } ?: "None" + + val list = + remember { + PushDistributorHandler.getInstalledDistributors().plus("None").toImmutableList() + } + + val readableListWithExplainer = + PushDistributorHandler + .formattedDistributorNames() + .mapIndexed { index, name -> + TitleExplainer( + name, + stringResource(id = R.string.push_server_uses_app_explainer, list[index]), + ) + }.plus( + TitleExplainer( + stringResource(id = R.string.push_server_none), + stringResource(id = R.string.push_server_none_explainer), + ), + ) + .toImmutableList() + + onInner( + currentDistributor, + list, + readableListWithExplainer, + ) +} + +@Composable +fun PushNotificationSettingsRow() { + val context = LocalContext.current + LoadDistributors { currentDistributor, list, readableListWithExplainer -> + SettingsRow( + R.string.push_server_title, + R.string.push_server_explainer, + selectedItems = readableListWithExplainer, + selectedIndex = list.indexOf(currentDistributor), + ) { index -> + if (list[index] == "None") { + PushDistributorHandler.forceRemoveDistributor(context) + } else { + PushDistributorHandler.saveDistributor(list[index]) + } + } + } +} diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/ui/SettingsScreen.kt b/app/src/main/java/com/greenart7c3/nostrsigner/ui/SettingsScreen.kt index be5ac5e7..4a22c3f3 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/ui/SettingsScreen.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/ui/SettingsScreen.kt @@ -69,6 +69,7 @@ import com.greenart7c3.nostrsigner.models.Account import com.greenart7c3.nostrsigner.models.TimeUtils import com.greenart7c3.nostrsigner.service.ConnectivityService import com.greenart7c3.nostrsigner.service.NotificationDataSource +import com.greenart7c3.nostrsigner.service.PushNotificationUtils import com.greenart7c3.nostrsigner.ui.actions.AccountBackupDialog import com.greenart7c3.nostrsigner.ui.actions.AccountsBottomSheet import com.greenart7c3.nostrsigner.ui.actions.ConnectOrbotDialog @@ -323,7 +324,9 @@ fun SettingsScreen( notificationTypeDialog = false } - PostButton(isActive = true) { + PostButton( + isActive = true, + ) { notificationTypeDialog = false scope.launch(Dispatchers.IO) { LocalPreferences.updateNotificationType(parseNotificationType(notificationItemsIndex)) @@ -337,6 +340,8 @@ fun SettingsScreen( NotificationDataSource.stopSync() RelayPool.disconnect() } else { + PushNotificationUtils.hasInit = false + PushNotificationUtils.init(LocalPreferences.allSavedAccounts()) NostrSigner.instance.checkForNewRelays() NotificationDataSource.start() NostrSigner.instance.applicationContext.startService( @@ -364,6 +369,16 @@ fun SettingsScreen( notificationItemsIndex = it } } + + @Suppress("KotlinConstantConditions") + if (BuildConfig.FLAVOR == "free") { + Box( + Modifier + .padding(8.dp), + ) { + PushNotificationSettingsRow() + } + } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index afe927b3..047f02ed 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -225,4 +225,19 @@ Logs Clear logs Default relays + Distributors + Select the push notification distributor + Select a UnifiedPush App + Push Notification Setup + + To receive push notifications, install any app that supports [Unified Push](https://unifiedpush.org/), such as [Nfty](https://ntfy.sh/). + After installing, select the app you want to use in the Settings. + + Uses app %1$s + Push Notification + From installed UnifiedPush apps + Don\'t show again + OK + None + Disables Push Notifications diff --git a/app/src/offline/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt b/app/src/offline/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt new file mode 100644 index 00000000..76ff05b8 --- /dev/null +++ b/app/src/offline/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt @@ -0,0 +1,6 @@ +package com.greenart7c3.nostrsigner.ui + +import androidx.compose.runtime.Composable + +@Composable +fun PushNotificationSettingsRow() {} diff --git a/app/src/play/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt b/app/src/play/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt new file mode 100644 index 00000000..76ff05b8 --- /dev/null +++ b/app/src/play/java/com/greenart7c3/nostrsigner/ui/SelectNotificationProvider.kt @@ -0,0 +1,6 @@ +package com.greenart7c3.nostrsigner.ui + +import androidx.compose.runtime.Composable + +@Composable +fun PushNotificationSettingsRow() {}