From 9586ada327411b17fd6aaf495dc1de80b9fe4dae Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 6 Dec 2020 03:31:13 +0300 Subject: [PATCH] EditProfileActivity: allow adding fiels as much as instance allows (fix #89) --- .../tusky/EditProfileActivity.kt | 29 +++++++++++++------ .../keylesspalace/tusky/entity/Instance.kt | 11 +++++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.kt b/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.kt index a1e68ddf..f9b2c5a2 100644 --- a/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/EditProfileActivity.kt @@ -64,7 +64,7 @@ class EditProfileActivity : BaseActivity(), Injectable { private const val AVATAR_PICK_RESULT = 1 private const val HEADER_PICK_RESULT = 2 private const val PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1 - private const val MAX_ACCOUNT_FIELDS = 4 + private const val MASTODON_MAX_ACCOUNT_FIELDS = 4 private const val BUNDLE_CURRENTLY_PICKING = "BUNDLE_CURRENTLY_PICKING" } @@ -77,6 +77,7 @@ class EditProfileActivity : BaseActivity(), Injectable { private var currentlyPicking: PickType = PickType.NOTHING private val accountFieldEditAdapter = AccountFieldEditAdapter() + private var maxAccountFields = MASTODON_MAX_ACCOUNT_FIELDS private enum class PickType { NOTHING, @@ -112,7 +113,7 @@ class EditProfileActivity : BaseActivity(), Injectable { addFieldButton.setOnClickListener { accountFieldEditAdapter.addField() - if(accountFieldEditAdapter.itemCount >= MAX_ACCOUNT_FIELDS) { + if(accountFieldEditAdapter.itemCount >= maxAccountFields) { it.isVisible = false } @@ -123,7 +124,7 @@ class EditProfileActivity : BaseActivity(), Injectable { viewModel.obtainProfile() - viewModel.profileData.observe(this, Observer> { profileRes -> + viewModel.profileData.observe(this) { profileRes -> when (profileRes) { is Success -> { val me = profileRes.data @@ -134,7 +135,7 @@ class EditProfileActivity : BaseActivity(), Injectable { lockedCheckBox.isChecked = me.locked accountFieldEditAdapter.setFields(me.source?.fields ?: emptyList()) - addFieldButton.isEnabled = me.source?.fields?.size ?: 0 < MAX_ACCOUNT_FIELDS + addFieldButton.isEnabled = me.source?.fields?.size ?: 0 < maxAccountFields if(viewModel.avatarData.value == null) { Glide.with(this) @@ -164,24 +165,34 @@ class EditProfileActivity : BaseActivity(), Injectable { } } - }) + } viewModel.obtainInstance() - viewModel.instanceData.observe(this, Observer> { result -> + viewModel.instanceData.observe(this) { result -> when (result) { is Success -> { val instance = result.data if (instance?.maxBioChars != null && instance.maxBioChars > 0) { noteEditTextLayout.counterMaxLength = instance.maxBioChars } + + instance?.pleroma?.metadata?.fieldsLimits?.let { + maxAccountFields = it.maxFields + + if(maxAccountFields > MASTODON_MAX_ACCOUNT_FIELDS + && accountFieldEditAdapter.itemCount == MASTODON_MAX_ACCOUNT_FIELDS + && !addFieldButton.isEnabled) { + addFieldButton.isEnabled = true + } + } } } - }) + } observeImage(viewModel.avatarData, avatarPreview, avatarProgressBar, true) observeImage(viewModel.headerData, headerPreview, headerProgressBar, false) - viewModel.saveData.observe(this, Observer> { + viewModel.saveData.observe(this) { when(it) { is Success -> { finish() @@ -193,7 +204,7 @@ class EditProfileActivity : BaseActivity(), Injectable { onSaveFailure(it.errorMessage) } } - }) + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Instance.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Instance.kt index 417129a3..f06d3ce7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Instance.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Instance.kt @@ -56,10 +56,15 @@ data class InstancePleroma ( ) data class InstancePleromaMetadata ( - val features: List + val features: List, + @SerializedName("fields_limits") val fieldsLimits: InstancePleromaMetadataFieldsLimits, +) + +data class InstancePleromaMetadataFieldsLimits( + @SerializedName("max_fields") val maxFields: Int, ) data class PollLimits ( - @SerializedName("max_options") val maxOptions: Int?, - @SerializedName("max_option_chars") val maxOptionChars: Int? + @SerializedName("max_options") val maxOptions: Int?, + @SerializedName("max_option_chars") val maxOptionChars: Int? )