Rearrange profile layout

This commit is contained in:
mjaillot 2021-02-26 11:21:04 +01:00
parent 483fcd3a7e
commit f9f4a6b8be
2 changed files with 69 additions and 91 deletions

View File

@ -46,26 +46,23 @@ import retrofit2.HttpException
import java.io.IOException import java.io.IOException
class ProfileActivity : BaseActivity() { class ProfileActivity : BaseActivity() {
private lateinit var pixelfedAPI : PixelfedAPI
private lateinit var pixelfedAPI : PixelfedAPI
private lateinit var accessToken : String private lateinit var accessToken : String
private lateinit var domain : String private lateinit var domain : String
private lateinit var accountId : String private lateinit var accountId : String
private lateinit var binding: ActivityProfileBinding
private var user: UserDatabaseEntity? = null
private lateinit var activityBinding: ActivityProfileBinding
private lateinit var profileAdapter: PagingDataAdapter<Status, RecyclerView.ViewHolder> private lateinit var profileAdapter: PagingDataAdapter<Status, RecyclerView.ViewHolder>
private lateinit var viewModel: FeedViewModel<Status> private lateinit var viewModel: FeedViewModel<Status>
private var user: UserDatabaseEntity? = null
private var job: Job? = null private var job: Job? = null
@ExperimentalPagingApi @ExperimentalPagingApi
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
activityBinding = ActivityProfileBinding.inflate(layoutInflater) binding = ActivityProfileBinding.inflate(layoutInflater)
setContentView(activityBinding.root) setContentView(binding.root)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
@ -79,12 +76,6 @@ class ProfileActivity : BaseActivity() {
val account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account? val account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account?
accountId = account?.id ?: user!!.user_id accountId = account?.id ?: user!!.user_id
setContent(account)
profileAdapter = ProfilePostsAdapter()
initAdapter(activityBinding, profileAdapter)
// get the view model // get the view model
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
viewModel = ViewModelProvider(this, ProfileViewModelFactory( viewModel = ViewModelProvider(this, ProfileViewModelFactory(
@ -96,19 +87,22 @@ class ProfileActivity : BaseActivity() {
) )
).get(FeedViewModel::class.java) as FeedViewModel<Status> ).get(FeedViewModel::class.java) as FeedViewModel<Status>
activityBinding.profilePostsRecyclerView.layoutManager = GridLayoutManager(this, 3) profileAdapter = ProfilePostsAdapter()
initAdapter(binding, profileAdapter)
profileLaunch() binding.profilePostsRecyclerView.layoutManager = GridLayoutManager(this, 3)
profileInitSearch()
activityBinding.profileRefreshLayout.setOnRefreshListener { binding.profileRefreshLayout.setOnRefreshListener {
//It shouldn't be necessary to also retry() in addition to refresh(), //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... //but if we don't do this, reloads after an error fail immediately...
profileAdapter.retry() profileAdapter.retry()
profileAdapter.refresh() profileAdapter.refresh()
} }
}
setContent(account)
profileLaunch()
profileInitSearch()
}
private fun profileLaunch() { private fun profileLaunch() {
// Make sure we cancel the previous job before creating a new one // Make sure we cancel the previous job before creating a new one
@ -128,23 +122,21 @@ class ProfileActivity : BaseActivity() {
.distinctUntilChangedBy { it.refresh } .distinctUntilChangedBy { it.refresh }
// Only react to cases where Remote REFRESH completes i.e., NotLoading. // Only react to cases where Remote REFRESH completes i.e., NotLoading.
.filter { it.refresh is LoadState.NotLoading } .filter { it.refresh is LoadState.NotLoading }
.collect { activityBinding.profilePostsRecyclerView.scrollToPosition(0) } .collect { binding.profilePostsRecyclerView.scrollToPosition(0) }
} }
} }
/** /**
* Shows or hides the error in the different FeedFragments * Shows or hides the error in the different FeedFragments
*/ */
private fun showError(errorText: String = "Something went wrong while loading", show: Boolean = true){ private fun showError(errorText: String = "Something went wrong while loading", show: Boolean = true){
if(show){ if(show){
activityBinding.motionLayout.transitionToEnd() binding.profileProgressBar.visibility = View.GONE
// binding.profileErrorLayout.errorText.text = errorText binding.motionLayout.transitionToEnd()
} else if(activityBinding.motionLayout.progress == 1F) { } else if(binding.motionLayout.progress == 1F) {
activityBinding.motionLayout.transitionToStart() binding.motionLayout.transitionToStart()
} }
activityBinding.profileProgressBar.visibility = View.GONE binding.profileRefreshLayout.isRefreshing = false
activityBinding.profileRefreshLayout.isRefreshing = false
} }
/** /**
@ -215,44 +207,32 @@ class ProfileActivity : BaseActivity() {
} }
} }
if(account != null && account.id != user?.user_id) {
//if we aren't viewing our own account, activate follow button //if we aren't viewing our own account, activate follow button
if(account != null && account.id != user?.user_id) activateFollow(account) activateFollow(account)
} else {
//if we *are* viewing our own account, activate the edit button //if we *are* viewing our own account, activate the edit button
else activateEditButton() activateEditButton()
}
// On click open followers list // On click open followers list
activityBinding.nbFollowersTextView.setOnClickListener{ onClickFollowers(account) } binding.nbFollowersTextView.setOnClickListener{ onClickFollowers(account) }
// On click open followers list // On click open followers list
activityBinding.nbFollowingTextView.setOnClickListener{ onClickFollowing(account) } binding.nbFollowingTextView.setOnClickListener{ onClickFollowing(account) }
}
private fun getAndSetAccount(){
lifecycleScope.launchWhenCreated {
val account = try{
pixelfedAPI.getAccount("Bearer $accessToken", accountId)
} catch (exception: IOException) {
Log.e("ProfileActivity:", exception.toString())
return@launchWhenCreated showError()
} catch (exception: HttpException) {
return@launchWhenCreated showError()
}
setContent(account)
}
} }
/** /**
* Populate profile page with user's data * Populate profile page with user's data
*/ */
private fun setViews(account: Account) { private fun setViews(account: Account) {
val profilePicture = activityBinding.profilePictureImageView val profilePicture = binding.profilePictureImageView
ImageConverter.setRoundImageFromURL( ImageConverter.setRoundImageFromURL(
View(applicationContext), View(applicationContext),
account.avatar, account.avatar,
profilePicture profilePicture
) )
activityBinding.descriptionTextView.text = parseHTMLText( binding.descriptionTextView.text = parseHTMLText(
account.note ?: "", emptyList(), pixelfedAPI, account.note ?: "", emptyList(), pixelfedAPI,
applicationContext, "Bearer $accessToken", applicationContext, "Bearer $accessToken",
lifecycleScope lifecycleScope
@ -260,27 +240,29 @@ class ProfileActivity : BaseActivity() {
val displayName = account.getDisplayName() val displayName = account.getDisplayName()
activityBinding.accountNameTextView.text = displayName binding.accountNameTextView.text = displayName
supportActionBar?.title = displayName supportActionBar?.title = displayName
if(displayName != "@${account.acct}"){ if(displayName != "@${account.acct}") {
supportActionBar?.subtitle = "@${account.acct}" supportActionBar?.subtitle = "@${account.acct}"
} }
activityBinding.nbPostsTextView.text = applicationContext.getString(R.string.nb_posts) binding.nbPostsTextView.text = applicationContext.getString(R.string.nb_posts)
.format(account.statuses_count.toString()) .format(account.statuses_count.toString())
activityBinding.nbFollowersTextView.text = applicationContext.getString(R.string.nb_followers) binding.nbFollowersTextView.text = applicationContext.getString(R.string.nb_followers)
.format(account.followers_count.toString()) .format(account.followers_count.toString())
activityBinding.nbFollowingTextView.text = applicationContext.getString(R.string.nb_following) binding.nbFollowingTextView.text = applicationContext.getString(R.string.nb_following)
.format(account.following_count.toString()) .format(account.following_count.toString())
} }
private fun onClickEditButton() { private fun onClickEditButton() {
val url = "$domain/settings/home" val url = "$domain/settings/home"
if (!openUrl(url)) Log.e("ProfileActivity", "Cannot open this link") if(!openUrl(url)) {
Log.e("ProfileActivity", "Cannot open this link")
}
} }
private fun onClickFollowers(account: Account?) { private fun onClickFollowers(account: Account?) {
@ -301,7 +283,7 @@ class ProfileActivity : BaseActivity() {
private fun activateEditButton() { private fun activateEditButton() {
// Edit button redirects to Pixelfed's "edit account" page // Edit button redirects to Pixelfed's "edit account" page
activityBinding.editButton.apply { binding.editButton.apply {
visibility = View.VISIBLE visibility = View.VISIBLE
setOnClickListener{ onClickEditButton() } setOnClickListener{ onClickEditButton() }
} }
@ -324,7 +306,7 @@ class ProfileActivity : BaseActivity() {
} else { } else {
setOnClickFollow(account) setOnClickFollow(account)
} }
activityBinding.followButton.visibility = View.VISIBLE binding.followButton.visibility = View.VISIBLE
} }
} catch (exception: IOException) { } catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString()) Log.e("FOLLOW ERROR", exception.toString())
@ -342,7 +324,7 @@ class ProfileActivity : BaseActivity() {
} }
private fun setOnClickFollow(account: Account) { private fun setOnClickFollow(account: Account) {
activityBinding.followButton.apply { binding.followButton.apply {
setText(R.string.follow) setText(R.string.follow)
setOnClickListener { setOnClickListener {
lifecycleScope.launchWhenResumed { lifecycleScope.launchWhenResumed {
@ -367,7 +349,7 @@ class ProfileActivity : BaseActivity() {
} }
private fun setOnClickUnfollow(account: Account) { private fun setOnClickUnfollow(account: Account) {
activityBinding.followButton.apply { binding.followButton.apply {
setText(R.string.unfollow) setText(R.string.unfollow)
setOnClickListener { setOnClickListener {

View File

@ -18,27 +18,26 @@
<ImageView <ImageView
android:id="@+id/profilePictureImageView" android:id="@+id/profilePictureImageView"
android:layout_width="120dp" android:layout_width="88dp"
android:layout_height="120dp" android:layout_height="88dp"
android:layout_marginStart="16dp" android:layout_marginStart="20dp"
android:layout_marginTop="16dp" android:layout_marginTop="6dp"
android:contentDescription="@string/profile_picture"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" tools:srcCompat="@tools:sample/avatars" />
android:contentDescription="@string/profile_picture" />
<TextView <TextView
android:id="@+id/nbPostsTextView" android:id="@+id/nbPostsTextView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_marginStart="20dp"
android:layout_marginTop="6dp"
android:gravity="center" android:gravity="center"
android:text="@string/default_nposts" android:text="@string/default_nposts"
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/profilePictureImageView" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_chainStyle="spread" app:layout_constraintTop_toBottomOf="@+id/descriptionTextView" />
app:layout_constraintStart_toEndOf="@+id/profilePictureImageView"
app:layout_constraintTop_toTopOf="@+id/profilePictureImageView" />
<TextView <TextView
android:id="@+id/nbFollowersTextView" android:id="@+id/nbFollowersTextView"
@ -56,7 +55,7 @@
android:id="@+id/nbFollowingTextView" android:id="@+id/nbFollowingTextView"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="8dp" android:layout_marginEnd="20dp"
android:gravity="center" android:gravity="center"
android:text="@string/default_nfollowing" android:text="@string/default_nfollowing"
android:textStyle="bold" android:textStyle="bold"
@ -85,10 +84,9 @@
android:layout_marginLeft="20dp" android:layout_marginLeft="20dp"
android:layout_marginTop="5dp" android:layout_marginTop="5dp"
android:layout_marginRight="20dp" android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/accountNameTextView"/> app:layout_constraintTop_toBottomOf="@id/accountNameTextView" />
<Button <Button
android:id="@+id/followButton" android:id="@+id/followButton"
@ -98,27 +96,26 @@
android:text="@string/follow" android:text="@string/follow"
android:textColor="@color/colorButtonText" android:textColor="@color/colorButtonText"
android:visibility="invisible" android:visibility="invisible"
tools:layout_editor_absoluteX="16dp" app:layout_constraintBottom_toBottomOf="@+id/profilePictureImageView"
tools:layout_editor_absoluteY="185dp" app:layout_constraintEnd_toEndOf="parent"
tools:visibility="visible" android:layout_marginEnd="20dp"
app:layout_constraintStart_toStartOf="@+id/profilePictureImageView" app:layout_constraintTop_toTopOf="@+id/profilePictureImageView"
app:layout_constraintTop_toBottomOf="@+id/descriptionTextView"/> tools:visibility="visible" />
<Button <Button
android:id="@+id/editButton" android:id="@+id/editButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:backgroundTint="@color/colorButtonBg" android:backgroundTint="@color/colorButtonBg"
android:text="@string/edit_profile" android:text="@string/edit_profile"
android:textColor="@color/colorButtonText" android:textColor="@color/colorButtonText"
android:visibility="gone" android:visibility="gone"
app:icon="@drawable/ic_baseline_open_in_browser_24" app:icon="@drawable/ic_baseline_open_in_browser_24"
app:iconTint="@color/colorButtonText" app:iconTint="@color/colorButtonText"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="@+id/profilePictureImageView"
app:layout_constraintStart_toStartOf="@+id/profilePictureImageView" app:layout_constraintStart_toEndOf="@+id/profilePictureImageView"
app:layout_constraintTop_toBottomOf="@+id/descriptionTextView" /> app:layout_constraintTop_toTopOf="@+id/profilePictureImageView"
app:layout_constraintEnd_toEndOf="parent" />
<ProgressBar <ProgressBar
@ -130,19 +127,19 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/followButton" /> app:layout_constraintTop_toBottomOf="@id/nbFollowersTextView" />
<androidx.constraintlayout.motion.widget.MotionLayout <androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motionLayout" android:id="@+id/motionLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginTop="8dp" android:layout_marginTop="6dp"
android:visibility="visible" android:visibility="visible"
app:layoutDescription="@xml/error_layout_xml_error_scene" app:layoutDescription="@xml/error_layout_xml_error_scene"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/followButton" app:layout_constraintTop_toBottomOf="@id/nbFollowersTextView"
tools:visibility="visible"> tools:visibility="visible">
<include <include
@ -162,7 +159,6 @@
</androidx.constraintlayout.motion.widget.MotionLayout> </androidx.constraintlayout.motion.widget.MotionLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView> </androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>