make json fields nullable, correct request logic

This commit is contained in:
Matthieu 2021-03-27 15:24:58 +01:00
parent 8dacdc1970
commit 9f9d0e23bc
3 changed files with 42 additions and 31 deletions

View File

@ -7,6 +7,7 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.* import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.content.res.AppCompatResources import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
@ -239,10 +240,10 @@ class ProfileActivity : BaseActivity() {
).firstOrNull() ).firstOrNull()
if(relationship != null){ if(relationship != null){
if (relationship.following) { if (relationship.following == true || relationship.requested == true) {
setOnClickUnfollow(account, true) setOnClickUnfollow(account, relationship.requested == true)
} else { } else {
setOnClickFollow(account, relationship.requested) setOnClickFollow(account)
} }
binding.followButton.visibility = View.VISIBLE binding.followButton.visibility = View.VISIBLE
} }
@ -261,18 +262,15 @@ class ProfileActivity : BaseActivity() {
} }
} }
private fun setOnClickFollow(account: Account, requested: Boolean) { private fun setOnClickFollow(account: Account) {
binding.followButton.apply { binding.followButton.apply {
if(account.locked == true && requested) { setText(R.string.follow)
setText(R.string.follow_requested)
isEnabled = false
return
} else setText(R.string.follow)
setOnClickListener { setOnClickListener {
lifecycleScope.launchWhenResumed { lifecycleScope.launchWhenResumed {
try { try {
val rel = pixelfedAPI.follow(account.id.orEmpty(), "Bearer $accessToken") val rel = pixelfedAPI.follow(account.id.orEmpty(), "Bearer $accessToken")
setOnClickUnfollow(account) if(rel.following == true) setOnClickUnfollow(account, rel.requested == true)
else setOnClickFollow(account)
} catch (exception: IOException) { } catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString()) Log.e("FOLLOW ERROR", exception.toString())
Toast.makeText( Toast.makeText(
@ -290,19 +288,19 @@ class ProfileActivity : BaseActivity() {
} }
} }
private fun setOnClickUnfollow(account: Account, follow: Boolean = false) { private fun setOnClickUnfollow(account: Account, requested: Boolean) {
binding.followButton.apply { binding.followButton.apply {
if(account.locked == true && !follow) { if(account.locked == true && requested) {
setText(R.string.follow_requested) setText(R.string.follow_requested)
isEnabled = false
return
} else setText(R.string.unfollow) } else setText(R.string.unfollow)
setOnClickListener {
fun unfollow() {
lifecycleScope.launchWhenResumed { lifecycleScope.launchWhenResumed {
try { try {
pixelfedAPI.unfollow(account.id.orEmpty(), "Bearer $accessToken") val rel = pixelfedAPI.unfollow(account.id.orEmpty(), "Bearer $accessToken")
setOnClickFollow(account, false) if(rel.following == false && rel.requested == false) setOnClickFollow(account)
else setOnClickUnfollow(account, rel.requested == true)
} catch (exception: IOException) { } catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString()) Log.e("FOLLOW ERROR", exception.toString())
Toast.makeText( Toast.makeText(
@ -317,6 +315,18 @@ class ProfileActivity : BaseActivity() {
} }
} }
} }
setOnClickListener {
if(account.locked == true && requested){
AlertDialog.Builder(context)
.setMessage(R.string.dialog_message_cancel_follow_request)
.setPositiveButton(android.R.string.ok) { _, _ ->
unfollow()
}
.setNegativeButton(android.R.string.cancel){_, _ -> }
.show()
} else unfollow()
}
} }
} }
} }

View File

@ -5,14 +5,14 @@ import java.io.Serializable
data class Relationship( data class Relationship(
// Required atributes // Required atributes
val id: String, val id: String,
val following: Boolean, val following: Boolean?,
val requested: Boolean, val requested: Boolean?,
val endorsed: Boolean, val endorsed: Boolean?,
val followed_by: Boolean, val followed_by: Boolean?,
val muting: Boolean, val muting: Boolean?,
val muting_notifications: Boolean, val muting_notifications: Boolean?,
val showing_reblogs: Boolean, val showing_reblogs: Boolean?,
val blocking: Boolean, val blocking: Boolean?,
val domain_blocking: Boolean, val domain_blocking: Boolean?,
val blocked_by: Boolean val blocked_by: Boolean?
) : Serializable ) : Serializable

View File

@ -199,4 +199,5 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
<string name="mascot_description">Image showing a red panda, Pixelfed\'s mascot, using a phone</string> <string name="mascot_description">Image showing a red panda, Pixelfed\'s mascot, using a phone</string>
<string name="delete_post_failed_error">Could not delete the post, error %1$d</string> <string name="delete_post_failed_error">Could not delete the post, error %1$d</string>
<string name="delete_post_failed_io_except">Could not delete the post, check your connection?</string> <string name="delete_post_failed_io_except">Could not delete the post, check your connection?</string>
<string name="dialog_message_cancel_follow_request">Cancel the follow request?</string>
</resources> </resources>