convert conferenceGroupFragment to Kotlin

This commit is contained in:
Felix 2020-04-08 18:05:51 +02:00
parent 0e02c9ade9
commit 1dace55209
2 changed files with 101 additions and 121 deletions

View file

@ -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;
}
}

View file

@ -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<List<Conference>> { conferenceList: List<Conference>? ->
if (conferenceList != null) {
if (conferenceList.isNotEmpty()) {
setLoadingOverlayVisibility(false)
}
conferencesAdapter.conferences = conferenceList
val layoutState = arguments?.getParcelable<Parcelable>(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
}
}
}