From c65a121e2836f0b9cb1ed5467e0df59c70cd1f08 Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Mon, 2 Nov 2020 21:58:01 +0100 Subject: [PATCH] add reload and error to profile --- .../java/com/h/pixeldroid/ProfileActivity.kt | 131 +++++--- .../ProfilePostsRecyclerViewAdapter.kt | 6 +- .../fragments/ProfilePostsFragment.kt | 41 --- .../ic_baseline_open_in_browser_24.xml | 10 + app/src/main/res/layout/activity_profile.xml | 279 +++++++++--------- .../layout/fragment_profile_posts_list.xml | 12 - app/src/main/res/layout/fragment_search.xml | 5 +- ...e.xml => error_layout_xml_error_scene.xml} | 0 8 files changed, 235 insertions(+), 249 deletions(-) delete mode 100644 app/src/main/java/com/h/pixeldroid/fragments/ProfilePostsFragment.kt create mode 100644 app/src/main/res/drawable/ic_baseline_open_in_browser_24.xml delete mode 100644 app/src/main/res/layout/fragment_profile_posts_list.xml rename app/src/main/res/xml/{fragment_search_xml_error_scene.xml => error_layout_xml_error_scene.xml} (100%) diff --git a/app/src/main/java/com/h/pixeldroid/ProfileActivity.kt b/app/src/main/java/com/h/pixeldroid/ProfileActivity.kt index 1af025b8..b5b4aa4f 100644 --- a/app/src/main/java/com/h/pixeldroid/ProfileActivity.kt +++ b/app/src/main/java/com/h/pixeldroid/ProfileActivity.kt @@ -6,14 +6,14 @@ import android.net.Uri import android.os.Bundle import android.util.Log import android.view.View -import android.widget.Button -import android.widget.ImageView -import android.widget.TextView -import android.widget.Toast +import android.widget.* +import androidx.annotation.StringRes import androidx.appcompat.app.AppCompatActivity +import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.content.ContextCompat import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.h.pixeldroid.adapters.ProfilePostsRecyclerViewAdapter import com.h.pixeldroid.api.PixelfedAPI import com.h.pixeldroid.db.AppDatabase @@ -24,6 +24,7 @@ import com.h.pixeldroid.objects.Relationship import com.h.pixeldroid.objects.Status import com.h.pixeldroid.utils.HtmlUtils.Companion.parseHTMLText import com.h.pixeldroid.utils.ImageConverter +import kotlinx.android.synthetic.main.fragment_search.* import retrofit2.Call import retrofit2.Callback import retrofit2.Response @@ -33,9 +34,9 @@ class ProfileActivity : AppCompatActivity() { private lateinit var pixelfedAPI : PixelfedAPI private lateinit var adapter : ProfilePostsRecyclerViewAdapter private lateinit var recycler : RecyclerView + private lateinit var refreshLayout: SwipeRefreshLayout private lateinit var accessToken : String private lateinit var domain : String - private var account: Account? = null private var user: UserDatabaseEntity? = null @Inject @@ -64,7 +65,16 @@ class ProfileActivity : AppCompatActivity() { adapter = ProfilePostsRecyclerViewAdapter() recycler.adapter = adapter - setContent() + // Set profile according to given account + val account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account? + + setContent(account) + + refreshLayout = findViewById(R.id.profileRefreshLayout) + + refreshLayout.setOnRefreshListener { + getAndSetAccount(account?.id ?: user!!.user_id) + } } override fun onSupportNavigateUp(): Boolean { @@ -72,96 +82,128 @@ class ProfileActivity : AppCompatActivity() { return true } - private fun setContent() { - // Set profile according to given account - account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account? - + private fun setContent(account: Account?) { if(account != null){ - setViews() - setPosts() + setViews(account) + setPosts(account) } else { pixelfedAPI.verifyCredentials("Bearer $accessToken") .enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if (response.code() == 200) { - account = response.body()!! + val myAccount = response.body()!! - setViews() + setViews(myAccount) // Populate profile page with user's posts - setPosts() + setPosts(myAccount) + } else { + showError() } } override fun onFailure(call: Call, t: Throwable) { Log.e("ProfileActivity:", t.toString()) + showError() } }) } //if we aren't viewing our own account, activate follow button - if(account != null && account!!.id != user?.user_id) activateFollow() + if(account != null && account.id != user?.user_id) activateFollow(account) //if we *are* viewing our own account, activate the edit button else activateEditButton() // On click open followers list - findViewById(R.id.nbFollowersTextView).setOnClickListener{ onClickFollowers() } + findViewById(R.id.nbFollowersTextView).setOnClickListener{ onClickFollowers(account) } // On click open followers list - findViewById(R.id.nbFollowingTextView).setOnClickListener{ onClickFollowing() } + findViewById(R.id.nbFollowingTextView).setOnClickListener{ onClickFollowing(account) } + } + + private fun getAndSetAccount(id: String){ + pixelfedAPI.getAccount("Bearer $accessToken", id) + .enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if (response.code() == 200) { + val account = response.body()!! + + setContent(account) + } else { + showError() + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.e("ProfileActivity:", t.toString()) + showError() + } + }) + } + + private fun showError(@StringRes errorText: Int = R.string.loading_toast, show: Boolean = true){ + val motionLayout = findViewById(R.id.motionLayout) + if(show){ + motionLayout?.transitionToEnd() + } else { + findViewById(R.id.profileProgressBar).visibility = View.GONE + motionLayout?.transitionToStart() + } + refreshLayout.isRefreshing = false } /** - * Populate myProfile page with user's data + * Populate profile page with user's data */ - private fun setViews() { + private fun setViews(account: Account) { val profilePicture = findViewById(R.id.profilePictureImageView) ImageConverter.setRoundImageFromURL( View(applicationContext), - account!!.avatar, + account.avatar, profilePicture ) val description = findViewById(R.id.descriptionTextView) description.text = parseHTMLText( - account!!.note ?: "", emptyList(), pixelfedAPI, + account.note ?: "", emptyList(), pixelfedAPI, applicationContext, "Bearer $accessToken" ) val accountName = findViewById(R.id.accountNameTextView) - accountName.text = account!!.getDisplayName() + accountName.text = account.getDisplayName() - val displayName = account!!.getDisplayName() + val displayName = account.getDisplayName() supportActionBar?.title = displayName - if(displayName != "@${account!!.acct}"){ - supportActionBar?.subtitle = "@${account!!.acct}" + if(displayName != "@${account.acct}"){ + supportActionBar?.subtitle = "@${account.acct}" } accountName.setTypeface(null, Typeface.BOLD) val nbPosts = findViewById(R.id.nbPostsTextView) nbPosts.text = applicationContext.getString(R.string.nb_posts) - .format(account!!.statuses_count.toString()) + .format(account.statuses_count.toString()) nbPosts.setTypeface(null, Typeface.BOLD) val nbFollowers = findViewById(R.id.nbFollowersTextView) nbFollowers.text = applicationContext.getString(R.string.nb_followers) - .format(account!!.followers_count.toString()) + .format(account.followers_count.toString()) nbFollowers.setTypeface(null, Typeface.BOLD) val nbFollowing = findViewById(R.id.nbFollowingTextView) nbFollowing.text = applicationContext.getString(R.string.nb_following) - .format(account!!.following_count.toString()) + .format(account.following_count.toString()) nbFollowing.setTypeface(null, Typeface.BOLD) } /** * Populate profile page with user's posts */ - private fun setPosts() { - pixelfedAPI.accountPosts("Bearer $accessToken", account_id = account!!.id) + private fun setPosts(account: Account) { + pixelfedAPI.accountPosts("Bearer $accessToken", account_id = account.id) .enqueue(object : Callback> { override fun onFailure(call: Call>, t: Throwable) { + showError() Log.e("ProfileActivity.Posts:", t.toString()) } @@ -172,6 +214,9 @@ class ProfileActivity : AppCompatActivity() { if (response.code() == 200) { val statuses = response.body()!! adapter.addPosts(statuses) + showError(show = false) + } else { + showError() } } }) @@ -188,7 +233,7 @@ class ProfileActivity : AppCompatActivity() { } } - private fun onClickFollowers() { + private fun onClickFollowers(account: Account?) { val intent = Intent(this, FollowsActivity::class.java) intent.putExtra(Account.FOLLOWERS_TAG, true) intent.putExtra(Account.ACCOUNT_TAG, account) @@ -196,7 +241,7 @@ class ProfileActivity : AppCompatActivity() { ContextCompat.startActivity(this, intent, null) } - private fun onClickFollowing() { + private fun onClickFollowing(account: Account?) { val intent = Intent(this, FollowsActivity::class.java) intent.putExtra(Account.FOLLOWERS_TAG, false) intent.putExtra(Account.ACCOUNT_TAG, account) @@ -214,9 +259,9 @@ class ProfileActivity : AppCompatActivity() { /** * Set up follow button */ - private fun activateFollow() { + private fun activateFollow(account: Account) { // Get relationship between the two users (credential and this) and set followButton accordingly - pixelfedAPI.checkRelationships("Bearer $accessToken", listOf(account!!.id.orEmpty())) + pixelfedAPI.checkRelationships("Bearer $accessToken", listOf(account.id.orEmpty())) .enqueue(object : Callback> { override fun onFailure(call: Call>, t: Throwable) { @@ -236,9 +281,9 @@ class ProfileActivity : AppCompatActivity() { val followButton = findViewById