PixelDroid-App-Android/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt

325 lines
12 KiB
Kotlin
Raw Normal View History

package com.h.pixeldroid.profile
import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import android.util.Log
import android.view.View
2020-11-02 21:58:01 +01:00
import android.widget.*
import androidx.annotation.StringRes
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.content.ContextCompat
2020-12-29 19:34:48 +01:00
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
2020-11-02 21:58:01 +01:00
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.h.pixeldroid.R
2021-01-13 22:46:00 +01:00
import com.h.pixeldroid.databinding.ActivityProfileBinding
2021-01-22 16:42:23 +01:00
import com.h.pixeldroid.databinding.FragmentProfileFeedBinding
import com.h.pixeldroid.databinding.FragmentProfilePostsBinding
import com.h.pixeldroid.posts.parseHTMLText
import com.h.pixeldroid.utils.BaseActivity
import com.h.pixeldroid.utils.ImageConverter
2021-01-13 22:46:00 +01:00
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.Account
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
import com.h.pixeldroid.utils.openUrl
2020-12-29 19:34:48 +01:00
import kotlinx.coroutines.launch
import retrofit2.Call
import retrofit2.Callback
2020-12-29 19:34:48 +01:00
import retrofit2.HttpException
import retrofit2.Response
2020-12-29 19:34:48 +01:00
import java.io.IOException
2020-12-11 16:53:12 +01:00
class ProfileActivity : BaseActivity() {
private lateinit var pixelfedAPI : PixelfedAPI
2021-01-22 16:42:23 +01:00
// private lateinit var adapter : ProfilePostsRecyclerViewAdapter
private lateinit var accessToken : String
private lateinit var domain : String
2021-01-22 16:42:23 +01:00
private var user: UserDatabaseEntity? = null
2021-01-22 16:42:23 +01:00
private var postsFragment = ProfileFeedFragment()
2021-01-22 16:42:23 +01:00
private lateinit var activityBinding: ActivityProfileBinding
private lateinit var feedFragmentBinding: FragmentProfileFeedBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-01-22 16:42:23 +01:00
activityBinding = ActivityProfileBinding.inflate(layoutInflater)
setContentView(activityBinding.root)
2021-01-13 22:46:00 +01:00
supportActionBar?.setDisplayHomeAsUpEnabled(true)
user = db.userDao().getActiveUser()
domain = user?.instance_uri.orEmpty()
pixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
accessToken = user?.accessToken.orEmpty()
// Set posts RecyclerView as a grid with 3 columns
2021-01-22 16:42:23 +01:00
feedFragmentBinding.profilePostsRecyclerView.layoutManager = GridLayoutManager(applicationContext, 3)
// adapter = ProfilePostsRecyclerViewAdapter()
// binding.profilePostsRecyclerView.adapter = adapter
2020-11-02 21:58:01 +01:00
// Set profile according to given account
val account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account?
setContent(account)
2021-01-22 16:42:23 +01:00
activityBinding.profileRefreshLayout.setOnRefreshListener {
2020-11-02 21:58:01 +01:00
getAndSetAccount(account?.id ?: user!!.user_id)
}
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
2020-11-02 21:58:01 +01:00
private fun setContent(account: Account?) {
2021-01-22 16:42:23 +01:00
if(account != null) {
2020-11-02 21:58:01 +01:00
setViews(account)
2021-01-22 16:42:23 +01:00
// setPosts(account)
startFragment(account)
} else {
2020-12-29 19:34:48 +01:00
lifecycleScope.launchWhenResumed {
val myAccount: Account = try {
pixelfedAPI.verifyCredentials("Bearer $accessToken")
} catch (exception: IOException) {
Log.e("ProfileActivity:", exception.toString())
return@launchWhenResumed showError()
} catch (exception: HttpException) {
return@launchWhenResumed showError()
}
setViews(myAccount)
// Populate profile page with user's posts
2021-01-22 16:42:23 +01:00
// setPosts(myAccount)
startFragment(myAccount)
2020-12-29 19:34:48 +01:00
}
}
//if we aren't viewing our own account, activate follow button
2020-11-02 21:58:01 +01:00
if(account != null && account.id != user?.user_id) activateFollow(account)
//if we *are* viewing our own account, activate the edit button
else activateEditButton()
// On click open followers list
2021-01-22 16:42:23 +01:00
activityBinding.nbFollowersTextView.setOnClickListener{ onClickFollowers(account) }
// On click open followers list
2021-01-22 16:42:23 +01:00
activityBinding.nbFollowingTextView.setOnClickListener{ onClickFollowing(account) }
2020-11-02 21:58:01 +01:00
}
private fun getAndSetAccount(id: String){
2020-12-29 22:14:32 +01:00
lifecycleScope.launchWhenCreated {
val account = try{
pixelfedAPI.getAccount("Bearer $accessToken", id)
} catch (exception: IOException) {
Log.e("ProfileActivity:", exception.toString())
return@launchWhenCreated showError()
} catch (exception: HttpException) {
return@launchWhenCreated showError()
}
setContent(account)
}
2020-11-02 21:58:01 +01:00
}
private fun showError(@StringRes errorText: Int = R.string.loading_toast, show: Boolean = true){
2021-01-22 16:42:23 +01:00
val motionLayout = activityBinding.motionLayout
2020-11-02 21:58:01 +01:00
if(show){
2021-01-13 22:46:00 +01:00
motionLayout.transitionToEnd()
2020-11-02 21:58:01 +01:00
} else {
2021-01-13 22:46:00 +01:00
motionLayout.transitionToStart()
2020-11-02 21:58:01 +01:00
}
2021-01-22 16:42:23 +01:00
activityBinding.profileProgressBar.visibility = View.GONE
activityBinding.profileRefreshLayout.isRefreshing = false
}
/**
2020-11-02 21:58:01 +01:00
* Populate profile page with user's data
*/
2020-11-02 21:58:01 +01:00
private fun setViews(account: Account) {
2021-01-22 16:42:23 +01:00
val profilePicture = activityBinding.profilePictureImageView
ImageConverter.setRoundImageFromURL(
View(applicationContext),
2020-11-02 21:58:01 +01:00
account.avatar,
profilePicture
)
2021-01-22 16:42:23 +01:00
activityBinding.descriptionTextView.text = parseHTMLText(
2020-11-02 21:58:01 +01:00
account.note ?: "", emptyList(), pixelfedAPI,
2020-12-29 22:14:32 +01:00
applicationContext, "Bearer $accessToken",
lifecycleScope
)
2020-11-02 21:58:01 +01:00
val displayName = account.getDisplayName()
2021-01-13 22:46:00 +01:00
2021-01-22 16:42:23 +01:00
activityBinding.accountNameTextView.text = displayName
2021-01-13 22:46:00 +01:00
supportActionBar?.title = displayName
2020-11-02 21:58:01 +01:00
if(displayName != "@${account.acct}"){
supportActionBar?.subtitle = "@${account.acct}"
}
2021-01-22 16:42:23 +01:00
activityBinding.nbPostsTextView.text = applicationContext.getString(R.string.nb_posts)
2020-11-02 21:58:01 +01:00
.format(account.statuses_count.toString())
2021-01-22 16:42:23 +01:00
activityBinding.nbFollowersTextView.text = applicationContext.getString(R.string.nb_followers)
2020-11-02 21:58:01 +01:00
.format(account.followers_count.toString())
2021-01-22 16:42:23 +01:00
activityBinding.nbFollowingTextView.text = applicationContext.getString(R.string.nb_following)
2020-11-02 21:58:01 +01:00
.format(account.following_count.toString())
}
2021-01-22 16:42:23 +01:00
private fun startFragment(account: Account) {
2021-01-22 16:42:23 +01:00
val arguments = Bundle()
arguments.putSerializable(Account.ACCOUNT_ID_TAG, account.id)
postsFragment.arguments = arguments
2021-01-22 16:42:23 +01:00
supportFragmentManager.beginTransaction().add(R.id.fragment_profile_feed, postsFragment).commit()
}
2021-01-22 16:42:23 +01:00
/**
* Populate profile page with user's posts
*/
// private fun setPosts(account: Account) {
// pixelfedAPI.accountPosts("Bearer $accessToken", account_id = account.id)
// .enqueue(object : Callback<List<Status>> {
//
// override fun onFailure(call: Call<List<Status>>, t: Throwable) {
// showError()
// Log.e("ProfileActivity.Posts:", t.toString())
// }
//
// override fun onResponse(
// call: Call<List<Status>>,
// response: Response<List<Status>>
// ) {
// if (response.code() == 200) {
// val statuses = response.body()!!
// adapter.addPosts(statuses)
// showError(show = false)
// } else {
// showError()
// }
// }
// })
// }
private fun onClickEditButton() {
val url = "$domain/settings/home"
if (!openUrl(url)) Log.e("ProfileActivity", "Cannot open this link")
}
2020-11-02 21:58:01 +01:00
private fun onClickFollowers(account: Account?) {
val intent = Intent(this, FollowsActivity::class.java)
intent.putExtra(Account.FOLLOWERS_TAG, true)
intent.putExtra(Account.ACCOUNT_TAG, account)
ContextCompat.startActivity(this, intent, null)
}
2020-11-02 21:58:01 +01:00
private fun onClickFollowing(account: Account?) {
val intent = Intent(this, FollowsActivity::class.java)
intent.putExtra(Account.FOLLOWERS_TAG, false)
intent.putExtra(Account.ACCOUNT_TAG, account)
ContextCompat.startActivity(this, intent, null)
}
private fun activateEditButton() {
// Edit button redirects to Pixelfed's "edit account" page
2021-01-22 16:42:23 +01:00
activityBinding.editButton.apply {
2021-01-13 22:46:00 +01:00
visibility = View.VISIBLE
setOnClickListener{ onClickEditButton() }
}
}
/**
* Set up follow button
*/
2020-11-02 21:58:01 +01:00
private fun activateFollow(account: Account) {
// Get relationship between the two users (credential and this) and set followButton accordingly
2020-12-29 19:34:48 +01:00
lifecycleScope.launch {
try {
val relationship = pixelfedAPI.checkRelationships(
"Bearer $accessToken", listOf(account.id.orEmpty())
).firstOrNull()
2020-12-29 19:34:48 +01:00
if(relationship != null){
if (relationship.following) {
setOnClickUnfollow(account)
} else {
2020-12-29 19:34:48 +01:00
setOnClickFollow(account)
}
2021-01-22 16:42:23 +01:00
activityBinding.followButton.visibility = View.VISIBLE
}
2020-12-29 19:34:48 +01:00
} catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString())
Toast.makeText(
applicationContext, getString(R.string.follow_status_failed),
Toast.LENGTH_SHORT
).show()
} catch (exception: HttpException) {
Toast.makeText(
applicationContext, getString(R.string.follow_button_failed),
Toast.LENGTH_SHORT
).show()
}
}
}
2020-11-02 21:58:01 +01:00
private fun setOnClickFollow(account: Account) {
2021-01-22 16:42:23 +01:00
activityBinding.followButton.apply {
2021-01-13 22:46:00 +01:00
setText(R.string.follow)
setOnClickListener {
lifecycleScope.launchWhenResumed {
try {
pixelfedAPI.follow(account.id.orEmpty(), "Bearer $accessToken")
setOnClickUnfollow(account)
} catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString())
Toast.makeText(
applicationContext, getString(R.string.follow_error),
Toast.LENGTH_SHORT
).show()
} catch (exception: HttpException) {
Toast.makeText(
applicationContext, getString(R.string.follow_error),
Toast.LENGTH_SHORT
).show()
}
2020-12-29 19:34:48 +01:00
}
}
}
}
2020-11-02 21:58:01 +01:00
private fun setOnClickUnfollow(account: Account) {
2021-01-22 16:42:23 +01:00
activityBinding.followButton.apply {
2021-01-13 22:46:00 +01:00
setText(R.string.unfollow)
2021-01-13 22:46:00 +01:00
setOnClickListener {
lifecycleScope.launchWhenResumed {
try {
pixelfedAPI.unfollow(account.id.orEmpty(), "Bearer $accessToken")
setOnClickFollow(account)
} catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString())
Toast.makeText(
applicationContext, getString(R.string.unfollow_error),
Toast.LENGTH_SHORT
).show()
} catch (exception: HttpException) {
Toast.makeText(
applicationContext, getString(R.string.unfollow_error),
Toast.LENGTH_SHORT
).show()
}
2020-12-29 19:34:48 +01:00
}
}
}
}
}