Convert DirectPendingInboxFragment to kotlin
This commit is contained in:
parent
ffafda0085
commit
01c944486d
@ -1,175 +1,148 @@
|
|||||||
package awais.instagrabber.fragments.directmessages;
|
package awais.instagrabber.fragments.directmessages
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.res.Configuration
|
||||||
import android.content.res.Configuration;
|
import android.os.Bundle
|
||||||
import android.os.Bundle;
|
import android.view.LayoutInflater
|
||||||
import android.view.LayoutInflater;
|
import android.view.View
|
||||||
import android.view.View;
|
import android.view.ViewGroup
|
||||||
import android.view.ViewGroup;
|
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.fragment.app.activityViewModels
|
||||||
|
import androidx.lifecycle.Observer
|
||||||
|
import androidx.navigation.fragment.NavHostFragment
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
|
||||||
|
import awais.instagrabber.activities.MainActivity
|
||||||
|
import awais.instagrabber.adapters.DirectMessageInboxAdapter
|
||||||
|
import awais.instagrabber.customviews.helpers.RecyclerLazyLoaderAtEdge
|
||||||
|
import awais.instagrabber.databinding.FragmentDirectPendingInboxBinding
|
||||||
|
import awais.instagrabber.models.Resource
|
||||||
|
import awais.instagrabber.repositories.responses.directmessages.DirectInbox
|
||||||
|
import awais.instagrabber.repositories.responses.directmessages.DirectThread
|
||||||
|
import awais.instagrabber.viewmodels.DirectPendingInboxViewModel
|
||||||
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
class DirectPendingInboxFragment : Fragment(), OnRefreshListener {
|
||||||
import androidx.annotation.Nullable;
|
private val viewModel: DirectPendingInboxViewModel by activityViewModels()
|
||||||
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
|
||||||
import androidx.fragment.app.Fragment;
|
|
||||||
import androidx.lifecycle.Observer;
|
|
||||||
import androidx.lifecycle.ViewModelProvider;
|
|
||||||
import androidx.navigation.fragment.NavHostFragment;
|
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
||||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
||||||
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
private lateinit var root: CoordinatorLayout
|
||||||
|
private lateinit var lazyLoader: RecyclerLazyLoaderAtEdge
|
||||||
|
private lateinit var binding: FragmentDirectPendingInboxBinding
|
||||||
|
private lateinit var fragmentActivity: MainActivity
|
||||||
|
private lateinit var inboxAdapter: DirectMessageInboxAdapter
|
||||||
|
|
||||||
import java.util.Collections;
|
private var shouldRefresh = true
|
||||||
import java.util.List;
|
private var scrollToTop = false
|
||||||
|
private var navigating = false
|
||||||
|
private var threadsObserver: Observer<List<DirectThread?>>? = null
|
||||||
|
|
||||||
import awais.instagrabber.activities.MainActivity;
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
import awais.instagrabber.adapters.DirectMessageInboxAdapter;
|
super.onCreate(savedInstanceState)
|
||||||
import awais.instagrabber.customviews.helpers.RecyclerLazyLoaderAtEdge;
|
fragmentActivity = requireActivity() as MainActivity
|
||||||
import awais.instagrabber.databinding.FragmentDirectPendingInboxBinding;
|
|
||||||
import awais.instagrabber.repositories.responses.directmessages.DirectThread;
|
|
||||||
import awais.instagrabber.viewmodels.DirectPendingInboxViewModel;
|
|
||||||
|
|
||||||
public class DirectPendingInboxFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
|
|
||||||
private static final String TAG = DirectPendingInboxFragment.class.getSimpleName();
|
|
||||||
|
|
||||||
private CoordinatorLayout root;
|
|
||||||
private RecyclerLazyLoaderAtEdge lazyLoader;
|
|
||||||
private DirectPendingInboxViewModel viewModel;
|
|
||||||
private boolean shouldRefresh = true;
|
|
||||||
private FragmentDirectPendingInboxBinding binding;
|
|
||||||
private DirectMessageInboxAdapter inboxAdapter;
|
|
||||||
private MainActivity fragmentActivity;
|
|
||||||
private boolean scrollToTop = false;
|
|
||||||
private boolean navigating;
|
|
||||||
private Observer<List<DirectThread>> threadsObserver;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
fragmentActivity = (MainActivity) getActivity();
|
|
||||||
if (fragmentActivity != null) {
|
|
||||||
viewModel = new ViewModelProvider(fragmentActivity).get(DirectPendingInboxViewModel.class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 = FragmentDirectPendingInboxBinding.inflate(inflater, container, false);
|
binding = FragmentDirectPendingInboxBinding.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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun onResume() {
|
||||||
public void onResume() {
|
super.onResume()
|
||||||
super.onResume();
|
setupObservers()
|
||||||
setupObservers();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||||
public void onConfigurationChanged(@NonNull final Configuration newConfig) {
|
super.onConfigurationChanged(newConfig)
|
||||||
super.onConfigurationChanged(newConfig);
|
init()
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun onDestroy() {
|
||||||
public void onDestroy() {
|
super.onDestroy()
|
||||||
super.onDestroy();
|
removeViewModelObservers()
|
||||||
removeViewModelObservers();
|
viewModel.onDestroy()
|
||||||
viewModel.onDestroy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupObservers() {
|
private fun setupObservers() {
|
||||||
removeViewModelObservers();
|
removeViewModelObservers()
|
||||||
threadsObserver = list -> {
|
threadsObserver = Observer { list: List<DirectThread?>? ->
|
||||||
if (inboxAdapter == null) return;
|
if (!this::inboxAdapter.isInitialized) return@Observer
|
||||||
if (binding.swipeRefreshLayout.getVisibility() == View.GONE) {
|
if (binding.swipeRefreshLayout.visibility == View.GONE) {
|
||||||
binding.swipeRefreshLayout.setVisibility(View.VISIBLE);
|
binding.swipeRefreshLayout.visibility = View.VISIBLE
|
||||||
binding.empty.setVisibility(View.GONE);
|
binding.empty.visibility = View.GONE
|
||||||
|
}
|
||||||
|
inboxAdapter.submitList(list ?: emptyList()) {
|
||||||
|
if (!scrollToTop) return@submitList
|
||||||
|
binding.pendingInboxList.smoothScrollToPosition(0)
|
||||||
|
scrollToTop = false
|
||||||
}
|
}
|
||||||
inboxAdapter.submitList(list == null ? Collections.emptyList() : list, () -> {
|
|
||||||
if (!scrollToTop) return;
|
|
||||||
binding.pendingInboxList.smoothScrollToPosition(0);
|
|
||||||
scrollToTop = false;
|
|
||||||
});
|
|
||||||
if (list == null || list.isEmpty()) {
|
if (list == null || list.isEmpty()) {
|
||||||
binding.swipeRefreshLayout.setVisibility(View.GONE);
|
binding.swipeRefreshLayout.visibility = View.GONE
|
||||||
binding.empty.setVisibility(View.VISIBLE);
|
binding.empty.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
viewModel.getThreads().observe(fragmentActivity, threadsObserver);
|
threadsObserver?.let { viewModel.threads.observe(fragmentActivity, it) }
|
||||||
viewModel.getInbox().observe(getViewLifecycleOwner(), inboxResource -> {
|
viewModel.inbox.observe(viewLifecycleOwner, { inboxResource: Resource<DirectInbox?>? ->
|
||||||
if (inboxResource == null) return;
|
if (inboxResource == null) return@observe
|
||||||
switch (inboxResource.status) {
|
when (inboxResource.status) {
|
||||||
case SUCCESS:
|
Resource.Status.SUCCESS -> binding.swipeRefreshLayout.isRefreshing = false
|
||||||
binding.swipeRefreshLayout.setRefreshing(false);
|
Resource.Status.ERROR -> {
|
||||||
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()
|
||||||
}
|
}
|
||||||
binding.swipeRefreshLayout.setRefreshing(false);
|
binding.swipeRefreshLayout.isRefreshing = false
|
||||||
break;
|
|
||||||
case LOADING:
|
|
||||||
binding.swipeRefreshLayout.setRefreshing(true);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
});
|
Resource.Status.LOADING -> binding.swipeRefreshLayout.isRefreshing = true
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeViewModelObservers() {
|
private fun removeViewModelObservers() {
|
||||||
if (viewModel == null) return;
|
threadsObserver?.let { viewModel.threads.removeObserver(it) }
|
||||||
if (threadsObserver != null) {
|
|
||||||
viewModel.getThreads().removeObserver(threadsObserver);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.pendingInboxList.setHasFixedSize(true)
|
||||||
binding.pendingInboxList.setHasFixedSize(true);
|
binding.pendingInboxList.setItemViewCacheSize(20)
|
||||||
binding.pendingInboxList.setItemViewCacheSize(20);
|
val layoutManager = LinearLayoutManager(context)
|
||||||
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
|
binding.pendingInboxList.layoutManager = layoutManager
|
||||||
binding.pendingInboxList.setLayoutManager(layoutManager);
|
inboxAdapter = DirectMessageInboxAdapter { thread ->
|
||||||
inboxAdapter = new DirectMessageInboxAdapter(thread -> {
|
if (navigating) return@DirectMessageInboxAdapter
|
||||||
if (navigating) return;
|
val threadId = thread.threadId ?: return@DirectMessageInboxAdapter
|
||||||
navigating = true;
|
val threadTitle = thread.threadTitle ?: return@DirectMessageInboxAdapter
|
||||||
if (isAdded()) {
|
navigating = true
|
||||||
final DirectPendingInboxFragmentDirections.ActionPendingInboxToThread directions = DirectPendingInboxFragmentDirections
|
if (isAdded) {
|
||||||
.actionPendingInboxToThread(thread.getThreadId(), thread.getThreadTitle());
|
val directions = DirectPendingInboxFragmentDirections.actionPendingInboxToThread(threadId, threadTitle)
|
||||||
directions.setPending(true);
|
directions.pending = true
|
||||||
NavHostFragment.findNavController(this).navigate(directions);
|
NavHostFragment.findNavController(this).navigate(directions)
|
||||||
}
|
}
|
||||||
navigating = false;
|
navigating = false
|
||||||
});
|
}
|
||||||
inboxAdapter.setHasStableIds(true);
|
inboxAdapter.setHasStableIds(true)
|
||||||
binding.pendingInboxList.setAdapter(inboxAdapter);
|
binding.pendingInboxList.adapter = inboxAdapter
|
||||||
lazyLoader = new RecyclerLazyLoaderAtEdge(layoutManager, page -> {
|
lazyLoader = RecyclerLazyLoaderAtEdge(layoutManager) { viewModel.fetchInbox() }
|
||||||
if (viewModel == null) return;
|
binding.pendingInboxList.addOnScrollListener(lazyLoader)
|
||||||
viewModel.fetchInbox();
|
|
||||||
});
|
|
||||||
binding.pendingInboxList.addOnScrollListener(lazyLoader);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user