Fix discover

This commit is contained in:
Matthieu 2021-02-09 20:47:04 +01:00
parent c88f4a4cec
commit 6f9bbf5100
5 changed files with 8 additions and 69 deletions

View File

@ -6,7 +6,6 @@ import android.view.View
import androidx.lifecycle.lifecycleScope
import com.h.pixeldroid.R
import com.h.pixeldroid.databinding.ActivityPostBinding
import com.h.pixeldroid.utils.api.objects.DiscoverPost
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.utils.api.objects.Status.Companion.DISCOVER_TAG
import com.h.pixeldroid.utils.api.objects.Status.Companion.DOMAIN_TAG
@ -30,7 +29,6 @@ class PostActivity : BaseActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
val status = intent.getSerializableExtra(POST_TAG) as Status?
val discoverPost: DiscoverPost? = intent.getSerializableExtra(DISCOVER_TAG) as DiscoverPost?
val user = db.userDao().getActiveUser()
@ -41,38 +39,14 @@ class PostActivity : BaseActivity() {
val arguments = Bundle()
arguments.putString(DOMAIN_TAG, domain)
if (discoverPost != null) {
binding.postProgressBar.visibility = View.VISIBLE
getDiscoverPost(arguments, discoverPost)
} else {
initializeFragment(arguments, status)
}
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
private fun getDiscoverPost(
arguments: Bundle,
discoverPost: DiscoverPost
) {
val api = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
val id = discoverPost.url?.substringAfterLast('/') ?: ""
lifecycleScope.launchWhenCreated {
try {
val status = api.getStatus("Bearer $accessToken", id)
binding.postProgressBar.visibility = View.GONE
initializeFragment(arguments, status)
} catch (exception: IOException) {
//TODO show error message
Log.e("PostActivity:", exception.toString())
} catch (exception: HttpException) {
}
}
}
private fun initializeFragment(arguments: Bundle, status: Status?){
supportActionBar?.title = getString(R.string.post_title).format(status!!.account?.getDisplayName())
arguments.putSerializable(POST_TAG, status)

View File

@ -4,25 +4,17 @@ import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.annotation.StringRes
import androidx.appcompat.widget.SearchView
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.h.pixeldroid.R
import com.h.pixeldroid.databinding.FragmentSearchBinding
import com.h.pixeldroid.databinding.PostFragmentBinding
import com.h.pixeldroid.profile.ProfilePostViewHolder
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.DiscoverPost
import com.h.pixeldroid.utils.api.objects.DiscoverPosts
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.posts.PostActivity
import com.h.pixeldroid.utils.BaseFragment
@ -34,10 +26,7 @@ import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.mikepenz.iconics.utils.color
import com.mikepenz.iconics.utils.paddingDp
import com.mikepenz.iconics.utils.sizeDp
import retrofit2.Call
import retrofit2.Callback
import retrofit2.HttpException
import retrofit2.Response
import java.io.IOException
/**
@ -122,12 +111,12 @@ class SearchDiscoverFragment : BaseFragment() {
}
/**
* [RecyclerView.Adapter] that can display a list of [DiscoverPost]s
* [RecyclerView.Adapter] that can display a list of [Status]s' thumbnails for the discover view
*/
class DiscoverRecyclerViewAdapter: RecyclerView.Adapter<ProfilePostViewHolder>() {
private val posts: ArrayList<DiscoverPost> = ArrayList()
private val posts: ArrayList<Status> = ArrayList()
fun addPosts(newPosts : List<DiscoverPost>) {
fun addPosts(newPosts : List<Status>) {
posts.clear()
posts.addAll(newPosts)
notifyDataSetChanged()
@ -141,15 +130,15 @@ class SearchDiscoverFragment : BaseFragment() {
override fun onBindViewHolder(holder: ProfilePostViewHolder, position: Int) {
val post = posts[position]
if(post.type?.contains("album") == true) {
if(post.media_attachments?.size ?: 0 > 1) {
holder.albumIcon.visibility = View.VISIBLE
} else {
holder.albumIcon.visibility = View.GONE
}
ImageConverter.setSquareImageFromURL(holder.postView, post.thumb, holder.postPreview)
ImageConverter.setSquareImageFromURL(holder.postView, post.media_attachments?.firstOrNull()?.preview_url, holder.postPreview)
holder.postPreview.setOnClickListener {
val intent = Intent(holder.postView.context, PostActivity::class.java)
intent.putExtra(Status.DISCOVER_TAG, post)
intent.putExtra(Status.POST_TAG, post)
holder.postView.context.startActivity(intent)
}
}

View File

@ -1,13 +0,0 @@
package com.h.pixeldroid.utils.api.objects
import java.io.Serializable
/*
NOT DOCUMENTED, USE WITH CAUTION
*/
data class DiscoverPost(
val type: String?, //This is probably an enum, with these values: https://github.com/pixelfed/pixelfed/blob/700c7805cecc364b68b9cfe20df00608e0f6c465/app/Status.php#L31
val url: String?, //URL to post
val thumb: String? //URL to thumbnail
) : Serializable

View File

@ -4,5 +4,5 @@ import java.io.Serializable
data class DiscoverPosts(
//Required attributes
val posts: List<DiscoverPost>
val posts: List<Status>
) : Serializable

View File

@ -6,17 +6,6 @@
android:layout_height="match_parent"
tools:context=".posts.PostActivity">
<ProgressBar
android:id="@+id/postProgressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">