diff --git a/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt b/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt index 4979e4d1..357a80c8 100644 --- a/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt +++ b/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt @@ -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 @@ -37,7 +38,6 @@ import com.h.pixeldroid.utils.api.objects.Status import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity import com.h.pixeldroid.utils.openUrl import kotlinx.coroutines.Job -import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import retrofit2.HttpException import java.io.IOException @@ -240,8 +240,8 @@ class ProfileActivity : BaseActivity() { ).firstOrNull() if(relationship != null){ - if (relationship.following) { - setOnClickUnfollow(account) + if (relationship.following == true || relationship.requested == true) { + setOnClickUnfollow(account, relationship.requested == true) } else { setOnClickFollow(account) } @@ -268,8 +268,9 @@ class ProfileActivity : BaseActivity() { setOnClickListener { lifecycleScope.launchWhenResumed { try { - pixelfedAPI.follow(account.id.orEmpty(), "Bearer $accessToken") - setOnClickUnfollow(account) + val rel = pixelfedAPI.follow(account.id.orEmpty(), "Bearer $accessToken") + if(rel.following == true) setOnClickUnfollow(account, rel.requested == true) + else setOnClickFollow(account) } catch (exception: IOException) { Log.e("FOLLOW ERROR", exception.toString()) Toast.makeText( @@ -287,29 +288,45 @@ class ProfileActivity : BaseActivity() { } } - private fun setOnClickUnfollow(account: Account) { + private fun setOnClickUnfollow(account: Account, requested: Boolean) { binding.followButton.apply { - setText(R.string.unfollow) + if(account.locked == true && requested) { + setText(R.string.follow_requested) + } else setText(R.string.unfollow) - setOnClickListener { + + fun unfollow() { lifecycleScope.launchWhenResumed { try { - pixelfedAPI.unfollow(account.id.orEmpty(), "Bearer $accessToken") - setOnClickFollow(account) + 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() + } } } } diff --git a/app/src/main/java/com/h/pixeldroid/utils/api/objects/Relationship.kt b/app/src/main/java/com/h/pixeldroid/utils/api/objects/Relationship.kt index 82e4f951..e13d6449 100644 --- a/app/src/main/java/com/h/pixeldroid/utils/api/objects/Relationship.kt +++ b/app/src/main/java/com/h/pixeldroid/utils/api/objects/Relationship.kt @@ -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 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 35dbea71..9e01748b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -152,6 +152,8 @@ For more info about Pixelfed, you can check here: https://pixelfed.org" No Username Follow Unfollow + Follow Requested + Cancel the follow request? Edit profile Search