1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +00:00

Some refactoring and fixes

This commit is contained in:
Ammar Githam 2021-06-01 20:54:20 +09:00
parent fd3b1a8d18
commit da41393177

View File

@ -9,8 +9,8 @@ import android.util.Log
import android.view.* import android.view.*
import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
@ -30,15 +30,15 @@ import com.google.android.material.internal.ToolbarUtils
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
class DirectMessageInboxFragment : Fragment(), OnRefreshListener { class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
private val viewModel: DirectInboxViewModel by activityViewModels()
private lateinit var fragmentActivity: MainActivity private lateinit var fragmentActivity: MainActivity
private lateinit var viewModel: DirectInboxViewModel
private lateinit var root: CoordinatorLayout private lateinit var root: CoordinatorLayout
private lateinit var binding: FragmentDirectMessagesInboxBinding private lateinit var binding: FragmentDirectMessagesInboxBinding
private lateinit var inboxAdapter: DirectMessageInboxAdapter private lateinit var inboxAdapter: DirectMessageInboxAdapter
private lateinit var lazyLoader: RecyclerLazyLoaderAtEdge private lateinit var lazyLoader: RecyclerLazyLoaderAtEdge
private var shouldRefresh = true private var shouldRefresh = true
// private var receiver: DMRefreshBroadcastReceiver? = null
private var scrollToTop = false private var scrollToTop = false
private var navigating = false private var navigating = false
private var threadsObserver: Observer<List<DirectThread?>>? = null private var threadsObserver: Observer<List<DirectThread?>>? = null
@ -49,7 +49,6 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
fragmentActivity = requireActivity() as MainActivity fragmentActivity = requireActivity() as MainActivity
viewModel = ViewModelProvider(fragmentActivity).get(DirectInboxViewModel::class.java)
setHasOptionsMenu(true) setHasOptionsMenu(true)
} }
@ -78,13 +77,12 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
viewModel.refresh() viewModel.refresh()
} }
@SuppressLint("UnsafeExperimentalUsageError", "UnsafeOptInUsageError") @SuppressLint("UnsafeExperimentalUsageError", "UnsafeOptInUsageError", "RestrictedApi")
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
// unregisterReceiver()
isPendingRequestTotalBadgeAttached = false isPendingRequestTotalBadgeAttached = false
pendingRequestsMenuItem?.let { pendingRequestsMenuItem?.let {
@SuppressLint("RestrictedApi") val menuItemView = ToolbarUtils.getActionMenuItemView(fragmentActivity.toolbar, it.itemId) val menuItemView = ToolbarUtils.getActionMenuItemView(fragmentActivity.toolbar, it.itemId)
if (menuItemView != null) { if (menuItemView != null) {
BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.toolbar, it.itemId) BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.toolbar, it.itemId)
pendingRequestTotalBadgeDrawable = null pendingRequestTotalBadgeDrawable = null
@ -95,9 +93,6 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
setupObservers() setupObservers()
// val context = context ?: return
// receiver = DMRefreshBroadcastReceiver { Log.d(TAG, "onResume: broadcast received") }
// context.registerReceiver(receiver, IntentFilter(DMRefreshBroadcastReceiver.ACTION_REFRESH_DM))
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
@ -119,13 +114,6 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
// private fun unregisterReceiver() {
// if (receiver == null) return
// val context = context ?: return
// context.unregisterReceiver(receiver)
// receiver = null
// }
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) super.onConfigurationChanged(newConfig)
init() init()
@ -140,6 +128,7 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
private fun setupObservers() { private fun setupObservers() {
removeViewModelObservers() removeViewModelObservers()
threadsObserver = Observer { list: List<DirectThread?> -> threadsObserver = Observer { list: List<DirectThread?> ->
if (!this::inboxAdapter.isInitialized) return@Observer
inboxAdapter.submitList(list) { inboxAdapter.submitList(list) {
if (!scrollToTop) return@submitList if (!scrollToTop) return@submitList
binding.inboxList.post { binding.inboxList.smoothScrollToPosition(0) } binding.inboxList.post { binding.inboxList.smoothScrollToPosition(0) }
@ -166,7 +155,7 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
viewModel.pendingRequestsTotal.observe(viewLifecycleOwner, { count: Int? -> attachPendingRequestsBadge(count) }) viewModel.pendingRequestsTotal.observe(viewLifecycleOwner, { count: Int? -> attachPendingRequestsBadge(count) })
} }
@SuppressLint("UnsafeExperimentalUsageError", "UnsafeOptInUsageError") @SuppressLint("UnsafeExperimentalUsageError", "UnsafeOptInUsageError", "RestrictedApi")
private fun attachPendingRequestsBadge(count: Int?) { private fun attachPendingRequestsBadge(count: Int?) {
val pendingRequestsMenuItem1 = pendingRequestsMenuItem val pendingRequestsMenuItem1 = pendingRequestsMenuItem
if (pendingRequestsMenuItem1 == null) { if (pendingRequestsMenuItem1 == null) {
@ -179,7 +168,7 @@ class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
pendingRequestTotalBadgeDrawable = BadgeDrawable.create(context) pendingRequestTotalBadgeDrawable = BadgeDrawable.create(context)
} }
if (count == null || count == 0) { if (count == null || count == 0) {
@SuppressLint("RestrictedApi") val menuItemView = ToolbarUtils.getActionMenuItemView( val menuItemView = ToolbarUtils.getActionMenuItemView(
fragmentActivity.toolbar, fragmentActivity.toolbar,
pendingRequestsMenuItem1.itemId pendingRequestsMenuItem1.itemId
) )