stop using api objects but use apiholder directly

This commit is contained in:
Matthieu 2021-04-19 11:43:14 +02:00
parent e41e5b4f19
commit a3e1e3c669
7 changed files with 60 additions and 43 deletions

View File

@ -12,13 +12,13 @@ import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.core.text.toSpanned
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleCoroutineScope
import com.h.pixeldroid.R
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.Account.Companion.openAccountFromId
import com.h.pixeldroid.utils.api.objects.Mention
import kotlinx.coroutines.coroutineScope
import com.h.pixeldroid.utils.db.AppDatabase
import com.h.pixeldroid.utils.di.PixelfedAPIHolder
import java.net.URI
import java.net.URISyntaxException
import java.text.ParseException
@ -50,11 +50,12 @@ fun getDomain(urlString: String?): String {
}
fun parseHTMLText(
text : String,
mentions: List<Mention>?,
api : PixelfedAPI,
context: Context,
lifecycleScope: LifecycleCoroutineScope
text: String,
mentions: List<Mention>?,
apiHolder: PixelfedAPIHolder,
context: Context,
lifecycleScope: LifecycleCoroutineScope,
db: AppDatabase
) : Spanned {
//Convert text to spannable
val content = fromHtml(text)
@ -107,6 +108,7 @@ fun parseHTMLText(
Log.e("MENTION", "CLICKED")
//Retrieve the account for the given profile
lifecycleScope.launchWhenCreated {
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
openAccountFromId(accountId, api, context)
}
}

View File

@ -50,7 +50,7 @@ class PostActivity : BaseActivity() {
val holder = StatusViewHolder(binding.postFragmentSingle)
holder.bind(status, apiHolder.api!!, db, lifecycleScope, displayDimensionsInPx(), isActivity = true)
holder.bind(status, apiHolder, db, lifecycleScope, displayDimensionsInPx(), isActivity = true)
activateCommenter()
@ -109,9 +109,10 @@ class PostActivity : BaseActivity() {
itemBinding.commentText.text = parseHTMLText(
commentContent,
mentions,
apiHolder.api!!,
apiHolder,
context,
lifecycleScope
lifecycleScope,
db
)
}

View File

@ -29,6 +29,7 @@ import com.h.pixeldroid.utils.api.objects.Status.Companion.POST_COMMENT_TAG
import com.h.pixeldroid.utils.api.objects.Status.Companion.POST_TAG
import com.h.pixeldroid.utils.api.objects.Status.Companion.VIEW_COMMENTS_TAG
import com.h.pixeldroid.utils.db.AppDatabase
import com.h.pixeldroid.utils.di.PixelfedAPIHolder
import com.karumi.dexter.Dexter
import com.karumi.dexter.listener.PermissionDeniedResponse
import com.karumi.dexter.listener.PermissionGrantedResponse
@ -46,7 +47,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
private var status: Status? = null
fun bind(status: Status?, pixelfedAPI: PixelfedAPI, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope, displayDimensionsInPx: Pair<Int, Int>, isActivity: Boolean = false) {
fun bind(status: Status?, pixelfedAPI: PixelfedAPIHolder, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope, displayDimensionsInPx: Pair<Int, Int>, isActivity: Boolean = false) {
this.itemView.visibility = View.VISIBLE
this.status = status
@ -177,8 +178,9 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
private fun setDescription(
api: PixelfedAPI,
lifecycleScope: LifecycleCoroutineScope
apiHolder: PixelfedAPIHolder,
lifecycleScope: LifecycleCoroutineScope,
db: AppDatabase
) {
binding.description.apply {
if (status?.content.isNullOrBlank()) {
@ -187,9 +189,10 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
text = parseHTMLText(
status?.content.orEmpty(),
status?.mentions,
api,
apiHolder,
binding.root.context,
lifecycleScope
lifecycleScope,
db
)
movementMethod = LinkMovementMethod.getInstance()
}
@ -197,20 +200,20 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
//region buttons
private fun activateButtons(
api: PixelfedAPI,
apiHolder: PixelfedAPIHolder,
db: AppDatabase,
lifecycleScope: LifecycleCoroutineScope,
isActivity: Boolean
){
//Set the special HTML text
setDescription(api, lifecycleScope)
setDescription(apiHolder, lifecycleScope, db)
//Activate onclickListeners
activateLiker(
api, status?.favourited ?: false, lifecycleScope
apiHolder, status?.favourited ?: false, lifecycleScope, db
)
activateReblogger(
api, status?.reblogged ?: false, lifecycleScope
apiHolder, status?.reblogged ?: false, lifecycleScope, db
)
if(isActivity){
@ -230,13 +233,14 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
showComments(lifecycleScope, isActivity)
activateMoreButton(api, db, lifecycleScope)
activateMoreButton(apiHolder, db, lifecycleScope)
}
private fun activateReblogger(
api: PixelfedAPI,
isReblogged: Boolean,
lifecycleScope: LifecycleCoroutineScope
apiHolder: PixelfedAPIHolder,
isReblogged: Boolean,
lifecycleScope: LifecycleCoroutineScope,
db: AppDatabase
) {
binding.reblogger.apply {
//Set initial button state
@ -245,6 +249,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
//Activate the button
setEventListener { _, buttonState ->
lifecycleScope.launchWhenCreated {
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
if (buttonState) {
// Button is active
undoReblogPost(api)
@ -298,7 +303,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
}
private fun activateMoreButton(api: PixelfedAPI, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope){
private fun activateMoreButton(apiHolder: PixelfedAPIHolder, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope){
binding.statusMore.setOnClickListener {
PopupMenu(it.context, it).apply {
setOnMenuItemClickListener { item ->
@ -381,6 +386,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
db.homePostDao().delete(id, user.user_id, user.instance_uri)
db.publicPostDao().delete(id, user.user_id, user.instance_uri)
try {
val api = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
api.deleteStatus(id)
binding.root.visibility = View.GONE
} catch (exception: HttpException) {
@ -425,9 +431,10 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
}
private fun activateLiker(
api: PixelfedAPI,
isLiked: Boolean,
lifecycleScope: LifecycleCoroutineScope
apiHolder: PixelfedAPIHolder,
isLiked: Boolean,
lifecycleScope: LifecycleCoroutineScope,
db: AppDatabase
) {
binding.liker.apply {
@ -437,6 +444,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
//Activate the liker
setEventListener { _, buttonState ->
lifecycleScope.launchWhenCreated {
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
if (buttonState) {
// Button is active, unlike
unLikePostCall(api)
@ -458,6 +466,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
if(binding.sensitiveWarning.visibility == View.GONE) {
//Check for double click
if(clicked) {
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
if (binding.liker.isChecked) {
// Button is active, unlike
binding.liker.isChecked = false

View File

@ -27,7 +27,6 @@ import com.h.pixeldroid.posts.feeds.cachedFeeds.ViewModelFactory
import com.h.pixeldroid.posts.parseHTMLText
import com.h.pixeldroid.posts.setTextViewFromISO8601
import com.h.pixeldroid.profile.ProfileActivity
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.Account
import com.h.pixeldroid.utils.api.objects.Notification
import com.h.pixeldroid.utils.api.objects.Status
@ -165,8 +164,9 @@ class NotificationsFragment : CachedFeedFragment<Notification>() {
fun bind(
notification: Notification?,
api: PixelfedAPI,
lifecycleScope: LifecycleCoroutineScope
api: PixelfedAPIHolder,
lifecycleScope: LifecycleCoroutineScope,
db: AppDatabase
) {
this.notification = notification
@ -207,7 +207,8 @@ class NotificationsFragment : CachedFeedFragment<Notification>() {
notification?.status?.mentions,
api,
itemView.context,
lifecycleScope
lifecycleScope,
db
)
}
@ -255,8 +256,9 @@ class NotificationsFragment : CachedFeedFragment<Notification>() {
uiModel.let {
(holder as NotificationViewHolder).bind(
it,
apiHolder.setDomainToCurrentUser(db),
lifecycleScope
apiHolder,
lifecycleScope,
db
)
}
}

View File

@ -90,7 +90,7 @@ class PostFeedFragment<T: FeedContentDatabase>: CachedFeedFragment<T>() {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val uiModel = getItem(position) as Status
uiModel.let {
(holder as StatusViewHolder).bind(it, apiHolder.setDomainToCurrentUser(db), db, lifecycleScope, displayDimensionsInPx)
(holder as StatusViewHolder).bind(it, apiHolder, db, lifecycleScope, displayDimensionsInPx)
}
}
}

View File

@ -79,7 +79,7 @@ class SearchPostsFragment : UncachedFeedFragment<Status>() {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val uiModel = getItem(position) as Status
uiModel.let {
(holder as StatusViewHolder).bind(it, apiHolder.setDomainToCurrentUser(db), db, lifecycleScope, displayDimensionsInPx)
(holder as StatusViewHolder).bind(it, apiHolder, db, lifecycleScope, displayDimensionsInPx)
}
}
}

View File

@ -44,7 +44,6 @@ import java.io.IOException
class ProfileActivity : BaseActivity() {
private lateinit var pixelfedAPI : PixelfedAPI
private lateinit var domain : String
private lateinit var accountId : String
private lateinit var binding: ActivityProfileBinding
@ -65,7 +64,6 @@ class ProfileActivity : BaseActivity() {
user = db.userDao().getActiveUser()
domain = user?.instance_uri.orEmpty()
pixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
// Set profile according to given account
val account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account?
@ -121,8 +119,9 @@ class ProfileActivity : BaseActivity() {
setViews(account)
} else {
lifecycleScope.launchWhenResumed {
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
val myAccount: Account = try {
pixelfedAPI.verifyCredentials()
api.verifyCredentials()
} catch (exception: IOException) {
Log.e("ProfileActivity:", exception.toString())
return@launchWhenResumed showError()
@ -159,9 +158,10 @@ class ProfileActivity : BaseActivity() {
)
binding.descriptionTextView.text = parseHTMLText(
account.note ?: "", emptyList(), pixelfedAPI,
account.note ?: "", emptyList(), apiHolder,
applicationContext,
lifecycleScope
lifecycleScope,
db
)
val displayName = account.getDisplayName()
@ -232,7 +232,8 @@ class ProfileActivity : BaseActivity() {
// Get relationship between the two users (credential and this) and set followButton accordingly
lifecycleScope.launch {
try {
val relationship = pixelfedAPI.checkRelationships(
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
val relationship = api.checkRelationships(
listOf(account.id.orEmpty())
).firstOrNull()
@ -265,7 +266,8 @@ class ProfileActivity : BaseActivity() {
setOnClickListener {
lifecycleScope.launchWhenResumed {
try {
val rel = pixelfedAPI.follow(account.id.orEmpty())
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
val rel = api.follow(account.id.orEmpty())
if(rel.following == true) setOnClickUnfollow(account, rel.requested == true)
else setOnClickFollow(account)
} catch (exception: IOException) {
@ -295,7 +297,8 @@ class ProfileActivity : BaseActivity() {
fun unfollow() {
lifecycleScope.launchWhenResumed {
try {
val rel = pixelfedAPI.unfollow(account.id.orEmpty())
val api: PixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
val rel = api.unfollow(account.id.orEmpty())
if(rel.following == false && rel.requested == false) setOnClickFollow(account)
else setOnClickUnfollow(account, rel.requested == true)
} catch (exception: IOException) {