mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 10:25:35 +03:00
Widget: get user list widgets from user account
This commit is contained in:
parent
dea903bcb5
commit
3faf42be53
6 changed files with 100 additions and 16 deletions
|
@ -46,6 +46,7 @@ import im.vector.matrix.android.api.session.sync.SyncState
|
|||
import im.vector.matrix.android.api.session.terms.TermsService
|
||||
import im.vector.matrix.android.api.session.user.UserService
|
||||
import im.vector.matrix.android.api.session.widgets.WidgetService
|
||||
import im.vector.matrix.android.api.session.widgets.WidgetURLBuilder
|
||||
import im.vector.matrix.android.internal.auth.SessionParamsStore
|
||||
import im.vector.matrix.android.internal.crypto.DefaultCryptoService
|
||||
import im.vector.matrix.android.internal.crypto.crosssigning.ShieldTrustUpdater
|
||||
|
@ -58,6 +59,7 @@ import im.vector.matrix.android.internal.session.room.timeline.TimelineEventDecr
|
|||
import im.vector.matrix.android.internal.session.sync.SyncTokenStore
|
||||
import im.vector.matrix.android.internal.session.sync.job.SyncThread
|
||||
import im.vector.matrix.android.internal.session.sync.job.SyncWorker
|
||||
import im.vector.matrix.android.internal.session.widgets.WidgetDependenciesHolder
|
||||
import im.vector.matrix.android.internal.session.widgets.WidgetManager
|
||||
import im.vector.matrix.android.internal.task.TaskExecutor
|
||||
import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers
|
||||
|
@ -105,13 +107,12 @@ internal class DefaultSession @Inject constructor(
|
|||
private val _sharedSecretStorageService: Lazy<SharedSecretStorageService>,
|
||||
private val accountService: Lazy<AccountService>,
|
||||
private val timelineEventDecryptor: TimelineEventDecryptor,
|
||||
private val integrationManager: IntegrationManager,
|
||||
private val widgetManager: WidgetManager,
|
||||
private val shieldTrustUpdater: ShieldTrustUpdater,
|
||||
private val coroutineDispatchers: MatrixCoroutineDispatchers,
|
||||
private val defaultIdentityService: DefaultIdentityService,
|
||||
private val taskExecutor: TaskExecutor
|
||||
) : Session,
|
||||
private val taskExecutor: TaskExecutor,
|
||||
private val widgetDependenciesHolder: WidgetDependenciesHolder,
|
||||
private val shieldTrustUpdater: ShieldTrustUpdater)
|
||||
: Session,
|
||||
RoomService by roomService.get(),
|
||||
RoomDirectoryService by roomDirectoryService.get(),
|
||||
GroupService by groupService.get(),
|
||||
|
@ -148,9 +149,8 @@ internal class DefaultSession @Inject constructor(
|
|||
eventBus.register(this)
|
||||
timelineEventDecryptor.start()
|
||||
shieldTrustUpdater.start()
|
||||
integrationManager.start()
|
||||
widgetManager.start()
|
||||
defaultIdentityService.start()
|
||||
widgetDependenciesHolder.start()
|
||||
}
|
||||
|
||||
override fun requireBackgroundSync() {
|
||||
|
@ -193,11 +193,10 @@ internal class DefaultSession @Inject constructor(
|
|||
isOpen = false
|
||||
eventBus.unregister(this)
|
||||
shieldTrustUpdater.stop()
|
||||
integrationManager.stop()
|
||||
widgetManager.stop()
|
||||
taskExecutor.executorScope.launch(coroutineDispatchers.main) {
|
||||
// This has to be done on main thread
|
||||
defaultIdentityService.stop()
|
||||
widgetDependenciesHolder.stop()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,6 @@ import androidx.lifecycle.LifecycleOwner
|
|||
import androidx.lifecycle.LifecycleRegistry
|
||||
import im.vector.matrix.android.R
|
||||
import im.vector.matrix.android.api.MatrixCallback
|
||||
import im.vector.matrix.android.api.session.events.model.Content
|
||||
import im.vector.matrix.android.api.session.events.model.toModel
|
||||
import im.vector.matrix.android.api.session.widgets.model.WidgetContent
|
||||
import im.vector.matrix.android.api.util.Cancelable
|
||||
|
@ -32,6 +31,7 @@ import im.vector.matrix.android.internal.session.sync.model.accountdata.UserAcco
|
|||
import im.vector.matrix.android.internal.session.sync.model.accountdata.UserAccountDataEvent
|
||||
import im.vector.matrix.android.internal.session.user.accountdata.AccountDataDataSource
|
||||
import im.vector.matrix.android.internal.session.user.accountdata.UpdateUserAccountDataTask
|
||||
import im.vector.matrix.android.internal.session.widgets.helper.extractWidgets
|
||||
import im.vector.matrix.android.internal.task.TaskExecutor
|
||||
import im.vector.matrix.android.internal.task.configureWith
|
||||
import im.vector.matrix.android.internal.util.StringProvider
|
||||
|
@ -294,11 +294,8 @@ internal class IntegrationManager @Inject constructor(private val taskExecutor:
|
|||
}
|
||||
|
||||
private fun UserAccountDataEvent.asIntegrationManagerWidgetContent(): WidgetContent? {
|
||||
return content.asSequence()
|
||||
.mapNotNull {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(it.value as? Content)?.toModel<WidgetContent>()
|
||||
}.filter {
|
||||
return extractWidgets()
|
||||
.filter {
|
||||
it.type == INTEGRATION_MANAGER_WIDGET
|
||||
}
|
||||
.firstOrNull()
|
||||
|
|
|
@ -21,6 +21,6 @@ import im.vector.matrix.android.api.session.widgets.model.WidgetContent
|
|||
|
||||
data class Widget(
|
||||
private val widgetContent: WidgetContent,
|
||||
private val event: Event
|
||||
private val event: Event? = null
|
||||
)
|
||||
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (c) 2020 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package im.vector.matrix.android.internal.session.widgets
|
||||
|
||||
import im.vector.matrix.android.internal.session.integrationmanager.IntegrationManager
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class WidgetDependenciesHolder @Inject constructor(private val integrationManager: IntegrationManager,
|
||||
private val widgetManager: WidgetManager,
|
||||
private val widgetURLBuilder: DefaultWidgetURLBuilder) {
|
||||
|
||||
fun start() {
|
||||
integrationManager.start()
|
||||
widgetManager.start()
|
||||
widgetURLBuilder.start()
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
integrationManager.stop()
|
||||
widgetManager.stop()
|
||||
widgetURLBuilder.stop()
|
||||
}
|
||||
}
|
|
@ -33,6 +33,9 @@ import im.vector.matrix.android.internal.di.UserId
|
|||
import im.vector.matrix.android.internal.session.SessionScope
|
||||
import im.vector.matrix.android.internal.session.integrationmanager.IntegrationManager
|
||||
import im.vector.matrix.android.internal.session.room.state.StateEventDataSource
|
||||
import im.vector.matrix.android.internal.session.sync.model.accountdata.UserAccountData
|
||||
import im.vector.matrix.android.internal.session.user.accountdata.AccountDataDataSource
|
||||
import im.vector.matrix.android.internal.session.widgets.helper.extractWidgets
|
||||
import im.vector.matrix.android.internal.task.TaskExecutor
|
||||
import im.vector.matrix.android.internal.task.launchToCallback
|
||||
import java.util.HashMap
|
||||
|
@ -40,6 +43,7 @@ import javax.inject.Inject
|
|||
|
||||
@SessionScope
|
||||
internal class WidgetManager @Inject constructor(private val integrationManager: IntegrationManager,
|
||||
private val accountDataDataSource: AccountDataDataSource,
|
||||
private val stateEventDataSource: StateEventDataSource,
|
||||
private val taskExecutor: TaskExecutor,
|
||||
private val createWidgetTask: CreateWidgetTask,
|
||||
|
@ -98,6 +102,23 @@ internal class WidgetManager @Inject constructor(private val integrationManager:
|
|||
return widgets.values.toList()
|
||||
}
|
||||
|
||||
fun getUserWidgets(
|
||||
widgetTypes: Set<String>? = null,
|
||||
excludedTypes: Set<String>? = null
|
||||
): List<Widget> {
|
||||
val widgetsAccountData = accountDataDataSource.getAccountDataEvent(UserAccountData.TYPE_WIDGETS) ?: return emptyList()
|
||||
return widgetsAccountData.extractWidgets()
|
||||
.filter {
|
||||
val widgetType = it.type ?: return@filter false
|
||||
(widgetTypes == null || widgetTypes.contains(widgetType))
|
||||
&& (excludedTypes == null || !excludedTypes.contains(widgetType))
|
||||
}
|
||||
.map {
|
||||
Widget(it)
|
||||
}
|
||||
.toList()
|
||||
}
|
||||
|
||||
fun createWidget(roomId: String, widgetId: String, content: Content, callback: MatrixCallback<Widget>): Cancelable {
|
||||
return taskExecutor.executorScope.launchToCallback(callback = callback) {
|
||||
if (!hasPermissionsToHandleWidgets(roomId)) {
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2020 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package im.vector.matrix.android.internal.session.widgets.helper
|
||||
|
||||
import im.vector.matrix.android.api.session.events.model.Content
|
||||
import im.vector.matrix.android.api.session.events.model.toModel
|
||||
import im.vector.matrix.android.api.session.widgets.model.WidgetContent
|
||||
import im.vector.matrix.android.internal.session.sync.model.accountdata.UserAccountDataEvent
|
||||
|
||||
internal fun UserAccountDataEvent.extractWidgets(): Sequence<WidgetContent> {
|
||||
return content.asSequence()
|
||||
.mapNotNull {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(it.value as? Content)?.toModel<WidgetContent>()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue