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

418 lines
15 KiB
Kotlin
Raw Normal View History

package com.h.pixeldroid.profile
import android.content.Intent
import android.os.Bundle
import android.util.Log
2021-02-08 15:07:23 +01:00
import android.view.LayoutInflater
import android.view.View
2021-02-08 15:07:23 +01:00
import android.view.ViewGroup
2020-11-02 21:58:01 +01:00
import android.widget.*
import androidx.appcompat.app.AlertDialog
2021-02-08 15:07:23 +01:00
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
2021-02-08 15:07:23 +01:00
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
2020-12-29 19:34:48 +01:00
import androidx.lifecycle.lifecycleScope
2021-02-08 15:07:23 +01:00
import androidx.paging.ExperimentalPagingApi
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
2021-02-08 15:07:23 +01:00
import androidx.recyclerview.widget.RecyclerView
2021-03-26 17:51:42 +01:00
import com.google.android.material.snackbar.Snackbar
import com.h.pixeldroid.R
2021-01-13 22:46:00 +01:00
import com.h.pixeldroid.databinding.ActivityProfileBinding
2021-02-08 15:07:23 +01:00
import com.h.pixeldroid.databinding.FragmentProfilePostsBinding
import com.h.pixeldroid.posts.PostActivity
2021-03-19 17:28:13 +01:00
import com.h.pixeldroid.posts.feeds.initAdapter
2021-03-26 10:42:51 +01:00
import com.h.pixeldroid.posts.feeds.launch
2021-02-08 15:07:23 +01:00
import com.h.pixeldroid.posts.feeds.uncachedFeeds.FeedViewModel
import com.h.pixeldroid.posts.feeds.uncachedFeeds.UncachedContentRepository
import com.h.pixeldroid.posts.feeds.uncachedFeeds.profile.ProfileContentRepository
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
2021-03-26 10:42:51 +01:00
import com.h.pixeldroid.utils.api.objects.FeedContent
2021-01-13 22:46:00 +01:00
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
import com.h.pixeldroid.utils.openUrl
2021-02-08 15:07:23 +01:00
import kotlinx.coroutines.Job
2020-12-29 19:34:48 +01:00
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException
2020-12-11 16:53:12 +01:00
class ProfileActivity : BaseActivity() {
2021-01-22 19:25:57 +01:00
private lateinit var pixelfedAPI : PixelfedAPI
private lateinit var accessToken : String
private lateinit var domain : String
2021-02-08 15:07:23 +01:00
private lateinit var accountId : String
2021-01-13 22:46:00 +01:00
private lateinit var binding: ActivityProfileBinding
2021-02-08 15:07:23 +01:00
private lateinit var profileAdapter: PagingDataAdapter<Status, RecyclerView.ViewHolder>
private lateinit var viewModel: FeedViewModel<Status>
2021-02-26 11:21:04 +01:00
private var user: UserDatabaseEntity? = null
2021-02-08 15:07:23 +01:00
private var job: Job? = null
2021-02-08 15:07:23 +01:00
@ExperimentalPagingApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-01-13 22:46:00 +01:00
binding = ActivityProfileBinding.inflate(layoutInflater)
setContentView(binding.root)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
user = db.userDao().getActiveUser()
domain = user?.instance_uri.orEmpty()
pixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
accessToken = user?.accessToken.orEmpty()
2020-11-02 21:58:01 +01:00
// Set profile according to given account
val account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account?
2021-02-08 15:07:23 +01:00
accountId = account?.id ?: user!!.user_id
2020-11-02 21:58:01 +01:00
2021-02-08 15:07:23 +01:00
// get the view model
@Suppress("UNCHECKED_CAST")
viewModel = ViewModelProvider(this, ProfileViewModelFactory(
ProfileContentRepository(
apiHolder.setDomainToCurrentUser(db),
db.userDao().getActiveUser()!!.accessToken,
accountId
)
)
).get(FeedViewModel::class.java) as FeedViewModel<Status>
2021-02-26 11:21:04 +01:00
profileAdapter = ProfilePostsAdapter()
2021-03-19 17:28:13 +01:00
initAdapter(binding.profileProgressBar, binding.profileRefreshLayout,
binding.profilePostsRecyclerView, binding.motionLayout, binding.profileErrorLayout,
profileAdapter)
2021-02-08 15:07:23 +01:00
2021-02-26 11:21:04 +01:00
binding.profilePostsRecyclerView.layoutManager = GridLayoutManager(this, 3)
2020-11-02 21:58:01 +01:00
2021-02-26 11:21:04 +01:00
binding.profileRefreshLayout.setOnRefreshListener {
2021-02-08 15:07:23 +01:00
profileAdapter.refresh()
2020-11-02 21:58:01 +01:00
}
setContent(account)
2021-03-26 10:42:51 +01:00
@Suppress("UNCHECKED_CAST")
job = launch(job, lifecycleScope, viewModel as FeedViewModel<FeedContent>,
profileAdapter as PagingDataAdapter<FeedContent, RecyclerView.ViewHolder>)
2021-02-08 15:07:23 +01:00
}
2020-11-02 21:58:01 +01:00
2021-02-08 15:07:23 +01:00
/**
* Shows or hides the error in the different FeedFragments
*/
private fun showError(errorText: String = "Something went wrong while loading", show: Boolean = true){
if(show){
2021-02-26 11:21:04 +01:00
binding.profileProgressBar.visibility = View.GONE
binding.motionLayout.transitionToEnd()
} else if(binding.motionLayout.progress == 1F) {
binding.motionLayout.transitionToStart()
2020-11-02 21:58:01 +01:00
}
2021-02-26 11:21:04 +01:00
binding.profileRefreshLayout.isRefreshing = false
}
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)
} else {
2020-12-29 19:34:48 +01:00
lifecycleScope.launchWhenResumed {
val myAccount: Account = try {
pixelfedAPI.verifyCredentials()
2020-12-29 19:34:48 +01:00
} catch (exception: IOException) {
Log.e("ProfileActivity:", exception.toString())
return@launchWhenResumed showError()
} catch (exception: HttpException) {
return@launchWhenResumed showError()
}
setViews(myAccount)
}
}
2021-02-26 11:21:04 +01:00
if(account != null && account.id != user?.user_id) {
//if we aren't viewing our own account, activate follow button
activateFollow(account)
} else {
//if we *are* viewing our own account, activate the edit button
activateEditButton()
}
// On click open followers list
2021-01-13 22:46:00 +01:00
binding.nbFollowersTextView.setOnClickListener{ onClickFollowers(account) }
// On click open followers list
2021-01-13 22:46:00 +01:00
binding.nbFollowingTextView.setOnClickListener{ onClickFollowing(account) }
2020-11-02 21:58:01 +01:00
}
/**
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-13 22:46:00 +01:00
val profilePicture = binding.profilePictureImageView
ImageConverter.setRoundImageFromURL(
View(applicationContext),
2020-11-02 21:58:01 +01:00
account.avatar,
profilePicture
)
2021-01-13 22:46:00 +01:00
binding.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
binding.accountNameTextView.text = displayName
supportActionBar?.title = displayName
2021-02-26 11:21:04 +01:00
if(displayName != "@${account.acct}") {
2020-11-02 21:58:01 +01:00
supportActionBar?.subtitle = "@${account.acct}"
}
2021-01-25 11:52:27 +01:00
binding.nbPostsTextView.text = resources.getQuantityString(
R.plurals.nb_posts,
account.statuses_count ?: 0,
account.statuses_count ?: 0
)
2021-01-25 11:52:27 +01:00
binding.nbFollowersTextView.text = resources.getQuantityString(
R.plurals.nb_followers,
account.followers_count ?: 0,
account.followers_count ?: 0
)
2021-01-25 11:52:27 +01:00
binding.nbFollowingTextView.text = resources.getQuantityString(
R.plurals.nb_following,
account.following_count ?: 0,
account.following_count ?: 0
)
}
private fun onClickEditButton() {
val url = "$domain/settings/home"
2021-02-26 11:21:04 +01:00
if(!openUrl(url)) {
2021-03-26 17:51:42 +01:00
Snackbar.make(binding.root, getString(R.string.edit_link_failed),
Snackbar.LENGTH_LONG).show()
2021-02-26 11:21:04 +01:00
}
}
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-13 22:46:00 +01:00
binding.editButton.apply {
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(
listOf(account.id.orEmpty())
2020-12-29 19:34:48 +01:00
).firstOrNull()
2020-12-29 19:34:48 +01:00
if(relationship != null){
if (relationship.following == true || relationship.requested == true) {
setOnClickUnfollow(account, relationship.requested == true)
} else {
setOnClickFollow(account)
}
2021-01-13 22:46:00 +01:00
binding.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()
}
}
}
private fun setOnClickFollow(account: Account) {
2021-01-13 22:46:00 +01:00
binding.followButton.apply {
setText(R.string.follow)
2021-01-13 22:46:00 +01:00
setOnClickListener {
lifecycleScope.launchWhenResumed {
try {
val rel = pixelfedAPI.follow(account.id.orEmpty())
if(rel.following == true) setOnClickUnfollow(account, rel.requested == true)
else setOnClickFollow(account)
2021-01-13 22:46:00 +01:00
} 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
}
}
}
}
private fun setOnClickUnfollow(account: Account, requested: Boolean) {
2021-01-13 22:46:00 +01:00
binding.followButton.apply {
if(account.locked == true && requested) {
2021-03-26 19:54:04 +01:00
setText(R.string.follow_requested)
} else setText(R.string.unfollow)
fun unfollow() {
2021-01-13 22:46:00 +01:00
lifecycleScope.launchWhenResumed {
try {
val rel = pixelfedAPI.unfollow(account.id.orEmpty())
if(rel.following == false && rel.requested == false) setOnClickFollow(account)
else setOnClickUnfollow(account, rel.requested == true)
2021-01-13 22:46:00 +01:00
} catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString())
Toast.makeText(
applicationContext, getString(R.string.unfollow_error),
Toast.LENGTH_SHORT
2021-01-13 22:46:00 +01:00
).show()
} catch (exception: HttpException) {
Toast.makeText(
applicationContext, getString(R.string.unfollow_error),
Toast.LENGTH_SHORT
2021-01-13 22:46:00 +01:00
).show()
}
2020-12-29 19:34:48 +01:00
}
}
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()
}
}
}
2021-02-08 15:07:23 +01:00
}
class ProfileViewModelFactory @ExperimentalPagingApi constructor(
private val searchContentRepository: UncachedContentRepository<Status>
) : ViewModelProvider.Factory {
@ExperimentalPagingApi
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(FeedViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return FeedViewModel(searchContentRepository) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
class ProfilePostsViewHolder(binding: FragmentProfilePostsBinding) : RecyclerView.ViewHolder(binding.root) {
private val postPreview: ImageView = binding.postPreview
private val albumIcon: ImageView = binding.albumIcon
fun bind(post: Status) {
if(post.sensitive!!) {
ImageConverter.setSquareImageFromDrawable(
itemView,
AppCompatResources.getDrawable(itemView.context, R.drawable.ic_sensitive),
postPreview
)
} else {
ImageConverter.setSquareImageFromURL(itemView, post.getPostPreviewURL(), postPreview)
}
if(post.media_attachments?.size ?: 0 > 1) {
albumIcon.visibility = View.VISIBLE
} else {
albumIcon.visibility = View.GONE
}
postPreview.setOnClickListener {
val intent = Intent(postPreview.context, PostActivity::class.java)
intent.putExtra(Status.POST_TAG, post)
postPreview.context.startActivity(intent)
}
}
companion object {
fun create(parent: ViewGroup): ProfilePostsViewHolder {
val itemBinding = FragmentProfilePostsBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
return ProfilePostsViewHolder(itemBinding)
}
}
}
class ProfilePostsAdapter : PagingDataAdapter<Status, RecyclerView.ViewHolder>(
UIMODEL_COMPARATOR
) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return ProfilePostsViewHolder.create(parent)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val post = getItem(position)
post?.let {
(holder as ProfilePostsViewHolder).bind(it)
}
}
companion object {
private val UIMODEL_COMPARATOR = object : DiffUtil.ItemCallback<Status>() {
override fun areItemsTheSame(oldItem: Status, newItem: Status): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Status, newItem: Status): Boolean =
oldItem.content == newItem.content
}
}
}