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
|
2020-04-09 22:36:59 +02:00
|
|
|
import android.os.Bundle
|
2020-05-01 11:26:18 +02:00
|
|
|
import android.util.Log
|
2021-02-08 15:07:23 +01:00
|
|
|
import android.view.LayoutInflater
|
2020-05-01 11:26:18 +02:00
|
|
|
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.*
|
2021-02-08 15:07:23 +01:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources
|
2020-05-01 11:26:18 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
2021-02-08 15:07:23 +01:00
|
|
|
import androidx.core.view.isVisible
|
|
|
|
import androidx.core.view.size
|
|
|
|
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.LoadState
|
|
|
|
import androidx.paging.PagingDataAdapter
|
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
2020-05-01 11:26:18 +02:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
2021-02-08 15:07:23 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
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-02-08 15:07:23 +01:00
|
|
|
import com.h.pixeldroid.databinding.FragmentProfilePostsBinding
|
|
|
|
import com.h.pixeldroid.posts.PostActivity
|
|
|
|
import com.h.pixeldroid.posts.feeds.ReposLoadStateAdapter
|
|
|
|
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
|
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
|
2021-02-08 15:07:23 +01:00
|
|
|
import com.h.pixeldroid.utils.api.objects.Status
|
2021-01-13 22:46:00 +01:00
|
|
|
import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
|
2020-12-28 17:18:07 +01:00
|
|
|
import com.h.pixeldroid.utils.openUrl
|
2021-02-08 15:07:23 +01:00
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import kotlinx.coroutines.flow.collect
|
|
|
|
import kotlinx.coroutines.flow.collectLatest
|
|
|
|
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
|
|
|
import kotlinx.coroutines.flow.filter
|
2020-12-29 19:34:48 +01:00
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import retrofit2.HttpException
|
|
|
|
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 19:25:57 +01:00
|
|
|
|
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
|
|
|
|
2021-02-08 15:07:23 +01:00
|
|
|
private lateinit var accountId : String
|
|
|
|
|
2020-10-31 22:11:01 +01:00
|
|
|
private var user: UserDatabaseEntity? = null
|
|
|
|
|
2021-01-22 16:42:23 +01:00
|
|
|
private lateinit var activityBinding: ActivityProfileBinding
|
2020-04-09 22:36:59 +02:00
|
|
|
|
2021-02-08 15:07:23 +01:00
|
|
|
private lateinit var profileAdapter: PagingDataAdapter<Status, RecyclerView.ViewHolder>
|
|
|
|
private lateinit var viewModel: FeedViewModel<Status>
|
|
|
|
private var job: Job? = null
|
|
|
|
|
|
|
|
@ExperimentalPagingApi
|
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
|
|
|
|
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
|
|
|
|
|
|
|
setContent(account)
|
2021-02-08 15:07:23 +01:00
|
|
|
|
|
|
|
profileAdapter = ProfilePostsAdapter()
|
|
|
|
|
|
|
|
initAdapter(activityBinding, profileAdapter)
|
|
|
|
|
|
|
|
// 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>
|
|
|
|
|
|
|
|
activityBinding.profilePostsRecyclerView.layoutManager = GridLayoutManager(this, 3)
|
|
|
|
|
|
|
|
profileLaunch()
|
|
|
|
profileInitSearch()
|
2020-11-02 21:58:01 +01:00
|
|
|
|
2021-01-22 16:42:23 +01:00
|
|
|
activityBinding.profileRefreshLayout.setOnRefreshListener {
|
2021-02-08 15:07:23 +01:00
|
|
|
//It shouldn't be necessary to also retry() in addition to refresh(),
|
|
|
|
//but if we don't do this, reloads after an error fail immediately...
|
|
|
|
profileAdapter.retry()
|
|
|
|
profileAdapter.refresh()
|
2020-11-02 21:58:01 +01:00
|
|
|
}
|
2020-05-01 11:26:18 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 15:07:23 +01:00
|
|
|
|
|
|
|
private fun profileLaunch() {
|
|
|
|
// Make sure we cancel the previous job before creating a new one
|
|
|
|
job?.cancel()
|
|
|
|
job = lifecycleScope.launch {
|
|
|
|
viewModel.flow().collectLatest {
|
|
|
|
profileAdapter.submitData(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun profileInitSearch() {
|
|
|
|
// Scroll to top when the list is refreshed from network.
|
|
|
|
lifecycleScope.launch {
|
|
|
|
profileAdapter.loadStateFlow
|
|
|
|
// Only emit when REFRESH LoadState for RemoteMediator changes.
|
|
|
|
.distinctUntilChangedBy { it.refresh }
|
|
|
|
// Only react to cases where Remote REFRESH completes i.e., NotLoading.
|
|
|
|
.filter { it.refresh is LoadState.NotLoading }
|
|
|
|
.collect { activityBinding.profilePostsRecyclerView.scrollToPosition(0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows or hides the error in the different FeedFragments
|
|
|
|
*/
|
|
|
|
private fun showError(errorText: String = "Something went wrong while loading", show: Boolean = true){
|
|
|
|
if(show){
|
|
|
|
activityBinding.motionLayout.transitionToEnd()
|
|
|
|
// binding.profileErrorLayout.errorText.text = errorText
|
|
|
|
} else if(activityBinding.motionLayout.progress == 1F) {
|
|
|
|
activityBinding.motionLayout.transitionToStart()
|
|
|
|
}
|
|
|
|
activityBinding.profileProgressBar.visibility = View.GONE
|
|
|
|
activityBinding.profileRefreshLayout.isRefreshing = false
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialises the [RecyclerView] adapter for the different FeedFragments.
|
|
|
|
*
|
|
|
|
* Makes the UI respond to various [LoadState]s, including errors when an error message is shown.
|
|
|
|
*/
|
|
|
|
internal fun <T: Any> initAdapter(binding: ActivityProfileBinding, adapter: PagingDataAdapter<T, RecyclerView.ViewHolder>) {
|
|
|
|
binding.profilePostsRecyclerView.adapter = adapter.withLoadStateFooter(
|
|
|
|
footer = ReposLoadStateAdapter { adapter.retry() }
|
|
|
|
)
|
|
|
|
|
|
|
|
adapter.addLoadStateListener { loadState ->
|
|
|
|
|
|
|
|
if(!binding.profileProgressBar.isVisible && binding.profileRefreshLayout.isRefreshing) {
|
|
|
|
// Stop loading spinner when loading is done
|
|
|
|
binding.profileRefreshLayout.isRefreshing = loadState.refresh is LoadState.Loading
|
|
|
|
} else {
|
|
|
|
// ProgressBar should stop showing as soon as the source stops loading ("source"
|
|
|
|
// meaning the database, so don't wait on the network)
|
|
|
|
val sourceLoading = loadState.source.refresh is LoadState.Loading
|
|
|
|
if(!sourceLoading && binding.profilePostsRecyclerView.size > 0){
|
|
|
|
binding.profilePostsRecyclerView.isVisible = true
|
|
|
|
binding.profileProgressBar.isVisible = false
|
|
|
|
} else if(binding.profilePostsRecyclerView.size == 0
|
|
|
|
&& loadState.append is LoadState.NotLoading
|
|
|
|
&& loadState.append.endOfPaginationReached){
|
|
|
|
binding.profileProgressBar.isVisible = false
|
|
|
|
showError(errorText = "Nothing to see here :(")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Toast on any error, regardless of whether it came from RemoteMediator or PagingSource
|
|
|
|
val errorState = loadState.source.append as? LoadState.Error
|
|
|
|
?: loadState.source.prepend as? LoadState.Error
|
|
|
|
?: loadState.source.refresh as? LoadState.Error
|
|
|
|
?: loadState.append as? LoadState.Error
|
|
|
|
?: loadState.prepend as? LoadState.Error
|
|
|
|
?: loadState.refresh as? LoadState.Error
|
|
|
|
errorState?.let {
|
|
|
|
showError(errorText = it.error.toString())
|
|
|
|
}
|
|
|
|
if (errorState == null) showError(show = false, errorText = "")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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)
|
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)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-02-08 15:07:23 +01:00
|
|
|
private fun getAndSetAccount(){
|
2020-12-29 22:14:32 +01:00
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val account = try{
|
2021-02-08 15:07:23 +01:00
|
|
|
pixelfedAPI.getAccount("Bearer $accessToken", accountId)
|
2020-12-29 22:14:32 +01:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|