From 1dace55209977c07a6ae95e602fb8d95ec7bd1b1 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 8 Apr 2020 18:05:51 +0200 Subject: [PATCH] convert conferenceGroupFragment to Kotlin --- .../touch/browse/ConferenceGroupFragment.java | 121 ------------------ .../touch/browse/ConferenceGroupFragment.kt | 101 +++++++++++++++ 2 files changed, 101 insertions(+), 121 deletions(-) delete mode 100644 touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferenceGroupFragment.java create mode 100644 touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferenceGroupFragment.kt diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferenceGroupFragment.java b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferenceGroupFragment.java deleted file mode 100644 index 0d0a5fc1..00000000 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferenceGroupFragment.java +++ /dev/null @@ -1,121 +0,0 @@ -package de.nicidienase.chaosflix.touch.browse; - -import android.content.Context; -import android.os.Bundle; -import android.os.Parcelable; -import androidx.recyclerview.widget.GridLayoutManager; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.recyclerview.widget.StaggeredGridLayoutManager; - -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import de.nicidienase.chaosflix.touch.R; -import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.ConferenceGroup; -import de.nicidienase.chaosflix.touch.browse.adapters.ConferenceRecyclerViewAdapter; - -public class ConferenceGroupFragment extends BrowseFragment { - - private static final String TAG = ConferenceGroupFragment.class.getSimpleName(); - - private static final String ARG_COLUMN_COUNT = "column-count"; - private static final String ARG_GROUP = "group-name"; - private static final String LAYOUTMANAGER_STATE = "layoutmanager-state"; - private ConferencesTabBrowseFragment.OnInteractionListener listener; - - private int columnCount = 1; - private ConferenceGroup conferenceGroup; - - private ConferenceRecyclerViewAdapter conferencesAdapter; - - private RecyclerView.LayoutManager layoutManager; - - public ConferenceGroupFragment() { - } - - public static ConferenceGroupFragment newInstance(ConferenceGroup group, int columnCount) { - ConferenceGroupFragment fragment = new ConferenceGroupFragment(); - Bundle args = new Bundle(); - args.putInt(ARG_COLUMN_COUNT, columnCount); - args.putParcelable(ARG_GROUP, group); - fragment.setArguments(args); - return fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - if (getArguments() != null) { - columnCount = getArguments().getInt(ARG_COLUMN_COUNT); - conferenceGroup = getArguments().getParcelable(ARG_GROUP); - } - - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_conferences_page, container, false); - - if (view instanceof RecyclerView) { - Context context = view.getContext(); - RecyclerView recyclerView = (RecyclerView) view; - if (columnCount <= 1) { - layoutManager = new LinearLayoutManager(context); - } else { - layoutManager = new StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL); - } - recyclerView.setLayoutManager(layoutManager); - - conferencesAdapter = new ConferenceRecyclerViewAdapter(listener); - conferencesAdapter.setHasStableIds(true); - recyclerView.setAdapter(conferencesAdapter); - getViewModel().getConferencesByGroup(conferenceGroup.getId()).observe(this, conferenceList -> { - if(conferenceList != null){ - if(conferenceList.size() > 0){ - setLoadingOverlayVisibility(false); - } - conferencesAdapter.setConferences(conferenceList); - Parcelable layoutState = getArguments().getParcelable(LAYOUTMANAGER_STATE); - if (layoutState != null) { - layoutManager.onRestoreInstanceState(layoutState); - } - } - }); - } - return view; - } - - @Override - public void onAttach(Context context) { - super.onAttach(context); - if (context instanceof ConferencesTabBrowseFragment.OnInteractionListener) { - listener = (ConferencesTabBrowseFragment.OnInteractionListener) context; - } else { - throw new RuntimeException(context.toString() + " must implement OnListFragmentInteractionListener"); - } - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - if(layoutManager != null){ - outState.putParcelable(LAYOUTMANAGER_STATE, layoutManager.onSaveInstanceState()); - } - } - - @Override - public void onPause() { - super.onPause(); - if(layoutManager != null){ - getArguments().putParcelable(LAYOUTMANAGER_STATE, layoutManager.onSaveInstanceState()); - } - } - - @Override - public void onDetach() { - super.onDetach(); - listener = null; - } -} \ No newline at end of file diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferenceGroupFragment.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferenceGroupFragment.kt new file mode 100644 index 00000000..fce95241 --- /dev/null +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/browse/ConferenceGroupFragment.kt @@ -0,0 +1,101 @@ +package de.nicidienase.chaosflix.touch.browse + +import android.content.Context +import android.os.Bundle +import android.os.Parcelable +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.lifecycle.Observer +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.StaggeredGridLayoutManager +import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.Conference +import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.ConferenceGroup +import de.nicidienase.chaosflix.touch.R +import de.nicidienase.chaosflix.touch.browse.ConferencesTabBrowseFragment.OnInteractionListener +import de.nicidienase.chaosflix.touch.browse.adapters.ConferenceRecyclerViewAdapter + +class ConferenceGroupFragment : BrowseFragment() { + private var listener: OnInteractionListener? = null + private var layoutManager: RecyclerView.LayoutManager? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + val view = inflater.inflate(R.layout.fragment_conferences_page, container, false) + + + val columnCount = arguments?.getInt(ARG_COLUMN_COUNT, 1) ?: 1 + val conferenceGroup: ConferenceGroup? = arguments?.getParcelable(ARG_GROUP) + + if (view is RecyclerView) { + val context = view.getContext() + layoutManager = if (columnCount <= 1) { + LinearLayoutManager(context) + } else { + StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL) + } + view.layoutManager = layoutManager + val conferencesAdapter: ConferenceRecyclerViewAdapter = ConferenceRecyclerViewAdapter(listener) + conferencesAdapter.setHasStableIds(true) + view.adapter = conferencesAdapter + val groupId = conferenceGroup?.id + if (groupId != null) { + viewModel.getConferencesByGroup(groupId).observe(viewLifecycleOwner, Observer> { conferenceList: List? -> + if (conferenceList != null) { + if (conferenceList.isNotEmpty()) { + setLoadingOverlayVisibility(false) + } + conferencesAdapter.conferences = conferenceList + val layoutState = arguments?.getParcelable(LAYOUTMANAGER_STATE)?.let { + layoutManager?.onRestoreInstanceState(it) + } + } + }) + + } + } + return view + } + + override fun onAttach(context: Context) { + super.onAttach(context) + listener = if (context is OnInteractionListener) { + context + } else { + throw RuntimeException("$context must implement OnListFragmentInteractionListener") + } + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putParcelable(LAYOUTMANAGER_STATE, layoutManager?.onSaveInstanceState()) + + } + + override fun onPause() { + super.onPause() + arguments?.putParcelable(LAYOUTMANAGER_STATE, layoutManager?.onSaveInstanceState()) + + } + + override fun onDetach() { + super.onDetach() + listener = null + } + + companion object { + private val TAG = ConferenceGroupFragment::class.java.simpleName + private const val ARG_COLUMN_COUNT = "column-count" + private const val ARG_GROUP = "group-name" + private const val LAYOUTMANAGER_STATE = "layoutmanager-state" + @JvmStatic + fun newInstance(group: ConferenceGroup?, columnCount: Int): ConferenceGroupFragment { + val fragment = ConferenceGroupFragment() + val args = Bundle() + args.putInt(ARG_COLUMN_COUNT, columnCount) + args.putParcelable(ARG_GROUP, group) + fragment.arguments = args + return fragment + } + } +} \ No newline at end of file