Convert DirectMessageInboxFragment to kotlin

This commit is contained in:
Ammar Githam 2021-06-01 20:03:00 +09:00
parent 652c57aa24
commit fd3b1a8d18
2 changed files with 192 additions and 237 deletions

View File

@ -753,10 +753,9 @@ class MainActivity : BaseLanguageActivity(), FragmentManager.OnBackStackChangedL
EmojiCompat.init(config) EmojiCompat.init(config)
} }
var toolbar: Toolbar? var toolbar: Toolbar
get() = binding.toolbar get() = binding.toolbar
set(toolbar) { set(toolbar) {
toolbar ?: return
binding.appBarLayout.visibility = View.GONE binding.appBarLayout.visibility = View.GONE
removeScrollingBehaviour() removeScrollingBehaviour()
setSupportActionBar(toolbar) setSupportActionBar(toolbar)

View File

@ -1,282 +1,238 @@
package awais.instagrabber.fragments.directmessages; package awais.instagrabber.fragments.directmessages
import android.annotation.SuppressLint; import android.annotation.SuppressLint
import android.content.Context; import android.content.res.Configuration
import android.content.IntentFilter; import android.os.Bundle
import android.content.res.Configuration; import android.os.Handler
import android.os.Bundle; import android.os.Looper
import android.os.Handler; import android.util.Log
import android.util.Log; import android.view.*
import android.view.LayoutInflater; import androidx.coordinatorlayout.widget.CoordinatorLayout
import android.view.Menu; import androidx.fragment.app.Fragment
import android.view.MenuInflater; import androidx.lifecycle.Observer
import android.view.MenuItem; import androidx.lifecycle.ViewModelProvider
import android.view.View; import androidx.navigation.fragment.NavHostFragment
import android.view.ViewGroup; import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
import awais.instagrabber.R
import awais.instagrabber.activities.MainActivity
import awais.instagrabber.adapters.DirectMessageInboxAdapter
import awais.instagrabber.customviews.helpers.RecyclerLazyLoaderAtEdge
import awais.instagrabber.databinding.FragmentDirectMessagesInboxBinding
import awais.instagrabber.models.Resource
import awais.instagrabber.repositories.responses.directmessages.DirectInbox
import awais.instagrabber.repositories.responses.directmessages.DirectThread
import awais.instagrabber.utils.extensions.TAG
import awais.instagrabber.viewmodels.DirectInboxViewModel
import com.google.android.material.badge.BadgeDrawable
import com.google.android.material.badge.BadgeUtils
import com.google.android.material.internal.ToolbarUtils
import com.google.android.material.snackbar.Snackbar
import androidx.annotation.NonNull; class DirectMessageInboxFragment : Fragment(), OnRefreshListener {
import androidx.annotation.Nullable; private lateinit var fragmentActivity: MainActivity
import androidx.appcompat.view.menu.ActionMenuItemView; private lateinit var viewModel: DirectInboxViewModel
import androidx.coordinatorlayout.widget.CoordinatorLayout; private lateinit var root: CoordinatorLayout
import androidx.fragment.app.Fragment; private lateinit var binding: FragmentDirectMessagesInboxBinding
import androidx.lifecycle.Observer; private lateinit var inboxAdapter: DirectMessageInboxAdapter
import androidx.lifecycle.ViewModelProvider; private lateinit var lazyLoader: RecyclerLazyLoaderAtEdge
import androidx.navigation.NavDirections;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.badge.BadgeDrawable; private var shouldRefresh = true
import com.google.android.material.badge.BadgeUtils; // private var receiver: DMRefreshBroadcastReceiver? = null
import com.google.android.material.internal.ToolbarUtils; private var scrollToTop = false
import com.google.android.material.snackbar.Snackbar; private var navigating = false
private var threadsObserver: Observer<List<DirectThread?>>? = null
private var pendingRequestsMenuItem: MenuItem? = null
private var pendingRequestTotalBadgeDrawable: BadgeDrawable? = null
private var isPendingRequestTotalBadgeAttached = false
import java.util.List; override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
import awais.instagrabber.R; fragmentActivity = requireActivity() as MainActivity
import awais.instagrabber.activities.MainActivity; viewModel = ViewModelProvider(fragmentActivity).get(DirectInboxViewModel::class.java)
import awais.instagrabber.adapters.DirectMessageInboxAdapter; setHasOptionsMenu(true)
import awais.instagrabber.broadcasts.DMRefreshBroadcastReceiver;
import awais.instagrabber.customviews.helpers.RecyclerLazyLoaderAtEdge;
import awais.instagrabber.databinding.FragmentDirectMessagesInboxBinding;
import awais.instagrabber.repositories.responses.directmessages.DirectThread;
import awais.instagrabber.viewmodels.DirectInboxViewModel;
public class DirectMessageInboxFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = "DirectMessagesInboxFrag";
private CoordinatorLayout root;
private RecyclerLazyLoaderAtEdge lazyLoader;
private DirectInboxViewModel viewModel;
private boolean shouldRefresh = true;
private FragmentDirectMessagesInboxBinding binding;
private DMRefreshBroadcastReceiver receiver;
private DirectMessageInboxAdapter inboxAdapter;
private MainActivity fragmentActivity;
private boolean scrollToTop = false;
private boolean navigating;
private Observer<List<DirectThread>> threadsObserver;
private MenuItem pendingRequestsMenuItem;
private BadgeDrawable pendingRequestTotalBadgeDrawable;
private boolean isPendingRequestTotalBadgeAttached;
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentActivity = (MainActivity) getActivity();
if (fragmentActivity != null) {
viewModel = new ViewModelProvider(fragmentActivity).get(DirectInboxViewModel.class);
}
setHasOptionsMenu(true);
} }
@Override override fun onCreateView(
public View onCreateView(@NonNull final LayoutInflater inflater, inflater: LayoutInflater,
final ViewGroup container, container: ViewGroup?,
final Bundle savedInstanceState) { savedInstanceState: Bundle?,
if (root != null) { ): View {
shouldRefresh = false; if (this::root.isInitialized) {
return root; shouldRefresh = false
return root
} }
binding = FragmentDirectMessagesInboxBinding.inflate(inflater, container, false); binding = FragmentDirectMessagesInboxBinding.inflate(inflater, container, false)
root = binding.getRoot(); root = binding.root
return root; return root
} }
@Override override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) { if (!shouldRefresh) return
if (!shouldRefresh) return; init()
init();
} }
@Override override fun onRefresh() {
public void onRefresh() { lazyLoader.resetState()
lazyLoader.resetState(); scrollToTop = true
scrollToTop = true; viewModel.refresh()
if (viewModel != null) { }
viewModel.refresh();
@SuppressLint("UnsafeExperimentalUsageError", "UnsafeOptInUsageError")
override fun onPause() {
super.onPause()
// unregisterReceiver()
isPendingRequestTotalBadgeAttached = false
pendingRequestsMenuItem?.let {
@SuppressLint("RestrictedApi") val menuItemView = ToolbarUtils.getActionMenuItemView(fragmentActivity.toolbar, it.itemId)
if (menuItemView != null) {
BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.toolbar, it.itemId)
pendingRequestTotalBadgeDrawable = null
}
} }
} }
@SuppressLint({"UnsafeExperimentalUsageError", "UnsafeOptInUsageError"}) override fun onResume() {
@Override super.onResume()
public void onPause() { setupObservers()
super.onPause(); // val context = context ?: return
unregisterReceiver(); // receiver = DMRefreshBroadcastReceiver { Log.d(TAG, "onResume: broadcast received") }
isPendingRequestTotalBadgeAttached = false; // context.registerReceiver(receiver, IntentFilter(DMRefreshBroadcastReceiver.ACTION_REFRESH_DM))
@SuppressLint("RestrictedApi") final ActionMenuItemView menuItemView = ToolbarUtils
.getActionMenuItemView(fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId());
if (pendingRequestTotalBadgeDrawable != null && menuItemView != null) {
BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId());
pendingRequestTotalBadgeDrawable = null;
}
} }
@Override override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
public void onResume() { inflater.inflate(R.menu.dm_inbox_menu, menu)
super.onResume(); pendingRequestsMenuItem = menu.findItem(R.id.pending_requests)
setupObservers(); pendingRequestsMenuItem?.isVisible = isPendingRequestTotalBadgeAttached
final Context context = getContext();
if (context == null) return;
receiver = new DMRefreshBroadcastReceiver(() -> {
Log.d(TAG, "onResume: broadcast received");
// refreshInbox = true;
});
context.registerReceiver(receiver, new IntentFilter(DMRefreshBroadcastReceiver.ACTION_REFRESH_DM));
} }
@SuppressLint("UnsafeExperimentalUsageError") override fun onOptionsItemSelected(item: MenuItem): Boolean {
@Override if (item.itemId == R.id.pending_requests) {
public void onDestroyView() { val directions = DirectMessageInboxFragmentDirections.actionInboxToPendingInbox()
super.onDestroyView();
unregisterReceiver();
}
@Override
public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) {
inflater.inflate(R.menu.dm_inbox_menu, menu);
pendingRequestsMenuItem = menu.findItem(R.id.pending_requests);
pendingRequestsMenuItem.setVisible(isPendingRequestTotalBadgeAttached);
}
@Override
public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
if (item.getItemId() == R.id.pending_requests) {
final NavDirections directions = DirectMessageInboxFragmentDirections.actionInboxToPendingInbox();
try { try {
NavHostFragment.findNavController(this).navigate(directions); NavHostFragment.findNavController(this).navigate(directions)
} catch (Exception e) { } catch (e: Exception) {
Log.e(TAG, "onOptionsItemSelected: ", e); Log.e(TAG, "onOptionsItemSelected: ", e)
} }
return true; return true
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item)
} }
private void unregisterReceiver() { // private fun unregisterReceiver() {
if (receiver == null) return; // if (receiver == null) return
final Context context = getContext(); // val context = context ?: return
if (context == null) return; // context.unregisterReceiver(receiver)
context.unregisterReceiver(receiver); // receiver = null
receiver = null; // }
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
init()
} }
@Override override fun onDestroy() {
public void onConfigurationChanged(@NonNull final Configuration newConfig) { super.onDestroy()
super.onConfigurationChanged(newConfig); removeViewModelObservers()
init(); viewModel.onDestroy()
} }
@Override private fun setupObservers() {
public void onDestroy() { removeViewModelObservers()
super.onDestroy(); threadsObserver = Observer { list: List<DirectThread?> ->
removeViewModelObservers(); inboxAdapter.submitList(list) {
viewModel.onDestroy(); if (!scrollToTop) return@submitList
binding.inboxList.post { binding.inboxList.smoothScrollToPosition(0) }
scrollToTop = false
} }
}
private void setupObservers() { threadsObserver?.let { viewModel.threads.observe(fragmentActivity, it) }
removeViewModelObservers(); viewModel.inbox.observe(viewLifecycleOwner, { inboxResource: Resource<DirectInbox?>? ->
threadsObserver = list -> { if (inboxResource == null) return@observe
if (inboxAdapter == null) return; when (inboxResource.status) {
inboxAdapter.submitList(list, () -> { Resource.Status.SUCCESS -> binding.swipeRefreshLayout.isRefreshing = false
if (!scrollToTop) return; Resource.Status.ERROR -> {
binding.inboxList.post(() -> binding.inboxList.smoothScrollToPosition(0));
scrollToTop = false;
});
};
viewModel.getThreads().observe(fragmentActivity, threadsObserver);
viewModel.getInbox().observe(getViewLifecycleOwner(), inboxResource -> {
if (inboxResource == null) return;
switch (inboxResource.status) {
case SUCCESS:
binding.swipeRefreshLayout.setRefreshing(false);
break;
case ERROR:
if (inboxResource.message != null) { if (inboxResource.message != null) {
Snackbar.make(binding.getRoot(), inboxResource.message, Snackbar.LENGTH_LONG).show(); Snackbar.make(binding.root, inboxResource.message, Snackbar.LENGTH_LONG).show()
} }
if (inboxResource.resId != 0) { if (inboxResource.resId != 0) {
Snackbar.make(binding.getRoot(), inboxResource.resId, Snackbar.LENGTH_LONG).show(); Snackbar.make(binding.root, inboxResource.resId, Snackbar.LENGTH_LONG).show()
} }
binding.swipeRefreshLayout.setRefreshing(false); binding.swipeRefreshLayout.isRefreshing = false
break;
case LOADING:
binding.swipeRefreshLayout.setRefreshing(true);
break;
} }
}); Resource.Status.LOADING -> binding.swipeRefreshLayout.isRefreshing = true
viewModel.getPendingRequestsTotal().observe(getViewLifecycleOwner(), this::attachPendingRequestsBadge); }
})
viewModel.pendingRequestsTotal.observe(viewLifecycleOwner, { count: Int? -> attachPendingRequestsBadge(count) })
} }
@SuppressLint({"UnsafeExperimentalUsageError", "UnsafeOptInUsageError"}) @SuppressLint("UnsafeExperimentalUsageError", "UnsafeOptInUsageError")
private void attachPendingRequestsBadge(@Nullable final Integer count) { private fun attachPendingRequestsBadge(count: Int?) {
if (pendingRequestsMenuItem == null) { val pendingRequestsMenuItem1 = pendingRequestsMenuItem
final Handler handler = new Handler(); if (pendingRequestsMenuItem1 == null) {
handler.postDelayed(() -> attachPendingRequestsBadge(count), 500); val handler = Handler(Looper.getMainLooper())
return; handler.postDelayed({ attachPendingRequestsBadge(count) }, 500)
return
} }
if (pendingRequestTotalBadgeDrawable == null) { if (pendingRequestTotalBadgeDrawable == null) {
final Context context = getContext(); val context = context ?: return
if (context == null) return; pendingRequestTotalBadgeDrawable = BadgeDrawable.create(context)
pendingRequestTotalBadgeDrawable = BadgeDrawable.create(context);
} }
if (count == null || count == 0) { if (count == null || count == 0) {
@SuppressLint("RestrictedApi") final ActionMenuItemView menuItemView = ToolbarUtils @SuppressLint("RestrictedApi") val menuItemView = ToolbarUtils.getActionMenuItemView(
.getActionMenuItemView(fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId()); fragmentActivity.toolbar,
pendingRequestsMenuItem1.itemId
)
if (menuItemView != null) { if (menuItemView != null) {
BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId()); BadgeUtils.detachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.toolbar, pendingRequestsMenuItem1.itemId)
} }
isPendingRequestTotalBadgeAttached = false; isPendingRequestTotalBadgeAttached = false
pendingRequestTotalBadgeDrawable.setNumber(0); pendingRequestTotalBadgeDrawable?.number = 0
pendingRequestsMenuItem.setVisible(false); pendingRequestsMenuItem1.isVisible = false
return; return
} }
pendingRequestsMenuItem.setVisible(true); pendingRequestsMenuItem1.isVisible = true
if (pendingRequestTotalBadgeDrawable.getNumber() == count) return; if (pendingRequestTotalBadgeDrawable?.number == count) return
pendingRequestTotalBadgeDrawable.setNumber(count); pendingRequestTotalBadgeDrawable?.number = count
if (!isPendingRequestTotalBadgeAttached) { if (!isPendingRequestTotalBadgeAttached) {
BadgeUtils.attachBadgeDrawable(pendingRequestTotalBadgeDrawable, fragmentActivity.getToolbar(), pendingRequestsMenuItem.getItemId()); pendingRequestTotalBadgeDrawable?.let {
isPendingRequestTotalBadgeAttached = true; BadgeUtils.attachBadgeDrawable(it, fragmentActivity.toolbar, pendingRequestsMenuItem1.itemId)
isPendingRequestTotalBadgeAttached = true
}
} }
} }
private void removeViewModelObservers() { private fun removeViewModelObservers() {
if (viewModel == null) return; threadsObserver?.let { viewModel.threads.removeObserver(it) }
if (threadsObserver != null) {
viewModel.getThreads().removeObserver(threadsObserver);
}
// no need to explicitly remove observers whose lifecycle owner is getViewLifecycleOwner // no need to explicitly remove observers whose lifecycle owner is getViewLifecycleOwner
} }
private void init() { private fun init() {
final Context context = getContext(); val context = context ?: return
if (context == null) return; setupObservers()
setupObservers(); binding.swipeRefreshLayout.setOnRefreshListener(this)
binding.swipeRefreshLayout.setOnRefreshListener(this); binding.inboxList.setHasFixedSize(true)
binding.inboxList.setHasFixedSize(true); binding.inboxList.setItemViewCacheSize(20)
binding.inboxList.setItemViewCacheSize(20); val layoutManager = LinearLayoutManager(context)
final LinearLayoutManager layoutManager = new LinearLayoutManager(context); binding.inboxList.layoutManager = layoutManager
binding.inboxList.setLayoutManager(layoutManager); inboxAdapter = DirectMessageInboxAdapter { thread ->
inboxAdapter = new DirectMessageInboxAdapter(thread -> { val threadId = thread.threadId
if (navigating) return; val threadTitle = thread.threadTitle
navigating = true; if (navigating || threadId.isNullOrBlank() || threadTitle.isNullOrBlank()) return@DirectMessageInboxAdapter
if (isAdded()) { navigating = true
final DirectMessageInboxFragmentDirections.ActionInboxToThread directions = DirectMessageInboxFragmentDirections if (isAdded) {
.actionInboxToThread(thread.getThreadId(), thread.getThreadTitle()); val directions = DirectMessageInboxFragmentDirections.actionInboxToThread(threadId, threadTitle)
try { try {
NavHostFragment.findNavController(this).navigate(directions); NavHostFragment.findNavController(this).navigate(directions)
} catch (Exception e) { } catch (e: Exception) {
Log.e(TAG, "init: ", e); Log.e(TAG, "init: ", e)
} }
} }
navigating = false; navigating = false
}); }
inboxAdapter.setHasStableIds(true); inboxAdapter.setHasStableIds(true)
binding.inboxList.setAdapter(inboxAdapter); binding.inboxList.adapter = inboxAdapter
lazyLoader = new RecyclerLazyLoaderAtEdge(layoutManager, page -> { lazyLoader = RecyclerLazyLoaderAtEdge(layoutManager) { viewModel.fetchInbox() }
if (viewModel == null) return; lazyLoader.let { binding.inboxList.addOnScrollListener(it) }
viewModel.fetchInbox();
});
binding.inboxList.addOnScrollListener(lazyLoader);
} }
} }