Use view binding more

This commit is contained in:
Matthieu 2021-01-13 22:46:00 +01:00
parent 79c0d5c0b4
commit 6fa4488296
6 changed files with 118 additions and 138 deletions

View File

@ -88,9 +88,7 @@ class PostCreationActivity : BaseActivity() {
val instances = db.instanceDao().getAll()
val textField = findViewById<TextInputLayout>(R.id.postTextInputLayout)
textField.counterMaxLength = if (user != null){
binding.postTextInputLayout.counterMaxLength = if (user != null){
val thisInstances =
instances.filter { instanceDatabaseEntity ->
instanceDatabaseEntity.uri.contains(user!!.instance_uri)
@ -103,7 +101,7 @@ class PostCreationActivity : BaseActivity() {
accessToken = user?.accessToken.orEmpty()
pixelfedAPI = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
val carousel: ImageCarousel = findViewById(R.id.carousel)
val carousel: ImageCarousel = binding.carousel
carousel.addData(photoData.map { CarouselItem(it.imageUri.toString()) })
carousel.layoutCarouselCallback = {
//TODO transition instead of at once
@ -120,12 +118,12 @@ class PostCreationActivity : BaseActivity() {
}
// get the description and send the post
findViewById<Button>(R.id.post_creation_send_button).setOnClickListener {
binding.postCreationSendButton.setOnClickListener {
if (validateDescription() && photoData.isNotEmpty()) upload()
}
// Button to retry image upload when it fails
findViewById<Button>(R.id.retry_upload_button).setOnClickListener {
binding.retryUploadButton.setOnClickListener {
binding.uploadError.visibility = View.GONE
photoData.forEach {
it.uploadId = null
@ -134,24 +132,24 @@ class PostCreationActivity : BaseActivity() {
upload()
}
findViewById<ImageButton>(R.id.editPhotoButton).setOnClickListener {
binding.editPhotoButton.setOnClickListener {
carousel.currentPosition.takeIf { it != -1 }?.let { currentPosition ->
edit(currentPosition)
}
}
findViewById<ImageButton>(R.id.addPhotoButton).setOnClickListener {
binding.addPhotoButton.setOnClickListener {
addPhoto(it.context)
}
findViewById<ImageButton>(R.id.savePhotoButton).setOnClickListener {
binding.savePhotoButton.setOnClickListener {
carousel.currentPosition.takeIf { it != -1 }?.let { currentPosition ->
savePicture(it, currentPosition)
}
}
findViewById<ImageButton>(R.id.removePhotoButton).setOnClickListener {
binding.removePhotoButton.setOnClickListener {
carousel.currentPosition.takeIf { it != -1 }?.let { currentPosition ->
photoData.removeAt(currentPosition)
carousel.addData(photoData.map { CarouselItem(it.imageUri.toString()) })
@ -226,12 +224,13 @@ class PostCreationActivity : BaseActivity() {
private fun validateDescription(): Boolean {
val textField = findViewById<TextInputLayout>(R.id.postTextInputLayout)
val content = textField.editText?.length() ?: 0
if (content > textField.counterMaxLength) {
// error, too many characters
textField.error = getString(R.string.description_max_characters).format(textField.counterMaxLength)
return false
binding.postTextInputLayout.run {
val content = editText?.length() ?: 0
if (content > counterMaxLength) {
// error, too many characters
error = getString(R.string.description_max_characters).format(counterMaxLength)
return false
}
}
return true
}

View File

@ -65,9 +65,6 @@ class PhotoEditActivity : BaseActivity() {
private lateinit var filterListFragment: FilterListFragment
private lateinit var editImageFragment: EditImageFragment
private lateinit var viewPager: NonSwipeableViewPager
private lateinit var tabLayout: TabLayout
private var brightnessFinal = BRIGHTNESS_START
private var saturationFinal = SATURATION_START
private var contrastFinal = CONTRAST_START
@ -100,22 +97,18 @@ class PhotoEditActivity : BaseActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)
val cropButton: FloatingActionButton = findViewById(R.id.cropImageButton)
initialUri = intent.getParcelableExtra("picture_uri")
imageUri = initialUri
// Crop button on-click listener
cropButton.setOnClickListener {
binding.cropImageButton.setOnClickListener {
startCrop()
}
loadImage()
viewPager = findViewById(R.id.viewPager)
tabLayout = findViewById(R.id.tabs)
setupViewPager(viewPager)
tabLayout.setupWithViewPager(viewPager)
setupViewPager(binding.viewPager)
binding.tabs.setupWithViewPager(binding.viewPager)
}

View File

@ -181,7 +181,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
credential: String,
lifecycleScope: LifecycleCoroutineScope
) {
rootView.findViewById<TextView>(R.id.description).apply {
binding.description.apply {
if (status?.content.isNullOrBlank()) {
visibility = View.GONE
} else {

View File

@ -14,14 +14,14 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.h.pixeldroid.R
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
import com.h.pixeldroid.utils.api.objects.Account
import com.h.pixeldroid.utils.api.objects.Relationship
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.databinding.ActivityProfileBinding
import com.h.pixeldroid.posts.parseHTMLText
import com.h.pixeldroid.utils.BaseActivity
import com.h.pixeldroid.utils.ImageConverter
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.Account
import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
import com.h.pixeldroid.utils.openUrl
import kotlinx.coroutines.launch
import retrofit2.Call
@ -33,16 +33,17 @@ import java.io.IOException
class ProfileActivity : BaseActivity() {
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 user: UserDatabaseEntity? = null
private lateinit var binding: ActivityProfileBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
binding = ActivityProfileBinding.inflate(layoutInflater)
setContentView(binding.root)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
user = db.userDao().getActiveUser()
@ -52,19 +53,16 @@ class ProfileActivity : BaseActivity() {
accessToken = user?.accessToken.orEmpty()
// Set posts RecyclerView as a grid with 3 columns
recycler = findViewById(R.id.profilePostsRecyclerView)
recycler.layoutManager = GridLayoutManager(applicationContext, 3)
binding.profilePostsRecyclerView.layoutManager = GridLayoutManager(applicationContext, 3)
adapter = ProfilePostsRecyclerViewAdapter()
recycler.adapter = adapter
binding.profilePostsRecyclerView.adapter = adapter
// Set profile according to given account
val account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account?
setContent(account)
refreshLayout = findViewById(R.id.profileRefreshLayout)
refreshLayout.setOnRefreshListener {
binding.profileRefreshLayout.setOnRefreshListener {
getAndSetAccount(account?.id ?: user!!.user_id)
}
}
@ -101,9 +99,9 @@ class ProfileActivity : BaseActivity() {
// On click open followers list
findViewById<TextView>(R.id.nbFollowersTextView).setOnClickListener{ onClickFollowers(account) }
binding.nbFollowersTextView.setOnClickListener{ onClickFollowers(account) }
// On click open followers list
findViewById<TextView>(R.id.nbFollowingTextView).setOnClickListener{ onClickFollowing(account) }
binding.nbFollowingTextView.setOnClickListener{ onClickFollowing(account) }
}
private fun getAndSetAccount(id: String){
@ -121,59 +119,50 @@ class ProfileActivity : BaseActivity() {
}
private fun showError(@StringRes errorText: Int = R.string.loading_toast, show: Boolean = true){
val motionLayout = findViewById<MotionLayout>(R.id.motionLayout)
val motionLayout = binding.motionLayout
if(show){
motionLayout?.transitionToEnd()
motionLayout.transitionToEnd()
} else {
motionLayout?.transitionToStart()
motionLayout.transitionToStart()
}
findViewById<ProgressBar>(R.id.profileProgressBar).visibility = View.GONE
refreshLayout.isRefreshing = false
binding.profileProgressBar.visibility = View.GONE
binding.profileRefreshLayout.isRefreshing = false
}
/**
* Populate profile page with user's data
*/
private fun setViews(account: Account) {
val profilePicture = findViewById<ImageView>(R.id.profilePictureImageView)
val profilePicture = binding.profilePictureImageView
ImageConverter.setRoundImageFromURL(
View(applicationContext),
account.avatar,
profilePicture
)
val description = findViewById<TextView>(R.id.descriptionTextView)
description.text = parseHTMLText(
binding.descriptionTextView.text = parseHTMLText(
account.note ?: "", emptyList(), pixelfedAPI,
applicationContext, "Bearer $accessToken",
lifecycleScope
)
val accountName = findViewById<TextView>(R.id.accountNameTextView)
accountName.text = account.getDisplayName()
val displayName = account.getDisplayName()
binding.accountNameTextView.text = displayName
supportActionBar?.title = displayName
if(displayName != "@${account.acct}"){
supportActionBar?.subtitle = "@${account.acct}"
}
accountName.setTypeface(null, Typeface.BOLD)
val nbPosts = findViewById<TextView>(R.id.nbPostsTextView)
nbPosts.text = applicationContext.getString(R.string.nb_posts)
binding.nbPostsTextView.text = applicationContext.getString(R.string.nb_posts)
.format(account.statuses_count.toString())
nbPosts.setTypeface(null, Typeface.BOLD)
val nbFollowers = findViewById<TextView>(R.id.nbFollowersTextView)
nbFollowers.text = applicationContext.getString(R.string.nb_followers)
binding.nbFollowersTextView.text = applicationContext.getString(R.string.nb_followers)
.format(account.followers_count.toString())
nbFollowers.setTypeface(null, Typeface.BOLD)
val nbFollowing = findViewById<TextView>(R.id.nbFollowingTextView)
nbFollowing.text = applicationContext.getString(R.string.nb_following)
binding.nbFollowingTextView.text = applicationContext.getString(R.string.nb_following)
.format(account.following_count.toString())
nbFollowing.setTypeface(null, Typeface.BOLD)
}
/**
@ -227,9 +216,10 @@ class ProfileActivity : BaseActivity() {
private fun activateEditButton() {
// Edit button redirects to Pixelfed's "edit account" page
val editButton = findViewById<Button>(R.id.editButton)
editButton.visibility = View.VISIBLE
editButton.setOnClickListener{ onClickEditButton() }
binding.editButton.apply {
visibility = View.VISIBLE
setOnClickListener{ onClickEditButton() }
}
}
/**
@ -244,14 +234,12 @@ class ProfileActivity : BaseActivity() {
).firstOrNull()
if(relationship != null){
val followButton = findViewById<Button>(R.id.followButton)
if (relationship.following) {
setOnClickUnfollow(account)
} else {
setOnClickFollow(account)
}
followButton.visibility = View.VISIBLE
binding.followButton.visibility = View.VISIBLE
}
} catch (exception: IOException) {
Log.e("FOLLOW ERROR", exception.toString())
@ -269,53 +257,51 @@ class ProfileActivity : BaseActivity() {
}
private fun setOnClickFollow(account: Account) {
val followButton = findViewById<Button>(R.id.followButton)
followButton.setText(R.string.follow)
followButton.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()
binding.followButton.apply {
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()
}
}
}
}
}
private fun setOnClickUnfollow(account: Account) {
val followButton = findViewById<Button>(R.id.followButton)
binding.followButton.apply {
setText(R.string.unfollow)
followButton.setText(R.string.unfollow)
followButton.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()
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()
}
}
}
}

View File

@ -17,6 +17,8 @@ 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
@ -25,6 +27,7 @@ import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.posts.PostActivity
import com.h.pixeldroid.utils.BaseFragment
import com.h.pixeldroid.utils.ImageConverter
import com.h.pixeldroid.utils.bindingLifecycleAware
import com.mikepenz.iconics.IconicsColor
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
@ -46,37 +49,35 @@ class SearchDiscoverFragment : BaseFragment() {
private lateinit var recycler : RecyclerView
private lateinit var adapter : DiscoverRecyclerViewAdapter
private lateinit var accessToken: String
private lateinit var discoverProgressBar: ProgressBar
private lateinit var discoverRefreshLayout: SwipeRefreshLayout
var binding: FragmentSearchBinding by bindingLifecycleAware()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_search, container, false)
val search = view.findViewById<SearchView>(R.id.search)
): View {
binding = FragmentSearchBinding.inflate(inflater, container, false)
//Configure the search widget (see https://developer.android.com/guide/topics/search/search-dialog#ConfiguringWidget)
// Configure the search widget (see https://developer.android.com/guide/topics/search/search-dialog#ConfiguringWidget)
val searchManager = requireActivity().getSystemService(Context.SEARCH_SERVICE) as SearchManager
search.setSearchableInfo(searchManager.getSearchableInfo(requireActivity().componentName))
search.isSubmitButtonEnabled = true
binding.search.apply {
setSearchableInfo(searchManager.getSearchableInfo(requireActivity().componentName))
isSubmitButtonEnabled = true
}
// Set posts RecyclerView as a grid with 3 columns
recycler = view.findViewById(R.id.discoverList)
recycler = binding.discoverList
recycler.layoutManager = GridLayoutManager(requireContext(), 3)
adapter = DiscoverRecyclerViewAdapter()
recycler.adapter = adapter
val discoverText = view.findViewById<TextView>(R.id.discoverText)
discoverText.setCompoundDrawables(IconicsDrawable(requireContext(), GoogleMaterial.Icon.gmd_explore).apply {
binding.discoverText.setCompoundDrawables(IconicsDrawable(requireContext(), GoogleMaterial.Icon.gmd_explore).apply {
sizeDp = 24
paddingDp = 20
color = IconicsColor.colorRes(R.color.colorDrawing)
}, null, null, null)
return view
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -86,30 +87,27 @@ class SearchDiscoverFragment : BaseFragment() {
accessToken = db.userDao().getActiveUser()?.accessToken.orEmpty()
discoverProgressBar = view.findViewById(R.id.discoverProgressBar)
discoverRefreshLayout = view.findViewById(R.id.discoverRefreshLayout)
getDiscover()
discoverRefreshLayout.setOnRefreshListener {
binding.discoverRefreshLayout.setOnRefreshListener {
getDiscover()
}
}
fun showError(@StringRes errorText: Int = R.string.loading_toast, show: Boolean = true){
val motionLayout = view?.findViewById<MotionLayout>(R.id.motionLayout)
if(show){
motionLayout?.transitionToEnd()
} else {
motionLayout?.transitionToStart()
binding.motionLayout.apply {
if(show){
transitionToEnd()
} else {
transitionToStart()
}
}
discoverRefreshLayout.isRefreshing = false
discoverProgressBar.visibility = View.GONE
binding.discoverRefreshLayout.isRefreshing = false
binding.discoverProgressBar.visibility = View.GONE
}
private fun getDiscover() {
lifecycleScope.launchWhenCreated {
try {
val discoverPosts = api.discover("Bearer $accessToken")

View File

@ -34,6 +34,7 @@
android:layout_marginStart="8dp"
android:gravity="center"
android:text="@string/default_nposts"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/profilePictureImageView"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toEndOf="@+id/profilePictureImageView"
@ -45,6 +46,7 @@
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/default_nfollowers"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/nbPostsTextView"
app:layout_constraintEnd_toStartOf="@+id/nbFollowingTextView"
app:layout_constraintStart_toEndOf="@+id/nbPostsTextView"
@ -57,6 +59,7 @@
android:layout_marginEnd="8dp"
android:gravity="center"
android:text="@string/default_nfollowing"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/nbFollowersTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/nbFollowersTextView" />
@ -69,9 +72,10 @@
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:text="@string/no_username"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/profilePictureImageView"/>
app:layout_constraintTop_toBottomOf="@id/profilePictureImageView" />
<TextView