Remove (disable) shortcut if a room is left
This commit is contained in:
parent
f166348a68
commit
3a81c10062
@ -26,8 +26,10 @@ import io.reactivex.disposables.Disposable
|
|||||||
import io.reactivex.disposables.Disposables
|
import io.reactivex.disposables.Disposables
|
||||||
import org.matrix.android.sdk.api.session.room.RoomSortOrder
|
import org.matrix.android.sdk.api.session.room.RoomSortOrder
|
||||||
import org.matrix.android.sdk.api.session.room.model.Membership
|
import org.matrix.android.sdk.api.session.room.model.Membership
|
||||||
|
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||||
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
|
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
|
||||||
import org.matrix.android.sdk.rx.asObservable
|
import org.matrix.android.sdk.rx.asObservable
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ShortcutsHandler @Inject constructor(
|
class ShortcutsHandler @Inject constructor(
|
||||||
@ -52,22 +54,40 @@ class ShortcutsHandler @Inject constructor(
|
|||||||
.asObservable()
|
.asObservable()
|
||||||
.subscribe { rooms ->
|
.subscribe { rooms ->
|
||||||
// Remove dead shortcuts (i.e. deleted rooms)
|
// Remove dead shortcuts (i.e. deleted rooms)
|
||||||
val roomIds = rooms.map { it.roomId }
|
removeDeadShortcut(rooms.map { it.roomId })
|
||||||
val deadShortcutIds = ShortcutManagerCompat.getShortcuts(context, ShortcutManagerCompat.FLAG_MATCH_DYNAMIC)
|
|
||||||
.map { it.id }
|
|
||||||
.filter { !roomIds.contains(it) }
|
|
||||||
ShortcutManagerCompat.removeLongLivedShortcuts(context, deadShortcutIds)
|
|
||||||
|
|
||||||
val shortcuts = rooms.mapIndexed { index, room ->
|
// Create shortcuts
|
||||||
shortcutCreator.create(room, index)
|
createShortcuts(rooms)
|
||||||
}
|
|
||||||
|
|
||||||
shortcuts.forEach { shortcut ->
|
|
||||||
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun removeDeadShortcut(roomIds: List<String>) {
|
||||||
|
val deadShortcutIds = ShortcutManagerCompat.getShortcuts(context, ShortcutManagerCompat.FLAG_MATCH_DYNAMIC)
|
||||||
|
.map { it.id }
|
||||||
|
.filter { !roomIds.contains(it) }
|
||||||
|
|
||||||
|
if (deadShortcutIds.isNotEmpty()) {
|
||||||
|
Timber.d("Removing shortcut(s) $deadShortcutIds")
|
||||||
|
ShortcutManagerCompat.removeLongLivedShortcuts(context, deadShortcutIds)
|
||||||
|
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||||
|
context.getSystemService<ShortcutManager>()?.disableShortcuts(deadShortcutIds)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createShortcuts(rooms: List<RoomSummary>) {
|
||||||
|
val shortcuts = rooms.mapIndexed { index, room ->
|
||||||
|
shortcutCreator.create(room, index)
|
||||||
|
}
|
||||||
|
|
||||||
|
shortcuts.forEach { shortcut ->
|
||||||
|
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun clearShortcuts() {
|
fun clearShortcuts() {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
|
||||||
// No op
|
// No op
|
||||||
|
Loading…
Reference in New Issue
Block a user