Convert WidgetService to suspend functions
Signed-off-by: Dominic Fischer <dominicfischer7@gmail.com>
This commit is contained in:
parent
2045a164c1
commit
c548a3d2fa
|
@ -17,10 +17,8 @@
|
||||||
package org.matrix.android.sdk.api.session.widgets
|
package org.matrix.android.sdk.api.session.widgets
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import org.matrix.android.sdk.api.MatrixCallback
|
|
||||||
import org.matrix.android.sdk.api.query.QueryStringValue
|
import org.matrix.android.sdk.api.query.QueryStringValue
|
||||||
import org.matrix.android.sdk.api.session.events.model.Content
|
import org.matrix.android.sdk.api.session.events.model.Content
|
||||||
import org.matrix.android.sdk.api.util.Cancelable
|
|
||||||
import org.matrix.android.sdk.api.session.widgets.model.Widget
|
import org.matrix.android.sdk.api.session.widgets.model.Widget
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -107,20 +105,16 @@ interface WidgetService {
|
||||||
* @param roomId the room where you want to create the widget.
|
* @param roomId the room where you want to create the widget.
|
||||||
* @param widgetId the widget to create.
|
* @param widgetId the widget to create.
|
||||||
* @param content the content of the widget
|
* @param content the content of the widget
|
||||||
* @param callback the matrix callback to listen for result.
|
|
||||||
* @return Cancelable
|
|
||||||
*/
|
*/
|
||||||
fun createRoomWidget(roomId: String, widgetId: String, content: Content, callback: MatrixCallback<Widget>): Cancelable
|
suspend fun createRoomWidget(roomId: String, widgetId: String, content: Content): Widget
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deactivate a widget in a room. It makes sure you have the rights to handle this.
|
* Deactivate a widget in a room. It makes sure you have the rights to handle this.
|
||||||
*
|
*
|
||||||
* @param roomId: the room where you want to deactivate the widget.
|
* @param roomId: the room where you want to deactivate the widget.
|
||||||
* @param widgetId: the widget to deactivate.
|
* @param widgetId: the widget to deactivate.
|
||||||
* @param callback the matrix callback to listen for result.
|
|
||||||
* @return Cancelable
|
|
||||||
*/
|
*/
|
||||||
fun destroyRoomWidget(roomId: String, widgetId: String, callback: MatrixCallback<Unit>): Cancelable
|
suspend fun destroyRoomWidget(roomId: String, widgetId: String)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if you can add/remove widgets. It goes through
|
* Returns true if you can add/remove widgets. It goes through
|
||||||
|
|
|
@ -17,14 +17,12 @@
|
||||||
package org.matrix.android.sdk.internal.session.widgets
|
package org.matrix.android.sdk.internal.session.widgets
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import org.matrix.android.sdk.api.MatrixCallback
|
|
||||||
import org.matrix.android.sdk.api.query.QueryStringValue
|
import org.matrix.android.sdk.api.query.QueryStringValue
|
||||||
import org.matrix.android.sdk.api.session.events.model.Content
|
import org.matrix.android.sdk.api.session.events.model.Content
|
||||||
import org.matrix.android.sdk.api.session.widgets.WidgetPostAPIMediator
|
import org.matrix.android.sdk.api.session.widgets.WidgetPostAPIMediator
|
||||||
import org.matrix.android.sdk.api.session.widgets.WidgetService
|
import org.matrix.android.sdk.api.session.widgets.WidgetService
|
||||||
import org.matrix.android.sdk.api.session.widgets.WidgetURLFormatter
|
import org.matrix.android.sdk.api.session.widgets.WidgetURLFormatter
|
||||||
import org.matrix.android.sdk.api.session.widgets.model.Widget
|
import org.matrix.android.sdk.api.session.widgets.model.Widget
|
||||||
import org.matrix.android.sdk.api.util.Cancelable
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Provider
|
import javax.inject.Provider
|
||||||
|
|
||||||
|
@ -77,21 +75,19 @@ internal class DefaultWidgetService @Inject constructor(private val widgetManage
|
||||||
return widgetManager.getUserWidgets(widgetTypes, excludedTypes)
|
return widgetManager.getUserWidgets(widgetTypes, excludedTypes)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun createRoomWidget(
|
override suspend fun createRoomWidget(
|
||||||
roomId: String,
|
roomId: String,
|
||||||
widgetId: String,
|
widgetId: String,
|
||||||
content: Content,
|
content: Content
|
||||||
callback: MatrixCallback<Widget>
|
): Widget {
|
||||||
): Cancelable {
|
return widgetManager.createRoomWidget(roomId, widgetId, content)
|
||||||
return widgetManager.createRoomWidget(roomId, widgetId, content, callback)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun destroyRoomWidget(
|
override suspend fun destroyRoomWidget(
|
||||||
roomId: String,
|
roomId: String,
|
||||||
widgetId: String,
|
widgetId: String
|
||||||
callback: MatrixCallback<Unit>
|
) {
|
||||||
): Cancelable {
|
return widgetManager.destroyRoomWidget(roomId, widgetId)
|
||||||
return widgetManager.destroyRoomWidget(roomId, widgetId, callback)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hasPermissionsToHandleWidgets(roomId: String): Boolean {
|
override fun hasPermissionsToHandleWidgets(roomId: String): Boolean {
|
||||||
|
|
|
@ -21,7 +21,6 @@ import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.lifecycle.LifecycleRegistry
|
import androidx.lifecycle.LifecycleRegistry
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.Transformations
|
import androidx.lifecycle.Transformations
|
||||||
import org.matrix.android.sdk.api.MatrixCallback
|
|
||||||
import org.matrix.android.sdk.api.query.QueryStringValue
|
import org.matrix.android.sdk.api.query.QueryStringValue
|
||||||
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
|
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
|
||||||
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
|
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
|
||||||
|
@ -34,7 +33,6 @@ import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
|
||||||
import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper
|
import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper
|
||||||
import org.matrix.android.sdk.api.session.widgets.WidgetManagementFailure
|
import org.matrix.android.sdk.api.session.widgets.WidgetManagementFailure
|
||||||
import org.matrix.android.sdk.api.session.widgets.model.Widget
|
import org.matrix.android.sdk.api.session.widgets.model.Widget
|
||||||
import org.matrix.android.sdk.api.util.Cancelable
|
|
||||||
import org.matrix.android.sdk.internal.di.UserId
|
import org.matrix.android.sdk.internal.di.UserId
|
||||||
import org.matrix.android.sdk.internal.session.SessionLifecycleObserver
|
import org.matrix.android.sdk.internal.session.SessionLifecycleObserver
|
||||||
import org.matrix.android.sdk.internal.session.SessionScope
|
import org.matrix.android.sdk.internal.session.SessionScope
|
||||||
|
@ -43,8 +41,6 @@ import org.matrix.android.sdk.internal.session.room.state.StateEventDataSource
|
||||||
import org.matrix.android.sdk.internal.session.user.accountdata.AccountDataDataSource
|
import org.matrix.android.sdk.internal.session.user.accountdata.AccountDataDataSource
|
||||||
import org.matrix.android.sdk.internal.session.widgets.helper.WidgetFactory
|
import org.matrix.android.sdk.internal.session.widgets.helper.WidgetFactory
|
||||||
import org.matrix.android.sdk.internal.session.widgets.helper.extractWidgetSequence
|
import org.matrix.android.sdk.internal.session.widgets.helper.extractWidgetSequence
|
||||||
import org.matrix.android.sdk.internal.task.TaskExecutor
|
|
||||||
import org.matrix.android.sdk.internal.task.launchToCallback
|
|
||||||
import java.util.HashMap
|
import java.util.HashMap
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@ -52,7 +48,6 @@ import javax.inject.Inject
|
||||||
internal class WidgetManager @Inject constructor(private val integrationManager: IntegrationManager,
|
internal class WidgetManager @Inject constructor(private val integrationManager: IntegrationManager,
|
||||||
private val accountDataDataSource: AccountDataDataSource,
|
private val accountDataDataSource: AccountDataDataSource,
|
||||||
private val stateEventDataSource: StateEventDataSource,
|
private val stateEventDataSource: StateEventDataSource,
|
||||||
private val taskExecutor: TaskExecutor,
|
|
||||||
private val createWidgetTask: CreateWidgetTask,
|
private val createWidgetTask: CreateWidgetTask,
|
||||||
private val widgetFactory: WidgetFactory,
|
private val widgetFactory: WidgetFactory,
|
||||||
@UserId private val userId: String)
|
@UserId private val userId: String)
|
||||||
|
@ -165,8 +160,7 @@ internal class WidgetManager @Inject constructor(private val integrationManager:
|
||||||
.toList()
|
.toList()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createRoomWidget(roomId: String, widgetId: String, content: Content, callback: MatrixCallback<Widget>): Cancelable {
|
suspend fun createRoomWidget(roomId: String, widgetId: String, content: Content): Widget {
|
||||||
return taskExecutor.executorScope.launchToCallback(callback = callback) {
|
|
||||||
if (!hasPermissionsToHandleWidgets(roomId)) {
|
if (!hasPermissionsToHandleWidgets(roomId)) {
|
||||||
throw WidgetManagementFailure.NotEnoughPower
|
throw WidgetManagementFailure.NotEnoughPower
|
||||||
}
|
}
|
||||||
|
@ -177,15 +171,13 @@ internal class WidgetManager @Inject constructor(private val integrationManager:
|
||||||
)
|
)
|
||||||
createWidgetTask.execute(params)
|
createWidgetTask.execute(params)
|
||||||
try {
|
try {
|
||||||
getRoomWidgets(roomId, widgetId = QueryStringValue.Equals(widgetId, QueryStringValue.Case.INSENSITIVE)).first()
|
return getRoomWidgets(roomId, widgetId = QueryStringValue.Equals(widgetId, QueryStringValue.Case.INSENSITIVE)).first()
|
||||||
} catch (failure: Throwable) {
|
} catch (failure: Throwable) {
|
||||||
throw WidgetManagementFailure.CreationFailed
|
throw WidgetManagementFailure.CreationFailed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun destroyRoomWidget(roomId: String, widgetId: String, callback: MatrixCallback<Unit>): Cancelable {
|
suspend fun destroyRoomWidget(roomId: String, widgetId: String) {
|
||||||
return taskExecutor.executorScope.launchToCallback(callback = callback) {
|
|
||||||
if (!hasPermissionsToHandleWidgets(roomId)) {
|
if (!hasPermissionsToHandleWidgets(roomId)) {
|
||||||
throw WidgetManagementFailure.NotEnoughPower
|
throw WidgetManagementFailure.NotEnoughPower
|
||||||
}
|
}
|
||||||
|
@ -196,7 +188,6 @@ internal class WidgetManager @Inject constructor(private val integrationManager:
|
||||||
)
|
)
|
||||||
createWidgetTask.execute(params)
|
createWidgetTask.execute(params)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun hasPermissionsToHandleWidgets(roomId: String): Boolean {
|
fun hasPermissionsToHandleWidgets(roomId: String): Boolean {
|
||||||
val powerLevelsEvent = stateEventDataSource.getStateEvent(
|
val powerLevelsEvent = stateEventDataSource.getStateEvent(
|
||||||
|
|
|
@ -97,12 +97,10 @@ import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
|
||||||
import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent
|
import org.matrix.android.sdk.api.session.room.timeline.getLastMessageContent
|
||||||
import org.matrix.android.sdk.api.session.room.timeline.getRelationContent
|
import org.matrix.android.sdk.api.session.room.timeline.getRelationContent
|
||||||
import org.matrix.android.sdk.api.session.room.timeline.getTextEditableContent
|
import org.matrix.android.sdk.api.session.room.timeline.getTextEditableContent
|
||||||
import org.matrix.android.sdk.api.session.widgets.model.Widget
|
|
||||||
import org.matrix.android.sdk.api.session.widgets.model.WidgetType
|
import org.matrix.android.sdk.api.session.widgets.model.WidgetType
|
||||||
import org.matrix.android.sdk.api.util.appendParamToUrl
|
import org.matrix.android.sdk.api.util.appendParamToUrl
|
||||||
import org.matrix.android.sdk.api.util.toOptional
|
import org.matrix.android.sdk.api.util.toOptional
|
||||||
import org.matrix.android.sdk.internal.crypto.model.event.WithHeldCode
|
import org.matrix.android.sdk.internal.crypto.model.event.WithHeldCode
|
||||||
import org.matrix.android.sdk.internal.util.awaitCallback
|
|
||||||
import org.matrix.android.sdk.rx.rx
|
import org.matrix.android.sdk.rx.rx
|
||||||
import org.matrix.android.sdk.rx.unwrap
|
import org.matrix.android.sdk.rx.unwrap
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
@ -483,9 +481,7 @@ class RoomDetailViewModel @AssistedInject constructor(
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val widget = awaitCallback<Widget> {
|
val widget = session.widgetService().createRoomWidget(roomId, widgetId, widgetEventContent)
|
||||||
session.widgetService().createRoomWidget(roomId, widgetId, widgetEventContent, it)
|
|
||||||
}
|
|
||||||
_viewEvents.post(RoomDetailViewEvents.JoinJitsiConference(widget, action.withVideo))
|
_viewEvents.post(RoomDetailViewEvents.JoinJitsiConference(widget, action.withVideo))
|
||||||
} catch (failure: Throwable) {
|
} catch (failure: Throwable) {
|
||||||
_viewEvents.post(RoomDetailViewEvents.ShowMessage(stringProvider.getString(R.string.failed_to_add_widget)))
|
_viewEvents.post(RoomDetailViewEvents.ShowMessage(stringProvider.getString(R.string.failed_to_add_widget)))
|
||||||
|
@ -499,7 +495,7 @@ class RoomDetailViewModel @AssistedInject constructor(
|
||||||
_viewEvents.post(RoomDetailViewEvents.ShowWaitingView)
|
_viewEvents.post(RoomDetailViewEvents.ShowWaitingView)
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
awaitCallback<Unit> { session.widgetService().destroyRoomWidget(room.roomId, widgetId, it) }
|
session.widgetService().destroyRoomWidget(room.roomId, widgetId)
|
||||||
// local echo
|
// local echo
|
||||||
setState {
|
setState {
|
||||||
copy(
|
copy(
|
||||||
|
|
|
@ -289,14 +289,15 @@ class WidgetPostAPIHandler @AssistedInject constructor(@Assisted private val roo
|
||||||
callback = createWidgetAPICallback(widgetPostAPIMediator, eventData)
|
callback = createWidgetAPICallback(widgetPostAPIMediator, eventData)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
launchWidgetAPIAction(widgetPostAPIMediator, eventData) {
|
||||||
session.widgetService().createRoomWidget(
|
session.widgetService().createRoomWidget(
|
||||||
roomId = roomId,
|
roomId = roomId,
|
||||||
widgetId = widgetId,
|
widgetId = widgetId,
|
||||||
content = widgetEventContent,
|
content = widgetEventContent
|
||||||
callback = createWidgetAPICallback(widgetPostAPIMediator, eventData)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the 'plumbing state"
|
* Update the 'plumbing state"
|
||||||
|
|
|
@ -41,7 +41,6 @@ import org.matrix.android.sdk.api.session.integrationmanager.IntegrationManagerS
|
||||||
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
|
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
|
||||||
import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper
|
import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper
|
||||||
import org.matrix.android.sdk.api.session.widgets.WidgetManagementFailure
|
import org.matrix.android.sdk.api.session.widgets.WidgetManagementFailure
|
||||||
import org.matrix.android.sdk.internal.util.awaitCallback
|
|
||||||
import org.matrix.android.sdk.rx.mapOptional
|
import org.matrix.android.sdk.rx.mapOptional
|
||||||
import org.matrix.android.sdk.rx.rx
|
import org.matrix.android.sdk.rx.rx
|
||||||
import org.matrix.android.sdk.rx.unwrap
|
import org.matrix.android.sdk.rx.unwrap
|
||||||
|
@ -173,10 +172,8 @@ class WidgetViewModel @AssistedInject constructor(@Assisted val initialState: Wi
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val widgetId = initialState.widgetId ?: return@launch
|
val widgetId = initialState.widgetId ?: return@launch
|
||||||
try {
|
try {
|
||||||
awaitCallback<Unit> {
|
widgetService.destroyRoomWidget(initialState.roomId, widgetId)
|
||||||
widgetService.destroyRoomWidget(initialState.roomId, widgetId, it)
|
|
||||||
_viewEvents.post(WidgetViewEvents.Close())
|
_viewEvents.post(WidgetViewEvents.Close())
|
||||||
}
|
|
||||||
} catch (failure: Throwable) {
|
} catch (failure: Throwable) {
|
||||||
_viewEvents.post(WidgetViewEvents.Failure(failure))
|
_viewEvents.post(WidgetViewEvents.Failure(failure))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue