-
Notifications
You must be signed in to change notification settings - Fork 226
Move push provider setting #2928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
300ca40
05b3337
c253da1
0ba595f
ea3a785
bd46321
3349df5
ab99137
9a24e7d
35a02a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Move push provider setting to the "Notifications" screen and display it only when several push provider are available. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,17 +20,24 @@ import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableIntStateOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import io.element.android.libraries.architecture.AsyncAction | ||
import io.element.android.libraries.architecture.Presenter | ||
import io.element.android.libraries.architecture.runCatchingUpdatingState | ||
import io.element.android.libraries.matrix.api.MatrixClient | ||
import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService | ||
import io.element.android.libraries.matrix.api.room.RoomNotificationMode | ||
import io.element.android.libraries.push.api.PushService | ||
import io.element.android.libraries.pushproviders.api.Distributor | ||
import io.element.android.libraries.pushproviders.api.PushProvider | ||
import io.element.android.libraries.pushstore.api.UserPushStore | ||
import io.element.android.libraries.pushstore.api.UserPushStoreFactory | ||
import kotlinx.collections.immutable.toImmutableList | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.FlowPreview | ||
import kotlinx.coroutines.flow.debounce | ||
|
@@ -44,7 +51,8 @@ class NotificationSettingsPresenter @Inject constructor( | |
private val notificationSettingsService: NotificationSettingsService, | ||
private val userPushStoreFactory: UserPushStoreFactory, | ||
private val matrixClient: MatrixClient, | ||
private val systemNotificationsEnabledProvider: SystemNotificationsEnabledProvider | ||
private val pushService: PushService, | ||
private val systemNotificationsEnabledProvider: SystemNotificationsEnabledProvider, | ||
) : Presenter<NotificationSettingsState> { | ||
@Composable | ||
override fun present(): NotificationSettingsState { | ||
|
@@ -68,6 +76,61 @@ class NotificationSettingsPresenter @Inject constructor( | |
observeNotificationSettings(matrixSettings) | ||
} | ||
|
||
// List of PushProvider -> Distributor | ||
val distributors = remember { | ||
pushService.getAvailablePushProviders() | ||
.flatMap { pushProvider -> | ||
pushProvider.getDistributors().map { distributor -> | ||
pushProvider to distributor | ||
} | ||
} | ||
} | ||
// List of Distributor names | ||
val distributorNames = remember { | ||
distributors.map { it.second.name } | ||
} | ||
|
||
var currentDistributorName by remember { mutableStateOf<AsyncAction<String>>(AsyncAction.Uninitialized) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, having an 'action' called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK: 9a24e7d |
||
var refreshPushProvider by remember { mutableIntStateOf(0) } | ||
|
||
LaunchedEffect(refreshPushProvider) { | ||
jmartinesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val p = pushService.getCurrentPushProvider() | ||
val name = p?.getCurrentDistributor(matrixClient)?.name | ||
currentDistributorName = if (name != null) { | ||
AsyncAction.Success(name) | ||
} else { | ||
AsyncAction.Failure(Exception("Failed to get current push provider")) | ||
} | ||
} | ||
|
||
var showChangePushProviderDialog by remember { mutableStateOf(false) } | ||
|
||
fun CoroutineScope.changePushProvider( | ||
data: Pair<PushProvider, Distributor>? | ||
) = launch { | ||
showChangePushProviderDialog = false | ||
data ?: return@launch | ||
// No op if the value is the same. | ||
if (data.second.name == currentDistributorName.dataOrNull()) return@launch | ||
currentDistributorName = AsyncAction.Loading | ||
data.let { (pushProvider, distributor) -> | ||
pushService.registerWith( | ||
matrixClient = matrixClient, | ||
pushProvider = pushProvider, | ||
distributor = distributor | ||
) | ||
.fold( | ||
{ | ||
currentDistributorName = AsyncAction.Success(distributor.name) | ||
refreshPushProvider++ | ||
}, | ||
{ | ||
currentDistributorName = AsyncAction.Failure(it) | ||
} | ||
) | ||
} | ||
} | ||
|
||
fun handleEvents(event: NotificationSettingsEvents) { | ||
when (event) { | ||
is NotificationSettingsEvents.SetAtRoomNotificationsEnabled -> { | ||
|
@@ -88,6 +151,9 @@ class NotificationSettingsPresenter @Inject constructor( | |
systemNotificationsEnabled.value = systemNotificationsEnabledProvider.notificationsEnabled() | ||
} | ||
NotificationSettingsEvents.ClearNotificationChangeError -> changeNotificationSettingAction.value = AsyncAction.Uninitialized | ||
NotificationSettingsEvents.ChangePushProvider -> showChangePushProviderDialog = true | ||
NotificationSettingsEvents.CancelChangePushProvider -> showChangePushProviderDialog = false | ||
is NotificationSettingsEvents.SetPushProvider -> localCoroutineScope.changePushProvider(distributors.getOrNull(event.index)) | ||
} | ||
} | ||
|
||
|
@@ -98,6 +164,9 @@ class NotificationSettingsPresenter @Inject constructor( | |
appNotificationsEnabled = appNotificationsEnabled.value | ||
), | ||
changeNotificationSettingAction = changeNotificationSettingAction.value, | ||
currentPushDistributor = currentDistributorName, | ||
availablePushDistributors = distributorNames.toImmutableList(), | ||
showChangePushProviderDialog = showChangePushProviderDialog, | ||
eventSink = ::handleEvents | ||
) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.