2020-12-26 12:10:54 +01:00
|
|
|
package com.h.pixeldroid.profile
|
2020-04-09 22:36:59 +02:00
|
|
|
|
2020-05-01 11:26:18 +02:00
|
|
|
import android.content.Intent
|
|
|
|
import android.graphics.Typeface
|
2020-04-09 22:36:59 +02:00
|
|
|
import android.os.Bundle
|
2020-05-01 11:26:18 +02:00
|
|
|
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
|
2020-05-01 11:26:18 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2020-12-29 19:34:48 +01:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2020-05-01 11:26:18 +02:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2020-11-02 21:58:01 +01:00
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
2020-12-26 12:10:54 +01:00
|
|
|
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
|
2020-12-26 12:10:54 +01:00
|
|
|
import com.h.pixeldroid.posts.parseHTMLText
|
|
|
|
import com.h.pixeldroid.utils.BaseActivity
|
2020-05-01 11:26:18 +02:00
|
|
|
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
|
2020-12-28 17:18:07 +01:00
|
|
|
import com.h.pixeldroid.utils.openUrl
|
2020-12-29 19:34:48 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2020-05-01 11:26:18 +02:00
|
|
|
import retrofit2.Call
|
|
|
|
import retrofit2.Callback
|
2020-12-29 19:34:48 +01:00
|
|
|
import retrofit2.HttpException
|
2020-05-01 11:26:18 +02:00
|
|
|
import retrofit2.Response
|
2020-12-29 19:34:48 +01:00
|
|
|
import java.io.IOException
|
2020-04-09 22:36:59 +02:00
|
|
|
|
2020-12-11 16:53:12 +01:00
|
|
|
class ProfileActivity : BaseActivity() {
|
2020-05-01 11:26:18 +02:00
|
|
|
private lateinit var pixelfedAPI : PixelfedAPI
|
2021-01-22 16:42:23 +01:00
|
|
|
// private lateinit var adapter : ProfilePostsRecyclerViewAdapter
|
2020-05-19 09:49:34 +02:00
|
|
|
private lateinit var accessToken : String
|
|
|
|
private lateinit var domain : String
|
2021-01-22 16:42:23 +01:00
|
|
|
|
2020-10-31 22:11:01 +01:00
|
|
|
private var user: UserDatabaseEntity? = null
|
2021-01-22 16:42:23 +01:00
|
|
|
private var postsFragment = ProfileFeedFragment()
|
2020-10-31 22:11:01 +01:00
|
|
|
|
2021-01-22 16:42:23 +01:00
|
|
|
private lateinit var activityBinding: ActivityProfileBinding
|
|
|
|
private lateinit var feedFragmentBinding: FragmentProfileFeedBinding
|
2020-04-09 22:36:59 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-09-26 00:25:08 +02:00
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
2020-04-09 22:36:59 +02:00
|
|
|
|
2020-10-31 22:11:01 +01:00
|
|
|
user = db.userDao().getActiveUser()
|
2020-05-19 09:49:34 +02:00
|
|
|
|
|
|
|
domain = user?.instance_uri.orEmpty()
|
2020-07-26 20:56:01 +02:00
|
|
|
pixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
|
2020-05-19 09:49:34 +02:00
|
|
|
accessToken = user?.accessToken.orEmpty()
|
2020-05-01 11:26:18 +02:00
|
|
|
|
|
|
|
// 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-05-01 11:26:18 +02:00
|
|
|
|
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)
|
|
|
|
}
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 00:25:08 +02:00
|
|
|
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)
|
2020-10-31 22:11:01 +01:00
|
|
|
} 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
|
|
|
}
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 22:11:01 +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)
|
2020-10-31 22:11:01 +01:00
|
|
|
//if we *are* viewing our own account, activate the edit button
|
|
|
|
else activateEditButton()
|
|
|
|
|
|
|
|
|
2020-05-01 11:26:18 +02:00
|
|
|
// On click open followers list
|
2021-01-22 16:42:23 +01:00
|
|
|
activityBinding.nbFollowersTextView.setOnClickListener{ onClickFollowers(account) }
|
2020-05-01 11:26:18 +02:00
|
|
|
// 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-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-02 21:58:01 +01:00
|
|
|
* Populate profile page with user's data
|
2020-05-01 11:26:18 +02:00
|
|
|
*/
|
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
|
2020-09-26 00:25:08 +02:00
|
|
|
ImageConverter.setRoundImageFromURL(
|
|
|
|
View(applicationContext),
|
2020-11-02 21:58:01 +01:00
|
|
|
account.avatar,
|
2020-09-26 00:25:08 +02:00
|
|
|
profilePicture
|
|
|
|
)
|
2020-05-01 11:26:18 +02:00
|
|
|
|
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-09-26 00:25:08 +02:00
|
|
|
)
|
2020-05-01 11:26:18 +02:00
|
|
|
|
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
|
|
|
|
2020-11-01 18:44:32 +01:00
|
|
|
supportActionBar?.title = displayName
|
2020-11-02 21:58:01 +01:00
|
|
|
if(displayName != "@${account.acct}"){
|
|
|
|
supportActionBar?.subtitle = "@${account.acct}"
|
2020-09-26 00:25:08 +02:00
|
|
|
}
|
|
|
|
|
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())
|
2020-05-01 11:26:18 +02:00
|
|
|
|
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())
|
2020-05-01 11:26:18 +02:00
|
|
|
|
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())
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 16:42:23 +01:00
|
|
|
private fun startFragment(account: Account) {
|
2020-05-01 11:26:18 +02:00
|
|
|
|
2021-01-22 16:42:23 +01:00
|
|
|
val arguments = Bundle()
|
|
|
|
arguments.putSerializable(Account.ACCOUNT_ID_TAG, account.id)
|
|
|
|
postsFragment.arguments = arguments
|
2020-05-01 11:26:18 +02:00
|
|
|
|
2021-01-22 16:42:23 +01:00
|
|
|
supportFragmentManager.beginTransaction().add(R.id.fragment_profile_feed, postsFragment).commit()
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
2020-05-01 11:26:18 +02:00
|
|
|
private fun onClickEditButton() {
|
2020-05-19 09:49:34 +02:00
|
|
|
val url = "$domain/settings/home"
|
2020-05-01 11:26:18 +02:00
|
|
|
|
2020-12-28 17:18:07 +01:00
|
|
|
if (!openUrl(url)) Log.e("ProfileActivity", "Cannot open this link")
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 21:58:01 +01:00
|
|
|
private fun onClickFollowers(account: Account?) {
|
2020-05-01 11:26:18 +02:00
|
|
|
val intent = Intent(this, FollowsActivity::class.java)
|
2020-09-26 00:25:08 +02:00
|
|
|
intent.putExtra(Account.FOLLOWERS_TAG, true)
|
2020-10-31 22:11:01 +01:00
|
|
|
intent.putExtra(Account.ACCOUNT_TAG, account)
|
2020-05-01 11:26:18 +02:00
|
|
|
|
|
|
|
ContextCompat.startActivity(this, intent, null)
|
|
|
|
}
|
|
|
|
|
2020-11-02 21:58:01 +01:00
|
|
|
private fun onClickFollowing(account: Account?) {
|
2020-05-01 11:26:18 +02:00
|
|
|
val intent = Intent(this, FollowsActivity::class.java)
|
2020-09-26 00:25:08 +02:00
|
|
|
intent.putExtra(Account.FOLLOWERS_TAG, false)
|
|
|
|
intent.putExtra(Account.ACCOUNT_TAG, account)
|
2020-05-01 11:26:18 +02:00
|
|
|
|
|
|
|
ContextCompat.startActivity(this, intent, null)
|
|
|
|
}
|
|
|
|
|
2020-10-31 22:11:01 +01:00
|
|
|
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() }
|
|
|
|
}
|
2020-10-31 22:11:01 +01:00
|
|
|
}
|
|
|
|
|
2020-05-01 11:26:18 +02:00
|
|
|
/**
|
|
|
|
* Set up follow button
|
|
|
|
*/
|
2020-11-02 21:58:01 +01:00
|
|
|
private fun activateFollow(account: Account) {
|
2020-05-01 11:26:18 +02:00
|
|
|
// 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-05-01 11:26:18 +02:00
|
|
|
|
2020-12-29 19:34:48 +01:00
|
|
|
if(relationship != null){
|
|
|
|
if (relationship.following) {
|
|
|
|
setOnClickUnfollow(account)
|
2020-09-26 00:25:08 +02:00
|
|
|
} else {
|
2020-12-29 19:34:48 +01:00
|
|
|
setOnClickFollow(account)
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
2021-01-22 16:42:23 +01:00
|
|
|
activityBinding.followButton.visibility = View.VISIBLE
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
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-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
|
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-05-01 11:26:18 +02: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)
|
2020-05-01 11:26:18 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
2020-04-09 22:36:59 +02:00
|
|
|
}
|
2020-04-10 12:55:02 +02:00
|
|
|
}
|