replace Service with ViewModel

This commit is contained in:
Felix 2017-09-25 02:38:54 +02:00
parent b149205937
commit 51a0539d5a
13 changed files with 360 additions and 176 deletions

View file

@ -0,0 +1,98 @@
package de.nicidienase.chaosflix.touch;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelStore;
import android.arch.lifecycle.ViewModelStoreOwner;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import java.util.List;
import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
import de.nicidienase.chaosflix.common.entities.recording.ConferencesWrapper;
import de.nicidienase.chaosflix.common.entities.recording.Event;
import de.nicidienase.chaosflix.common.network.RecordingService;
import de.nicidienase.chaosflix.common.network.StreamingService;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by felix on 24.09.17.
*/
public class ChaosflixViewModel extends ViewModel {
private final StreamingService mStreamingApi;
private final RecordingService mRecordingApi;
public ChaosflixViewModel(String recordingUrl, String streamingUrl){
// Resources resources = context.getResources();
// String recordingUrl = resources.getString(R.string.api_media_ccc_url);
// String streamingUrl = resources.getString(R.string.streaming_media_ccc_url);
OkHttpClient client = new OkHttpClient();
GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create();
RxJava2CallAdapterFactory rxJava2CallAdapterFactory = RxJava2CallAdapterFactory.create();
Retrofit retrofitRecordings = new Retrofit.Builder()
.baseUrl(recordingUrl)
.client(client)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.build();
mRecordingApi = retrofitRecordings.create(RecordingService.class);
Retrofit retrofigStreaming = new Retrofit.Builder()
.baseUrl(streamingUrl)
.client(client)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.build();
mStreamingApi = retrofigStreaming.create(StreamingService.class);
}
public Observable<ConferencesWrapper> getConferencesWrapper() {
return mRecordingApi.getConferences()
.subscribeOn(Schedulers.io());
}
public Observable<List<Conference>> getConferencesByGroup(String group){
return mRecordingApi.getConferences().map(
conferencesWrapper -> conferencesWrapper.getConferencesBySeries().get(group))
.subscribeOn(Schedulers.io());
}
public Observable<Conference> getConference(int mConferenceId) {
return mRecordingApi.getConference(mConferenceId)
.subscribeOn(Schedulers.io());
}
public Observable<Event> getEvent(int apiID) {
return mRecordingApi.getEvent(apiID)
.subscribeOn(Schedulers.io());
}
public static class Factory extends ViewModelProvider.NewInstanceFactory{
private final String recordingUrl;
private final String streamUrl;
public Factory(String recordingUrl, String streamUrl){
this.recordingUrl = recordingUrl;
this.streamUrl = streamUrl;
}
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
return (T) new ChaosflixViewModel(recordingUrl,streamUrl);
}
}
}

View file

@ -24,7 +24,6 @@ public class ConferenceGroupsFragmentPager extends FragmentPagerAdapter {
private static final String TAG = ConferenceGroupsFragmentPager.class.getSimpleName();
private final Context mContext;
private List<String> orderedConferencesList = new ArrayList<>();
private Map<String, List<Conference>> mConferenceMap;
public ConferenceGroupsFragmentPager(Context context, FragmentManager fm) {
super(fm);
@ -33,11 +32,8 @@ public class ConferenceGroupsFragmentPager extends FragmentPagerAdapter {
@Override
public Fragment getItem(int position) {
ConferenceGroupFragment conferenceFragment = ConferenceGroupFragment.newInstance(1);
// ConferenceGroupFragment conferenceFragment = ConferenceGroupFragment.newInstance(getNumColumns());
String confKey = orderedConferencesList.get(position);
List<Conference> conferences = mConferenceMap.get(confKey);
conferenceFragment.setContent(conferences);
ConferenceGroupFragment conferenceFragment = ConferenceGroupFragment.newInstance(confKey,1);
conferenceFragment.setRetainInstance(true);
Log.d(TAG,"Created Fragment for: " + confKey);
return conferenceFragment;
@ -54,8 +50,6 @@ public class ConferenceGroupsFragmentPager extends FragmentPagerAdapter {
}
public void setContent(Map<String, List<Conference>> conferenceMap) {
mConferenceMap = conferenceMap;
// orderedConferencesList = new ArrayList<>(conferenceMap.keySet());
orderedConferencesList = new ArrayList<>();
for (String tag : ConferencesWrapper.getOrderedConferencesList()) {
if (conferenceMap.keySet().contains(tag)) {
@ -67,6 +61,7 @@ public class ConferenceGroupsFragmentPager extends FragmentPagerAdapter {
orderedConferencesList.add(tag);
}
}
notifyDataSetChanged();
}
@Override

View file

@ -1,5 +1,7 @@
package de.nicidienase.chaosflix.touch.activities;
import android.arch.lifecycle.ViewModelProviders;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
@ -17,6 +19,7 @@ import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
import de.nicidienase.chaosflix.common.entities.recording.Event;
import de.nicidienase.chaosflix.common.entities.recording.Recording;
import de.nicidienase.chaosflix.touch.ChaosflixViewModel;
import de.nicidienase.chaosflix.touch.fragments.ConferencesTabBrowseFragment;
import de.nicidienase.chaosflix.touch.fragments.EventDetailsFragment;
import de.nicidienase.chaosflix.touch.fragments.EventsFragment;
@ -24,6 +27,7 @@ import de.nicidienase.chaosflix.touch.fragments.ExoPlayerFragment;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
/**
* Created by felix on 17.09.17.
@ -36,48 +40,58 @@ public class BrowseActivity extends TouchBaseActivity implements
ExoPlayerFragment.OnMediaPlayerInteractionListener {
private static final String TAG = BrowseActivity.class.getSimpleName();
private static final String TAG_RETAINED_FRAGMENT = "retained_fragment";
// private static final String TAG_RETAINED_FRAGMENT = "retained_fragment";
CompositeDisposable mDisposables = new CompositeDisposable();
private ChaosflixViewModel mViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_container_layout);
Resources res = getResources();
ChaosflixViewModel.Factory factory =
new ChaosflixViewModel.Factory(
res.getString(R.string.api_media_ccc_url),
res.getString(R.string.streaming_media_ccc_url));
mViewModel = ViewModelProviders.of(this,factory).get(ChaosflixViewModel.class);
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = null;
if(savedInstanceState != null){
fragment = fragmentManager.getFragment(savedInstanceState,TAG_RETAINED_FRAGMENT);
}
if(fragment != null){
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.fragment_container,fragment,TAG_RETAINED_FRAGMENT);
ft.commit();
// Restore previous state
// fragment = fragmentManager.getFragment(savedInstanceState,TAG_RETAINED_FRAGMENT);
} else {
Disposable disposable = getApiServiceObservable()
.subscribe(mediaApiService -> {
mediaApiService.getConferences()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conferencesWrapper -> {
ConferencesTabBrowseFragment browseFragment
= ConferencesTabBrowseFragment.newInstance(getNumColumns());
browseFragment.setContent(conferencesWrapper);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.fragment_container,browseFragment,TAG_RETAINED_FRAGMENT);
ft.commit();
});
});
mDisposables.add(disposable);
// New instance
if(fragment != null){
FragmentTransaction ft = fragmentManager.beginTransaction();
// ft.replace(R.id.fragment_container,fragment,TAG_RETAINED_FRAGMENT);
ft.replace(R.id.fragment_container,fragment);
ft.commit();
} else {
mDisposables.add(mViewModel.getConferencesWrapper()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conferencesWrapper -> {
ConferencesTabBrowseFragment browseFragment
= ConferencesTabBrowseFragment.newInstance(getNumColumns());
// browseFragment.setContent(conferencesWrapper);
FragmentTransaction ft = fragmentManager.beginTransaction();
// ft.replace(R.id.fragment_container,browseFragment,TAG_RETAINED_FRAGMENT);
ft.replace(R.id.fragment_container,browseFragment);
ft.commit();
}));
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(TAG_RETAINED_FRAGMENT);
if(fragment != null){
fragmentManager.putFragment(outState,TAG_RETAINED_FRAGMENT,fragment);
}
// FragmentManager fragmentManager = getSupportFragmentManager();
// Fragment fragment = fragmentManager.findFragmentByTag(TAG_RETAINED_FRAGMENT);
// if(fragment != null){
// fragmentManager.putFragment(outState,TAG_RETAINED_FRAGMENT,fragment);
// }
}
@Override
@ -92,59 +106,54 @@ public class BrowseActivity extends TouchBaseActivity implements
@Override
public void onConferenceSelected(Conference con) {
Disposable disposable = getApiServiceObservable()
.subscribe(mediaApiService -> {
mediaApiService.getConference(con.getApiID())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conference -> {
EventsFragment eventsFragment = EventsFragment.newInstance(conference,getNumColumns());
FragmentManager fm = getSupportFragmentManager();
Fragment oldFragment = fm.findFragmentById(R.id.fragment_container);
mDisposables.add(mViewModel.getConference(con.getApiID())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conference -> {
EventsFragment eventsFragment = EventsFragment.newInstance(conference.getApiID(),getNumColumns());
FragmentManager fm = getSupportFragmentManager();
Fragment oldFragment = fm.findFragmentById(R.id.fragment_container);
TransitionInflater transitionInflater = TransitionInflater.from(this);
oldFragment.setExitTransition(
transitionInflater.inflateTransition(android.R.transition.fade));
eventsFragment.setEnterTransition(
transitionInflater.inflateTransition(android.R.transition.slide_right));
TransitionInflater transitionInflater = TransitionInflater.from(this);
oldFragment.setExitTransition(
transitionInflater.inflateTransition(android.R.transition.fade));
eventsFragment.setEnterTransition(
transitionInflater.inflateTransition(android.R.transition.slide_right));
Slide slideTransition = new Slide(Gravity.RIGHT);
Slide slideTransition = new Slide(Gravity.RIGHT);
// slideTransition.setDuration(1000);
eventsFragment.setEnterTransition(slideTransition);
eventsFragment.setEnterTransition(slideTransition);
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, eventsFragment,TAG_RETAINED_FRAGMENT);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
});
});
mDisposables.add(disposable);
FragmentTransaction ft = fm.beginTransaction();
// ft.replace(R.id.fragment_container, eventsFragment,TAG_RETAINED_FRAGMENT);
ft.replace(R.id.fragment_container, eventsFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}));
}
@Override
public void onEventSelected(Event e, View v) {
Disposable disposable = getApiServiceObservable()
.subscribe(mediaApiService -> {
mediaApiService.getEvent(e.getApiID())
.subscribe(event -> {
mDisposables.add(mViewModel.getEvent(e.getApiID())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
EventDetailsFragment detailsFragment = EventDetailsFragment.newInstance(event);
FragmentManager fm = getSupportFragmentManager();
EventDetailsFragment detailsFragment = EventDetailsFragment.newInstance(event);
FragmentManager fm = getSupportFragmentManager();
detailsFragment.setAllowEnterTransitionOverlap(true);
detailsFragment.setAllowReturnTransitionOverlap(true);
detailsFragment.setAllowEnterTransitionOverlap(true);
detailsFragment.setAllowReturnTransitionOverlap(true);
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, detailsFragment,TAG_RETAINED_FRAGMENT);
ft.addToBackStack(null);
FragmentTransaction ft = fm.beginTransaction();
// ft.replace(R.id.fragment_container, detailsFragment,TAG_RETAINED_FRAGMENT);
ft.replace(R.id.fragment_container, detailsFragment);
ft.addToBackStack(null);
View thumb = v.findViewById(R.id.imageView);
ft.addSharedElement(thumb,ViewCompat.getTransitionName(thumb));
View thumb = v.findViewById(R.id.imageView);
ft.addSharedElement(thumb,ViewCompat.getTransitionName(thumb));
ft.commit();
});
});
mDisposables.add(disposable);
ft.commit();
}));
}
@Override
@ -163,7 +172,8 @@ public class BrowseActivity extends TouchBaseActivity implements
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment playerFragment = ExoPlayerFragment.newInstance(event,recording);
ft.replace(R.id.fragment_container,playerFragment,TAG_RETAINED_FRAGMENT);
// ft.replace(R.id.fragment_container,playerFragment,TAG_RETAINED_FRAGMENT);
ft.replace(R.id.fragment_container,playerFragment);
ft.addToBackStack(null);
ft.commit();
}

View file

@ -1,71 +1,9 @@
package de.nicidienase.chaosflix.touch.activities;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import de.nicidienase.chaosflix.common.network.MediaApiService;
import io.reactivex.Single;
/**
* Created by felix on 24.03.17.
*/
public class TouchBaseActivity extends AppCompatActivity {
private MediaApiService mMediaApiService = null;
private ServiceConnection conn;
private boolean mConnected = false;
private String serverUrl = null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
serverUrl = getIntent().getStringExtra("server_url");
}
@Override
protected void onDestroy() {
if (mConnected) {
unbindService(conn);
conn = null;
}
super.onDestroy();
}
public Single<MediaApiService> getApiServiceObservable() {
Intent s = new Intent(this, MediaApiService.class);
if (serverUrl != null) {
s.putExtra(MediaApiService.RECORDING_URL, serverUrl);
s.putExtra(MediaApiService.STREAMING_URL, serverUrl);
}
return Single.create(e -> {
if (mMediaApiService != null) {
e.onSuccess(mMediaApiService);
} else {
conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mConnected = true;
mMediaApiService = ((MediaApiService.LocalBinder) service).getService();
e.onSuccess(mMediaApiService);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mMediaApiService = null;
mConnected = false;
}
};
TouchBaseActivity.this.bindService(s, conn, Context.BIND_AUTO_CREATE);
}
});
}
}
public class TouchBaseActivity extends AppCompatActivity {}

View file

@ -2,6 +2,7 @@ package de.nicidienase.chaosflix.touch.adapters;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
@ -11,12 +12,15 @@ public class ConferenceRecyclerViewAdapter extends ItemRecyclerViewAdapter<Confe
private final ConferencesTabBrowseFragment.OnConferenceListFragmentInteractionListener mListener;
public ConferenceRecyclerViewAdapter(ConferencesTabBrowseFragment.OnConferenceListFragmentInteractionListener listener){
this(new ArrayList<>(),listener);
}
public ConferenceRecyclerViewAdapter(List<Conference> items, ConferencesTabBrowseFragment.OnConferenceListFragmentInteractionListener listener) {
super(items);
mListener = listener;
}
@Override
public void onBindViewHolder(ItemRecyclerViewAdapter.ViewHolder holder, int position) {
holder.mItem = mItems.get(position);

View file

@ -6,6 +6,7 @@ import android.support.v4.view.ViewCompat;
import com.squareup.picasso.Picasso;
import java.util.Collections;
import java.util.List;
import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
@ -14,12 +15,22 @@ import de.nicidienase.chaosflix.touch.fragments.EventsFragment;
public class EventRecyclerViewAdapter extends ItemRecyclerViewAdapter<Event> {
private final boolean areTagsUsefull;
private boolean areTagsUsefull;
private final EventsFragment.OnEventsListFragmentInteractionListener mListener;
public EventRecyclerViewAdapter(EventsFragment.OnEventsListFragmentInteractionListener listener) {
super();
mListener = listener;
}
public EventRecyclerViewAdapter(Conference conference, EventsFragment.OnEventsListFragmentInteractionListener listener) {
super(conference.getEvents());
mListener = listener;
setItems(conference);
}
public void setItems(Conference conference){
setItems(conference.getEvents());
areTagsUsefull = conference.areTagsUsefull();
Collections.sort(mItems,(o1, o2) -> o1.getTitle().compareTo(o2.getTitle()));
}

View file

@ -7,18 +7,28 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import de.nicidienase.chaosflix.R;
public abstract class ItemRecyclerViewAdapter<T> extends RecyclerView.Adapter<ItemRecyclerViewAdapter.ViewHolder> {
protected final List<T> mItems;
protected List<T> mItems;
public ItemRecyclerViewAdapter(){
mItems = new ArrayList<>();
}
public ItemRecyclerViewAdapter(List<T> items) {
mItems = items;
}
public void setItems(List<T> items){
mItems = items;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())

View file

@ -0,0 +1,35 @@
package de.nicidienase.chaosflix.touch.fragments;
import android.arch.lifecycle.ViewModelProviders;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.touch.ChaosflixViewModel;
/**
* Created by felix on 25.09.17.
*/
public class ChaosflixFragment extends Fragment {
private ChaosflixViewModel mViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
ChaosflixViewModel.Factory factory =
new ChaosflixViewModel.Factory(
res.getString(R.string.api_media_ccc_url),
res.getString(R.string.streaming_media_ccc_url));
mViewModel = ViewModelProviders.of(getActivity(),factory).get(ChaosflixViewModel.class);
}
public ChaosflixViewModel getViewModel() {
return mViewModel;
}
}

View file

@ -1,5 +1,6 @@
package de.nicidienase.chaosflix.touch.fragments;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@ -13,31 +14,41 @@ import android.view.ViewGroup;
import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
import de.nicidienase.chaosflix.touch.ChaosflixViewModel;
import de.nicidienase.chaosflix.touch.adapters.ConferenceRecyclerViewAdapter;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import java.util.List;
public class ConferenceGroupFragment extends Fragment {
public class ConferenceGroupFragment extends ChaosflixFragment {
private static final String TAG = ConferenceGroupFragment.class.getSimpleName();
private static final String ARG_COLUMN_COUNT = "column-count";
private int mColumnCount = 1;
private static final String ARG_GROUP_NAME = "group-name";
private static final String LAYOUTMANAGER_STATE = "layoutmanager-state";
private ConferencesTabBrowseFragment.OnConferenceListFragmentInteractionListener mListener;
private List<Conference> mItmes = new ArrayList<>();
private int mColumnCount = 1;
private String mGroupName;
private ConferenceRecyclerViewAdapter mAdapter;
CompositeDisposable mDisposable = new CompositeDisposable();
private RecyclerView.LayoutManager mLayoutManager;
public ConferenceGroupFragment() {
}
public void setContent(List<Conference> itmes){
mItmes = itmes;
}
public static ConferenceGroupFragment newInstance(int columnCount) {
public static ConferenceGroupFragment newInstance(String group,int columnCount) {
ConferenceGroupFragment fragment = new ConferenceGroupFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
args.putString(ARG_GROUP_NAME, group);
fragment.setArguments(args);
return fragment;
}
@ -47,7 +58,9 @@ public class ConferenceGroupFragment extends Fragment {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
mGroupName = getArguments().getString(ARG_GROUP_NAME);
}
}
@Override
@ -58,15 +71,21 @@ public class ConferenceGroupFragment extends Fragment {
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView mRecyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
mLayoutManager = new LinearLayoutManager(context);
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
mLayoutManager = new GridLayoutManager(context, mColumnCount);
}
mLayoutManager.onRestoreInstanceState(savedInstanceState);
mRecyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(new ConferenceRecyclerViewAdapter(mItmes, mListener) {
});
mAdapter = new ConferenceRecyclerViewAdapter(mListener);
mRecyclerView.setAdapter(mAdapter);
mDisposable.add(getViewModel().getConferencesByGroup(mGroupName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conferences -> mAdapter.setItems(conferences)));
}
return view;
}
@ -82,6 +101,18 @@ public class ConferenceGroupFragment extends Fragment {
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(LAYOUTMANAGER_STATE,mLayoutManager.onSaveInstanceState());
}
@Override
public void onStop() {
super.onStop();
mDisposable.clear();
}
@Override
public void onDetach() {
super.onDetach();

View file

@ -1,7 +1,9 @@
package de.nicidienase.chaosflix.touch.fragments;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
@ -16,13 +18,17 @@ import android.view.ViewGroup;
import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
import de.nicidienase.chaosflix.common.entities.recording.ConferencesWrapper;
import de.nicidienase.chaosflix.touch.ChaosflixViewModel;
import de.nicidienase.chaosflix.touch.ConferenceGroupsFragmentPager;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
/**
* Created by felix on 19.09.17.
*/
public class ConferencesTabBrowseFragment extends Fragment {
public class ConferencesTabBrowseFragment extends ChaosflixFragment {
private static final String TAG = ConferencesTabBrowseFragment.class.getSimpleName();
@ -31,17 +37,19 @@ public class ConferencesTabBrowseFragment extends Fragment {
private static final String CURRENTTAB_KEY = "current_tab";
private int mColumnCount = 1;
private OnConferenceListFragmentInteractionListener mListener;
private ConferencesWrapper mConferencesWrapper;
private Toolbar mToolbar;
private Context mContext;
private int mCurrentTab = -1;
private ViewPager mViewPager;
private final CompositeDisposable mDisposable = new CompositeDisposable();
public ConferencesTabBrowseFragment() {
}
public void setContent(ConferencesWrapper conferencesWrapper){
this.mConferencesWrapper = conferencesWrapper;
// this.mConferencesWrapper = conferencesWrapper;
}
public static ConferencesTabBrowseFragment newInstance(int columnCount) {
@ -67,10 +75,10 @@ public class ConferencesTabBrowseFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
Log.d(TAG,"onCreate");
if(savedInstanceState != null){
mConferencesWrapper = savedInstanceState.getParcelable(CONFERENCEWRAPPER_KEY);
// mConferencesWrapper = savedInstanceState.getParcelable(CONFERENCEWRAPPER_KEY);
mCurrentTab = savedInstanceState.getInt(CURRENTTAB_KEY);
}
if (getArguments() != null) {
@ -85,7 +93,14 @@ public class ConferencesTabBrowseFragment extends Fragment {
View view = inflater.inflate(R.layout.fragment_tab_pager_layout, container, false);
ConferenceGroupsFragmentPager fragmentPager
= new ConferenceGroupsFragmentPager(this.getContext(),getChildFragmentManager());
fragmentPager.setContent(mConferencesWrapper.getConferencesBySeries());
getViewModel().getConferencesWrapper()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conferencesWrapper -> {
fragmentPager.setContent(conferencesWrapper.getConferencesBySeries());
mViewPager.setCurrentItem(mCurrentTab);
});
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(fragmentPager);
@ -104,10 +119,16 @@ public class ConferencesTabBrowseFragment extends Fragment {
return view;
}
@Override
public void onStop() {
super.onStop();
mDisposable.clear();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(CONFERENCEWRAPPER_KEY, mConferencesWrapper);
// outState.putParcelable(CONFERENCEWRAPPER_KEY, mConferencesWrapper);
outState.putInt(CURRENTTAB_KEY, mViewPager.getCurrentItem());
}

View file

@ -47,7 +47,6 @@ public class EventDetailsFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
postponeEnterTransition();
Transition transition = TransitionInflater.from(getContext())

View file

@ -2,6 +2,7 @@ package de.nicidienase.chaosflix.touch.fragments;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
@ -17,27 +18,36 @@ import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.common.entities.recording.Conference;
import de.nicidienase.chaosflix.common.entities.recording.Event;
import de.nicidienase.chaosflix.touch.adapters.EventRecyclerViewAdapter;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
public class EventsFragment extends Fragment {
public class EventsFragment extends ChaosflixFragment {
private static final String ARG_COLUMN_COUNT = "column-count";
private static final String ARG_CONFERENCE = "conference";
private static final String LAYOUTMANAGER_STATE = "layoutmanager-state";
private int mColumnCount = 1;
private OnEventsListFragmentInteractionListener mListener;
private Conference mConference;
private CharSequence mPreviousTitle;
private Toolbar mToolbar;
private Context mContext;
private EventRecyclerViewAdapter mAdapter;
private int mConferenceId;
CompositeDisposable mDisposable = new CompositeDisposable();
private LinearLayoutManager layoutManager;
public EventsFragment() {
}
public static EventsFragment newInstance(Conference conference, int columnCount) {
public static EventsFragment newInstance(int conferenceId, int columnCount) {
EventsFragment fragment = new EventsFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
args.putParcelable(ARG_CONFERENCE, conference);
args.putInt(ARG_CONFERENCE, conferenceId);
fragment.setArguments(args);
return fragment;
}
@ -46,7 +56,6 @@ public class EventsFragment extends Fragment {
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
// mPreviousTitle = mActionBar.getTitle();
if (context instanceof OnEventsListFragmentInteractionListener) {
mListener = (OnEventsListFragmentInteractionListener) context;
} else {
@ -58,10 +67,9 @@ public class EventsFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
mConference = getArguments().getParcelable(ARG_CONFERENCE);
mConferenceId = getArguments().getInt(ARG_CONFERENCE);
}
}
@ -70,30 +78,55 @@ public class EventsFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycler_view_toolbar_layout, container, false);
// mActionBar.setTitle(mConference.getTitle());
// Set the adapter
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
layoutManager = new LinearLayoutManager(context);
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
layoutManager = new GridLayoutManager(context, mColumnCount);
}
layoutManager.onRestoreInstanceState(savedInstanceState);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new EventRecyclerViewAdapter(mConference, mListener));
mAdapter = new EventRecyclerViewAdapter(mListener);
recyclerView.setAdapter(mAdapter);
mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity)mContext).setSupportActionBar(mToolbar);
mToolbar.setTitle(mConference.getTitle());
mDisposable.add(getViewModel().getConference(mConferenceId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(conference -> {
if(savedInstanceState != null){
Parcelable parcelable = savedInstanceState.getParcelable(LAYOUTMANAGER_STATE);
layoutManager.onRestoreInstanceState(parcelable);
}
mAdapter.setItems(conference);
mToolbar.setTitle(conference.getTitle());
}));
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(LAYOUTMANAGER_STATE,layoutManager.onSaveInstanceState());
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
// mActionBar.setTitle(mPreviousTitle);
}
@Override
public void onStop() {
super.onStop();
mDisposable.clear();
}
public interface OnEventsListFragmentInteractionListener{

View file

@ -94,7 +94,6 @@ public class ExoPlayerFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if (getArguments() != null) {
mEvent = getArguments().getParcelable(ARG_EVENT);
mRecording = getArguments().getParcelable(ARG_RECORDING);