mirror of
https://github.com/greenart7c3/Amber.git
synced 2026-10-06 03:18:23 +00:00
Add distributor selection to push notification server on free flavor
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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<String>, ImmutableList<TitleExplainer>) -> 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])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,4 +225,19 @@
|
||||
<string name="logs">Logs</string>
|
||||
<string name="clear_logs">Clear logs</string>
|
||||
<string name="default_relays">Default relays</string>
|
||||
<string name="distributors">Distributors</string>
|
||||
<string name="select_distributor">Select the push notification distributor</string>
|
||||
<string name="select_push_server">Select a UnifiedPush App</string>
|
||||
<string name="push_server_install_app">Push Notification Setup</string>
|
||||
<string name="push_server_install_app_description">
|
||||
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.
|
||||
</string>
|
||||
<string name="push_server_uses_app_explainer">Uses app %1$s</string>
|
||||
<string name="push_server_title">Push Notification</string>
|
||||
<string name="push_server_explainer">From installed UnifiedPush apps</string>
|
||||
<string name="quick_action_dont_show_again_button">Don\'t show again</string>
|
||||
<string name="error_dialog_button_ok">OK</string>
|
||||
<string name="push_server_none">None</string>
|
||||
<string name="push_server_none_explainer">Disables Push Notifications</string>
|
||||
</resources>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.greenart7c3.nostrsigner.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
@Composable
|
||||
fun PushNotificationSettingsRow() {}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.greenart7c3.nostrsigner.ui
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
@Composable
|
||||
fun PushNotificationSettingsRow() {}
|
||||
Reference in New Issue
Block a user