Add upload capability on the screen and give possibility to refresh the data

This commit is contained in:
Benoit Marty 2021-02-18 23:40:03 +01:00 committed by Benoit Marty
parent 80524fb8c1
commit f9f54cabdd
8 changed files with 74 additions and 13 deletions

View File

@ -21,6 +21,11 @@ package org.matrix.android.sdk.api.session.homeserver
*/
interface HomeServerCapabilitiesService {
/**
* Force a refresh of the stored data
*/
suspend fun refreshHomeServerCapabilities()
/**
* Get the HomeServer capabilities
*/

View File

@ -17,16 +17,23 @@
package org.matrix.android.sdk.internal.session.homeserver
import com.zhuinden.monarchy.Monarchy
import io.realm.Realm
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
import org.matrix.android.sdk.internal.database.mapper.HomeServerCapabilitiesMapper
import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntity
import org.matrix.android.sdk.internal.database.query.get
import org.matrix.android.sdk.internal.di.SessionDatabase
import io.realm.Realm
import javax.inject.Inject
internal class DefaultHomeServerCapabilitiesService @Inject constructor(@SessionDatabase private val monarchy: Monarchy) : HomeServerCapabilitiesService {
internal class DefaultHomeServerCapabilitiesService @Inject constructor(
@SessionDatabase private val monarchy: Monarchy,
private val getHomeServerCapabilitiesTask: GetHomeServerCapabilitiesTask
) : HomeServerCapabilitiesService {
override suspend fun refreshHomeServerCapabilities() {
getHomeServerCapabilitiesTask.execute(GetHomeServerCapabilitiesTask.Params(forceRefresh = true))
}
override fun getHomeServerCapabilities(): HomeServerCapabilities {
return Realm.getInstance(monarchy.realmConfiguration).use { realm ->

View File

@ -38,7 +38,11 @@ import timber.log.Timber
import java.util.Date
import javax.inject.Inject
internal interface GetHomeServerCapabilitiesTask : Task<Unit, Unit>
internal interface GetHomeServerCapabilitiesTask : Task<GetHomeServerCapabilitiesTask.Params, Unit> {
data class Params(
val forceRefresh: Boolean
)
}
internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
private val capabilitiesAPI: CapabilitiesAPI,
@ -52,12 +56,14 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
private val userId: String
) : GetHomeServerCapabilitiesTask {
override suspend fun execute(params: Unit) {
var doRequest = false
monarchy.awaitTransaction { realm ->
val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm)
override suspend fun execute(params: GetHomeServerCapabilitiesTask.Params) {
var doRequest = params.forceRefresh
if (!doRequest) {
monarchy.awaitTransaction { realm ->
val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm)
doRequest = homeServerCapabilitiesEntity.lastUpdatedTimestamp + MIN_DELAY_BETWEEN_TWO_REQUEST_MILLIS < Date().time
doRequest = homeServerCapabilitiesEntity.lastUpdatedTimestamp + MIN_DELAY_BETWEEN_TWO_REQUEST_MILLIS < Date().time
}
}
if (!doRequest) {
@ -123,7 +129,7 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
}
companion object {
// 8 hours like on Riot Web
// 8 hours like on Element Web
private const val MIN_DELAY_BETWEEN_TWO_REQUEST_MILLIS = 8 * 60 * 60 * 1000
}
}

View File

@ -77,7 +77,7 @@ internal class DefaultSyncTask @Inject constructor(
initialSyncProgressService.startTask(R.string.initial_sync_start_importing_account, 100)
}
// Maybe refresh the home server capabilities data we know
getHomeServerCapabilitiesTask.execute(Unit)
getHomeServerCapabilitiesTask.execute(GetHomeServerCapabilitiesTask.Params(forceRefresh = false))
val readTimeOut = (params.timeout + TIMEOUT_MARGIN).coerceAtLeast(TimeOutInterceptor.DEFAULT_LONG_TIMEOUT)

View File

@ -20,8 +20,10 @@ import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.Uninitialized
import org.matrix.android.sdk.api.federation.FederationVersion
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
data class HomeServerSettingsViewState(
val baseUrl: String = "",
val homeServerCapabilities: HomeServerCapabilities = HomeServerCapabilities(),
val federationVersion: Async<FederationVersion> = Uninitialized
) : MvRxState

View File

@ -25,13 +25,16 @@ import im.vector.app.R
import im.vector.app.core.epoxy.errorWithRetryItem
import im.vector.app.core.epoxy.loadingItem
import im.vector.app.core.error.ErrorFormatter
import im.vector.app.core.resources.StringProvider
import im.vector.app.features.discovery.settingsCenteredImageItem
import im.vector.app.features.discovery.settingsInfoItem
import im.vector.app.features.discovery.settingsSectionTitleItem
import org.matrix.android.sdk.api.federation.FederationVersion
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
import javax.inject.Inject
class HomeserverSettingsController @Inject constructor(
private val stringProvider: StringProvider,
private val errorFormatter: ErrorFormatter
) : TypedEpoxyController<HomeServerSettingsViewState>() {
@ -51,15 +54,16 @@ class HomeserverSettingsController @Inject constructor(
loadingItem {
id("loading")
}
is Fail ->
is Fail ->
errorWithRetryItem {
id("error")
text(errorFormatter.toHumanReadable(federationVersion.error))
listener { callback?.retry() }
}
is Success ->
is Success ->
buildFederationVersion(federationVersion())
}
buildCapabilities(data)
}
private fun buildHeader(state: HomeServerSettingsViewState) {
@ -95,4 +99,22 @@ class HomeserverSettingsController @Inject constructor(
helperText(federationVersion.version)
}
}
private fun buildCapabilities(data: HomeServerSettingsViewState) {
settingsSectionTitleItem {
id("uploadTitle")
titleResId(R.string.settings_server_upload_size_title)
}
val limit = data.homeServerCapabilities.maxUploadFileSize
settingsInfoItem {
id("uploadValue")
if (limit == HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN) {
helperTextResId(R.string.settings_server_upload_size_unknown)
} else {
helperText(stringProvider.getString(R.string.settings_server_upload_size_content, "${limit / 1048576L} MB"))
}
}
}
}

View File

@ -53,10 +53,26 @@ class HomeserverSettingsViewModel @AssistedInject constructor(
init {
setState {
copy(
baseUrl = session.sessionParams.homeServerUrl
baseUrl = session.sessionParams.homeServerUrl,
homeServerCapabilities = session.getHomeServerCapabilities()
)
}
fetchHomeserverVersion()
refreshHomeServerCapabilities()
}
private fun refreshHomeServerCapabilities() {
viewModelScope.launch {
runCatching {
session.refreshHomeServerCapabilities()
}
setState {
copy(
homeServerCapabilities = session.getHomeServerCapabilities()
)
}
}
}
private fun fetchHomeserverVersion() {

View File

@ -2338,6 +2338,9 @@
<string name="settings_server_name">Server name</string>
<string name="settings_server_version">Server version</string>
<string name="settings_server_upload_size_title">Server file upload limit</string>
<string name="settings_server_upload_size_content">Your homeserver accepts attachments (files, media, etc.) with a size up to %s.</string>
<string name="settings_server_upload_size_unknown">The limit is unknown.</string>
<string name="settings_failed_to_get_crypto_device_info">No cryptographic information available</string>