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.ViewGroup
import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel
@ -239,10 +240,10 @@ class ProfileActivity : BaseActivity() {
).firstOrNull()
if(relationship != null){
if (relationship.following) {
setOnClickUnfollow(account, true)
if (relationship.following == true || relationship.requested == true) {
setOnClickUnfollow(account, relationship.requested == true)
} else {
setOnClickFollow(account, relationship.requested)
setOnClickFollow(account)
}
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 {
if(account.locked == true && requested) {
setText(R.string.follow_requested)
isEnabled = false
return
} else setText(R.string.follow)
setText(R.string.follow)
setOnClickListener {
lifecycleScope.launchWhenResumed {
try {
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) {
Log.e("FOLLOW ERROR", exception.toString())
Toast.makeText(
@ -290,33 +288,45 @@ class ProfileActivity : BaseActivity() {
}
}
private fun setOnClickUnfollow(account: Account, follow: Boolean = false) {
private fun setOnClickUnfollow(account: Account, requested: Boolean) {
binding.followButton.apply {
if(account.locked == true && !follow) {
if(account.locked == true && requested) {
setText(R.string.follow_requested)
isEnabled = false
return
} else setText(R.string.unfollow)
setOnClickListener {
fun unfollow() {
lifecycleScope.launchWhenResumed {
try {
pixelfedAPI.unfollow(account.id.orEmpty(), "Bearer $accessToken")
setOnClickFollow(account, false)
val rel = pixelfedAPI.unfollow(account.id.orEmpty(), "Bearer $accessToken")
if(rel.following == false && rel.requested == false) setOnClickFollow(account)
else setOnClickUnfollow(account, rel.requested == true)
} catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString())
Toast.makeText(
applicationContext, getString(R.string.unfollow_error),
Toast.LENGTH_SHORT
applicationContext, getString(R.string.unfollow_error),
Toast.LENGTH_SHORT
).show()
} catch (exception: HttpException) {
Toast.makeText(
applicationContext, getString(R.string.unfollow_error),
Toast.LENGTH_SHORT
applicationContext, getString(R.string.unfollow_error),
Toast.LENGTH_SHORT
).show()
}
}
}
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(
// Required atributes
val id: String,
val following: Boolean,
val requested: Boolean,
val endorsed: Boolean,
val followed_by: Boolean,
val muting: Boolean,
val muting_notifications: Boolean,
val showing_reblogs: Boolean,
val blocking: Boolean,
val domain_blocking: Boolean,
val blocked_by: Boolean
val following: Boolean?,
val requested: Boolean?,
val endorsed: Boolean?,
val followed_by: Boolean?,
val muting: Boolean?,
val muting_notifications: Boolean?,
val showing_reblogs: Boolean?,
val blocking: Boolean?,
val domain_blocking: Boolean?,
val blocked_by: Boolean?
) : 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="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="dialog_message_cancel_follow_request">Cancel the follow request?</string>
</resources>