Skip to content

Commit

Permalink
Add distributor selection to push notification server on free flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jul 1, 2024
1 parent 4b8da28 commit 0aabf94
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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])
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -323,7 +324,9 @@ fun SettingsScreen(
notificationTypeDialog = false
}

PostButton(isActive = true) {
PostButton(
isActive = true,
) {
notificationTypeDialog = false
scope.launch(Dispatchers.IO) {
LocalPreferences.updateNotificationType(parseNotificationType(notificationItemsIndex))
Expand All @@ -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(
Expand Down Expand Up @@ -364,6 +369,16 @@ fun SettingsScreen(
notificationItemsIndex = it
}
}

@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "free") {
Box(
Modifier
.padding(8.dp),
) {
PushNotificationSettingsRow()
}
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.greenart7c3.nostrsigner.ui

import androidx.compose.runtime.Composable

@Composable
fun PushNotificationSettingsRow() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.greenart7c3.nostrsigner.ui

import androidx.compose.runtime.Composable

@Composable
fun PushNotificationSettingsRow() {}

0 comments on commit 0aabf94

Please sign in to comment.