Add an advanced action to reset an account data entry (there is no API to completely delete it)

This commit is contained in:
Benoit Marty 2020-09-18 12:49:14 +02:00
parent d29d1ead9b
commit 0e85a3d59f
7 changed files with 73 additions and 9 deletions

View File

@ -20,7 +20,7 @@ Build 🧱:
-
Other changes:
-
- Add an advanced action to reset an account data entry
Changes in Element 1.0.7 (2020-09-17)
===================================================

View File

@ -56,6 +56,9 @@ abstract class GenericItemWithValue : VectorEpoxyModel<GenericItemWithValue.Hold
@EpoxyAttribute
var itemClickAction: View.OnClickListener? = null
@EpoxyAttribute
var itemLongClickAction: View.OnLongClickListener? = null
override fun bind(holder: Holder) {
super.bind(holder)
holder.titleText.setTextOrHide(title)
@ -76,6 +79,7 @@ abstract class GenericItemWithValue : VectorEpoxyModel<GenericItemWithValue.Hold
}
holder.view.setOnClickListener(itemClickAction?.let { DebouncedClickListener(it) })
holder.view.setOnLongClickListener(itemLongClickAction)
}
class Holder : VectorEpoxyHolder() {

View File

@ -0,0 +1,23 @@
/*
* 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.app.features.settings.devtools
import im.vector.app.core.platform.VectorViewModelAction
sealed class AccountDataAction : VectorViewModelAction {
data class DeleteAccountData(val type: String) : AccountDataAction()
}

View File

@ -21,13 +21,13 @@ import com.airbnb.epoxy.TypedEpoxyController
import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.Success
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
import im.vector.app.R
import im.vector.app.core.epoxy.loadingItem
import im.vector.app.core.resources.StringProvider
import im.vector.app.core.ui.list.genericFooterItem
import im.vector.app.core.ui.list.genericItemWithValue
import im.vector.app.core.utils.DebouncedClickListener
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
import javax.inject.Inject
class AccountDataEpoxyController @Inject constructor(
@ -36,6 +36,7 @@ class AccountDataEpoxyController @Inject constructor(
interface InteractionListener {
fun didTap(data: UserAccountDataEvent)
fun didLongTap(data: UserAccountDataEvent)
}
var interactionListener: InteractionListener? = null
@ -70,6 +71,10 @@ class AccountDataEpoxyController @Inject constructor(
itemClickAction(DebouncedClickListener(View.OnClickListener {
interactionListener?.didTap(accountData)
}))
itemLongClickAction(View.OnLongClickListener {
interactionListener?.didLongTap(accountData)
true
})
}
}
}

View File

@ -16,13 +16,14 @@
package im.vector.app.features.settings.devtools
import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import org.matrix.android.sdk.internal.di.MoshiProvider
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
import im.vector.app.R
import im.vector.app.core.dialogs.withColoredButton
import im.vector.app.core.extensions.cleanup
import im.vector.app.core.extensions.configureWith
import im.vector.app.core.platform.VectorBaseActivity
@ -31,6 +32,8 @@ import im.vector.app.core.resources.ColorProvider
import im.vector.app.core.utils.createJSonViewerStyleProvider
import kotlinx.android.synthetic.main.fragment_generic_recycler.*
import org.billcarsonfr.jsonviewer.JSonViewerDialog
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
import org.matrix.android.sdk.internal.di.MoshiProvider
import javax.inject.Inject
class AccountDataFragment @Inject constructor(
@ -74,4 +77,16 @@ class AccountDataFragment @Inject constructor(
createJSonViewerStyleProvider(colorProvider)
).show(childFragmentManager, "JSON_VIEWER")
}
override fun didLongTap(data: UserAccountDataEvent) {
AlertDialog.Builder(requireActivity())
.setTitle(R.string.delete)
.setMessage(getString(R.string.delete_account_data_warning, data.type))
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.delete) { _, _ ->
viewModel.handle(AccountDataAction.DeleteAccountData(data.type))
}
.show()
.withColoredButton(DialogInterface.BUTTON_POSITIVE)
}
}

View File

@ -16,6 +16,7 @@
package im.vector.app.features.settings.devtools
import androidx.lifecycle.viewModelScope
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxState
@ -24,11 +25,13 @@ import com.airbnb.mvrx.Uninitialized
import com.airbnb.mvrx.ViewModelContext
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
import im.vector.app.core.platform.EmptyAction
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
import org.matrix.android.sdk.internal.util.awaitCallback
import org.matrix.android.sdk.rx.rx
data class AccountDataViewState(
@ -37,7 +40,7 @@ data class AccountDataViewState(
class AccountDataViewModel @AssistedInject constructor(@Assisted initialState: AccountDataViewState,
private val session: Session)
: VectorViewModel<AccountDataViewState, EmptyAction, EmptyViewEvents>(initialState) {
: VectorViewModel<AccountDataViewState, AccountDataAction, EmptyViewEvents>(initialState) {
init {
session.rx().liveAccountData(emptySet())
@ -46,7 +49,19 @@ class AccountDataViewModel @AssistedInject constructor(@Assisted initialState: A
}
}
override fun handle(action: EmptyAction) {}
override fun handle(action: AccountDataAction) {
when (action) {
is AccountDataAction.DeleteAccountData -> handleDeleteAccountData(action)
}.exhaustive
}
private fun handleDeleteAccountData(action: AccountDataAction.DeleteAccountData) {
viewModelScope.launch {
awaitCallback {
session.updateAccountData(action.type, emptyMap(), it)
}
}
}
@AssistedInject.Factory
interface Factory {

View File

@ -2226,6 +2226,8 @@
<string name="settings_dev_tools">Dev Tools</string>
<string name="settings_account_data">Account Data</string>
<string name="delete_account_data_warning">Delete the account data of type %1$s?\n\nUse with caution, it may lead to unexpected behavior.</string>
<plurals name="poll_info">
<item quantity="zero">%d vote</item>
<item quantity="other">%d votes</item>