diff --git a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/EventDetailsDescriptionPresenter.kt b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/EventDetailsDescriptionPresenter.kt index 9c0a190e..741cbf3c 100644 --- a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/EventDetailsDescriptionPresenter.kt +++ b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/EventDetailsDescriptionPresenter.kt @@ -24,15 +24,15 @@ class EventDetailsDescriptionPresenter(private val context: Context) : Presenter } val dataHolder: DetailDataHolder if (item is PersistentEvent) { - val (_, _, _, _, title, subtitle, _, _, description, _, _, releaseDate, _, _, _, _, _, _, _, _, _, persons, tags) = item + val persistentEvent = item val sb = StringBuilder() - val speaker = TextUtils.join(", ", persons!!) - sb.append(description) + val speaker = TextUtils.join(", ", persistentEvent.persons) + sb.append(persistentEvent.description) .append("\n") - .append("\nreleased at: ").append(releaseDate) - .append("\nTags: ").append(android.text.TextUtils.join(", ", tags!!)) - dataHolder = DetailDataHolder(title, - subtitle, + .append("\nreleased at: ").append(persistentEvent.releaseDate) + .append("\nTags: ").append(android.text.TextUtils.join(", ", persistentEvent.tags!!)) + dataHolder = DetailDataHolder(persistentEvent.title, + persistentEvent.subtitle, speaker, sb.toString()) } else if (item is Room) { diff --git a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/ItemViewClickedListener.java b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/ItemViewClickedListener.java index 1d5ab2bb..552fc25d 100644 --- a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/ItemViewClickedListener.java +++ b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/ItemViewClickedListener.java @@ -34,12 +34,12 @@ public class ItemViewClickedListener implements OnItemViewClickedListener { FragmentActivity activity = fragment.requireActivity(); if (item instanceof PersistentConference) { PersistentConference conference = (PersistentConference) item; - // Start EventsActivity for this conference Bundle transition = ActivityOptionsCompat.makeSceneTransitionAnimation( activity, ((ImageCardView) itemViewHolder.view).getMainImageView(), EventsActivity.Companion.getSHARED_ELEMENT_NAME()).toBundle(); EventsActivity.start(fragment.requireContext(),conference,transition); +// EventsActivity.start(fragment.requireContext(),conference); } else if (item instanceof PersistentEvent) { Bundle transistion = ActivityOptionsCompat.makeSceneTransitionAnimation( activity, @@ -54,7 +54,6 @@ public class ItemViewClickedListener implements OnItemViewClickedListener { EventDetailsActivity.Companion.getSHARED_ELEMENT_NAME()).toBundle(); Room room = (Room) item; DetailsActivity.start(fragment.requireContext(), room, transition); - } } } diff --git a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/activities/DetailsActivity.kt b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/activities/DetailsActivity.kt index 7b7aad78..7db4e01d 100644 --- a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/activities/DetailsActivity.kt +++ b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/activities/DetailsActivity.kt @@ -42,19 +42,27 @@ class DetailsActivity : FragmentActivity() { val TYPE_STREAM = 1 @JvmStatic - fun start(context: Context, event: PersistentEvent, transistion: Bundle? = null){ + fun start(context: Context, event: PersistentEvent, transition: Bundle? = null){ val i = Intent(context, DetailsActivity::class.java) i.putExtra(DetailsActivity.TYPE, DetailsActivity.TYPE_RECORDING) i.putExtra(DetailsActivity.EVENT, event) - context.startActivity(i, transistion) + if(transition != null){ + context.startActivity(i, transition) + } else { + context.startActivity(i) + } } @JvmStatic - fun start(context: Context, room: Room, transition: Bundle){ + fun start(context: Context, room: Room, transition: Bundle? = null){ val i = Intent(context, DetailsActivity::class.java) i.putExtra(DetailsActivity.TYPE, DetailsActivity.TYPE_STREAM) i.putExtra(DetailsActivity.ROOM, room) - context.startActivity(i, transition) + if(transition != null){ + context.startActivity(i, transition) + } else { + context.startActivity(i) + } } } diff --git a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/activities/EventsActivity.kt b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/activities/EventsActivity.kt index 32c1bc32..f371616e 100644 --- a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/activities/EventsActivity.kt +++ b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/activities/EventsActivity.kt @@ -30,10 +30,15 @@ class EventsActivity : FragmentActivity() { val SHARED_ELEMENT_NAME = "shared_element" @JvmStatic - fun start(context: Context ,conference: PersistentConference, transition: Bundle){ + @JvmOverloads + fun start(context: Context ,conference: PersistentConference, transition: Bundle? = null){ val i = Intent(context, EventsActivity::class.java) i.putExtra(EventsActivity.CONFERENCE, conference) - context.startActivity(i, transition) + if(transition != null){ + context.startActivity(i, transition) + } else { + context.startActivity(i) + } } } } diff --git a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/EventsBrowseFragment.kt b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/EventsBrowseFragment.kt index af998ecf..debfff97 100644 --- a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/EventsBrowseFragment.kt +++ b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/EventsBrowseFragment.kt @@ -87,21 +87,21 @@ class EventsBrowseFragment : BrowseSupportFragment() { } } } - val events = it?.data - if (events != null) { - Log.d(TAG, "Got ${events.size} events") - val eventsByTags = getEventsByTags(events, conference.acronym) - Log.d(TAG, "Got ${eventsByTags.keys.size} Tags") - for (item in eventsByTags) { - updateRowForTag(cardPresenter, item.key, item.value) - } - errorFragment?.dismiss() - } +// val events = it?.data +// if (events != null) { +// Log.d(TAG, "Got ${events.size} events") +// val eventsByTags = getEventsByTags(events, conference.acronym) +// Log.d(TAG, "Got ${eventsByTags.keys.size} Tags") +// for (item in eventsByTags) { +// updateRowForTag(cardPresenter, item.key, item.value) +// } +// errorFragment?.dismiss() +// } }) viewModel.getEventsforConference(conference).observe( this, Observer { - if (it != null) { + if (it != null && it.size > 0) { val eventsByTags = getEventsByTags(it, conference.acronym) for (item in eventsByTags) { updateRowForTag(cardPresenter, item.key, item.value) diff --git a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/EventsDetailsFragment.java b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/EventsDetailsFragment.java index 65f78cee..7924b7d4 100644 --- a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/EventsDetailsFragment.java +++ b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/EventsDetailsFragment.java @@ -1,6 +1,7 @@ package de.nicidienase.chaosflix.leanback.fragments; import android.app.AlertDialog; +import android.arch.lifecycle.ViewModelProviders; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -8,6 +9,7 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.Bundle; +import android.os.Handler; import android.support.annotation.NonNull; import android.support.v17.leanback.app.BackgroundManager; import android.support.v17.leanback.app.DetailsSupportFragment; @@ -47,8 +49,10 @@ import de.nicidienase.chaosflix.common.mediadata.entities.streaming.LiveConferen import de.nicidienase.chaosflix.common.mediadata.entities.streaming.Room; import de.nicidienase.chaosflix.common.mediadata.entities.streaming.Stream; import de.nicidienase.chaosflix.common.mediadata.entities.streaming.StreamUrl; -import de.nicidienase.chaosflix.common.userdata.entities.watchlist.WatchlistItem; +import de.nicidienase.chaosflix.common.viewmodel.DetailsViewModel; +import de.nicidienase.chaosflix.common.viewmodel.ViewModelFactory; import de.nicidienase.chaosflix.leanback.EventDetailsDescriptionPresenter; +import de.nicidienase.chaosflix.leanback.ItemViewClickedListener; import de.nicidienase.chaosflix.leanback.activities.ConferencesActivity; import de.nicidienase.chaosflix.leanback.activities.DetailsActivity; import de.nicidienase.chaosflix.leanback.activities.EventDetailsActivity; @@ -66,26 +70,29 @@ public class EventsDetailsFragment extends DetailsSupportFragment { private static final String TAG = EventsDetailsFragment.class.getSimpleName(); public static final int FRAGMENT = R.id.details_fragment; - public static final int DUMMY_ID = 1646465164; public static final int DEFAULT_DRAWABLE = R.drawable.default_background; - private static final long ADD_WATCHLIST_ACTION = 1646465165; - private static final long REMOVE_WATCHLIST_ACTION = 1646465166; + + private static final long ACTION_PLAY = 1646465164; + private static final long ACTION_ADD_WATCHLIST = 1646465165; + private static final long ACTION_REMOVE_WATCHLIST = 1646465166; + private PersistentEvent event; private Room room; private int eventType; private ArrayList streamUrlList; private BackgroundManager backgroundManager; - private DisplayMetrics metrics; - private WatchlistItem watchlistItem; private ArrayObjectAdapter recordingActionsAdapter; private OnActionClickedListener onActionClickedListener = new DetailActionClickedListener(); + private DetailsViewModel viewModel; @Override public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "onCreate"); super.onCreate(savedInstanceState); - prepareBackgroundManager(); + viewModel = ViewModelProviders.of(this, new ViewModelFactory(requireContext())).get(DetailsViewModel.class); + +// prepareBackgroundManager(); final BrowseErrorFragment browseErrorFragment = BrowseErrorFragment.showErrorFragment(getFragmentManager(), FRAGMENT); eventType = getActivity().getIntent().getIntExtra(DetailsActivity.Companion.getTYPE(), -1); @@ -93,7 +100,23 @@ public class EventsDetailsFragment extends DetailsSupportFragment { if (eventType == DetailsActivity.Companion.getTYPE_RECORDING()) { event = getActivity().getIntent() .getParcelableExtra(DetailsActivity.Companion.getEVENT()); -// watchlistItem = WatchlistItem.findById(WatchlistItem.class, event.getApiID()); + setTitle(event.getTitle()); +// viewModel.setEvent(event).observe(this, event1 -> { +// List recordings = event1.getRecordings(); +// if(recordings != null){ +// recordingActionsAdapter.setItems(recordings, new DiffCallback() { +// @Override +// public boolean areItemsTheSame(@NonNull PersistentRecording oldItem, @NonNull PersistentRecording newItem) { +// return oldItem.getUrl().equals(newItem.getUrl()); +// } +// +// @Override +// public boolean areContentsTheSame(@NonNull PersistentRecording oldItem, @NonNull PersistentRecording newItem) { +// return oldItem.getRecordingUrl().equals(newItem.getRecordingUrl()); +// } +// }); +// } +// }); } else if (eventType == DetailsActivity.Companion.getTYPE_STREAM()) { room = getActivity().getIntent() .getParcelableExtra(DetailsActivity.Companion.getROOM()); @@ -101,83 +124,91 @@ public class EventsDetailsFragment extends DetailsSupportFragment { FullWidthDetailsOverviewRowPresenter mDetailsPresenter = setupDetailsOverviewRowPresenter(); - ClassPresenterSelector mPresenterSelector = new ClassPresenterSelector(); - mPresenterSelector.addClassPresenter(DetailsOverviewRow.class, mDetailsPresenter); - mPresenterSelector.addClassPresenter(ListRow.class, new ListRowPresenter()); - final ArrayObjectAdapter adapter = new ArrayObjectAdapter(mPresenterSelector); + ClassPresenterSelector presenterSelector = new ClassPresenterSelector(); + presenterSelector.addClassPresenter(DetailsOverviewRow.class, mDetailsPresenter); + presenterSelector.addClassPresenter(ListRow.class, new ListRowPresenter()); + final ArrayObjectAdapter adapter = new ArrayObjectAdapter(presenterSelector); + final DetailsOverviewRow detailsOverviewRow = setupDetailsOverviewRow(event); + recordingActionsAdapter = getRecordingActionsAdapter(); + detailsOverviewRow.setActionsAdapter(recordingActionsAdapter); + adapter.add(detailsOverviewRow); + setOnItemViewClickedListener(new ItemViewClickedListener(this)); + setAdapter(adapter); + browseErrorFragment.dismiss(); // ((LeanbackBaseActivity) getActivity()).getApiServiceObservable() -// .doOnError(t -> browseErrorFragment.setErrorContent(t.getMessage())) -// .subscribe(mediaApiService -> { -// mMediaApiService = mediaApiService; -// if (eventType == DetailsActivity.Companion.getTYPE_RECORDING()) { -// final DetailsOverviewRow detailsOverviewRow = setupDetailsOverviewRow(event); -// mediaApiService.getEvent(event.getApiID()) -// .doOnError(t -> browseErrorFragment.setErrorContent(t.getMessage())) -// .subscribe(event -> { -// this.event = event; -// recordingActionsAdapter = -// getRecordingActionsAdapter(this.event.getRecordings()); -// detailsOverviewRow.setActionsAdapter(recordingActionsAdapter); -// adapter.add(detailsOverviewRow); -// mediaApiService.getConference( -// this.event.getConferenceId()) -// .observeOn(AndroidSchedulers.mainThread()) -// .subscribe(conference -> { -// String tag = null; -// if (this.event.getTags().size() > 0) { -// tag = this.event.getTags().get(0); -// List relatedEvents = conference.getEventsByTags().get(tag); -// relatedEvents.remove(this.event); -// Collections.shuffle(relatedEvents); -// if (relatedEvents.size() > 5) { -// relatedEvents = relatedEvents.subList(0, NUM_RELATED_TALKS); -// } -// ArrayObjectAdapter relatedEventsAdapter -// = new ArrayObjectAdapter(new CardPresenter()); -// relatedEventsAdapter.addAll(0, relatedEvents); -// HeaderItem header = new HeaderItem(getString(R.string.random_talks_on_this_track)); -// adapter.add(new ListRow(header, relatedEventsAdapter)); -// } +// .doOnError(t -> browseErrorFragment.setErrorContent(t.getMessage())) +// .subscribe(mediaApiService -> { +// mMediaApiService = mediaApiService; +// if (eventType == DetailsActivity.Companion.getTYPE_RECORDING()) { +// final DetailsOverviewRow detailsOverviewRow = setupDetailsOverviewRow(event); +// mediaApiService.getEvent(event.getApiID()) +// .doOnError(t -> browseErrorFragment.setErrorContent(t.getMessage())) +// .subscribe(event -> { +// this.event = event; +// recordingActionsAdapter = +// getRecordingActionsAdapter(this.event.getRecordings()); +// detailsOverviewRow.setActionsAdapter(recordingActionsAdapter); +// adapter.add(detailsOverviewRow); +// mediaApiService.getConference( +// this.event.getConferenceId()) +// .observeOn(AndroidSchedulers.mainThread()) +// .subscribe(conference -> { +// String tag = null; +// if (this.event.getTags().size() > 0) { +// tag = this.event.getTags().get(0); +// List relatedEvents = conference.getEventsByTags().get(tag); +// relatedEvents.remove(this.event); +// Collections.shuffle(relatedEvents); +// if (relatedEvents.size() > 5) { +// relatedEvents = relatedEvents.subList(0, NUM_RELATED_TALKS); +// } +// ArrayObjectAdapter relatedEventsAdapter +// = new ArrayObjectAdapter(new CardPresenter()); +// relatedEventsAdapter.addAll(0, relatedEvents); +// HeaderItem header = new HeaderItem(getString(R.string.random_talks_on_this_track)); +// adapter.add(new ListRow(header, relatedEventsAdapter)); +// } // -// List selectedEvents = getRandomEvents(conference, tag); -// if (selectedEvents.size() > 0) { -// ArrayObjectAdapter randomEventAdapter -// = new ArrayObjectAdapter(new CardPresenter()); -// randomEventAdapter.addAll(0, selectedEvents); -// HeaderItem header = new HeaderItem(getString(R.string.random_talks)); -// adapter.add(new ListRow(header, randomEventAdapter)); -// } +// List selectedEvents = getRandomEvents(conference, tag); +// if (selectedEvents.size() > 0) { +// ArrayObjectAdapter randomEventAdapter +// = new ArrayObjectAdapter(new CardPresenter()); +// randomEventAdapter.addAll(0, selectedEvents); +// HeaderItem header = new HeaderItem(getString(R.string.random_talks)); +// adapter.add(new ListRow(header, randomEventAdapter)); +// } // -// setOnItemViewClickedListener(new ItemViewClickedListener(this)); -// setAdapter(adapter); -// browseErrorFragment.dismiss(); -// }); -// }); -// } else if (eventType == DetailsActivity.Companion.getTYPE_STREAM()) { -// mediaApiService.getStreamingConferences() -// .observeOn(AndroidSchedulers.mainThread()) -// .subscribe(liveConferences -> { -// if(room.getStreams() == null){ -// room = getRoom(room, liveConferences); -// } -// final DetailsOverviewRow detailsOverviewRow = setupDetailsOverviewRow(room); -// ArrayObjectAdapter actionsAdapter = getStreamActionsAdapter(room.getStreams()); -// detailsOverviewRow.setActionsAdapter(actionsAdapter); -// adapter.add(detailsOverviewRow); -// setOnItemViewClickedListener(new ItemViewClickedListener(EventsDetailsFragment.this)); -// setAdapter(adapter); -// browseErrorFragment.dismiss(); -// // TODO add other streams -// }); -// } -// }); +// setOnItemViewClickedListener(new ItemViewClickedListener(this)); +// setAdapter(adapter); +// browseErrorFragment.dismiss(); +// }); +// }); +// } else if (eventType == DetailsActivity.Companion.getTYPE_STREAM()) { +// mediaApiService.getStreamingConferences() +// .observeOn(AndroidSchedulers.mainThread()) +// .subscribe(liveConferences -> { +// if(room.getStreams() == null){ +// room = getRoom(room, liveConferences); +// } +// final DetailsOverviewRow detailsOverviewRow = setupDetailsOverviewRow(room); +// ArrayObjectAdapter actionsAdapter = getStreamActionsAdapter(room.getStreams()); +// detailsOverviewRow.setActionsAdapter(actionsAdapter); +// adapter.add(detailsOverviewRow); +// setOnItemViewClickedListener(new ItemViewClickedListener(EventsDetailsFragment.this)); +// browseErrorFragment.dismiss(); +// setAdapter(adapter); +// // TODO add other streams +// }); +// } +// }); +// new Handler().postDelayed(this::startEntranceTransition, 500); + Log.d(TAG, "create finished"); } @Override public void onStart() { super.onStart(); - startEntranceTransition(); } @Override @@ -189,7 +220,7 @@ public class EventsDetailsFragment extends DetailsSupportFragment { private void prepareBackgroundManager() { backgroundManager = BackgroundManager.getInstance(getActivity()); backgroundManager.attach(getActivity().getWindow()); - metrics = new DisplayMetrics(); + DisplayMetrics metrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); } @@ -241,22 +272,22 @@ public class EventsDetailsFragment extends DetailsSupportFragment { } private FullWidthDetailsOverviewRowPresenter setupDetailsOverviewRowPresenter() { - FullWidthDetailsOverviewRowPresenter mDetailsPresenter = new FullWidthDetailsOverviewRowPresenter( + FullWidthDetailsOverviewRowPresenter detailsPresenter = new FullWidthDetailsOverviewRowPresenter( new EventDetailsDescriptionPresenter(getActivity()), new EventDetailsOverviewLogoPresenter()); - mDetailsPresenter.setBackgroundColor(getResources().getColor(R.color.selected_background)); - mDetailsPresenter.setInitialState(FullWidthDetailsOverviewRowPresenter.STATE_HALF); - mDetailsPresenter.setAlignmentMode(FullWidthDetailsOverviewRowPresenter.ALIGN_MODE_START); + detailsPresenter.setBackgroundColor(getResources().getColor(R.color.selected_background)); + detailsPresenter.setInitialState(FullWidthDetailsOverviewRowPresenter.STATE_HALF); + detailsPresenter.setAlignmentMode(FullWidthDetailsOverviewRowPresenter.ALIGN_MODE_START); FullWidthDetailsOverviewSharedElementHelper helper = new FullWidthDetailsOverviewSharedElementHelper(); helper.setSharedElementEnterTransition(getActivity(), EventDetailsActivity.Companion.getSHARED_ELEMENT_NAME()); - mDetailsPresenter.setListener(helper); + detailsPresenter.setListener(helper); prepareEntranceTransition(); - mDetailsPresenter.setOnActionClickedListener(onActionClickedListener); - return mDetailsPresenter; + detailsPresenter.setOnActionClickedListener(onActionClickedListener); + return detailsPresenter; } private DetailsOverviewRow setupDetailsOverviewRow(Object event) { @@ -274,7 +305,7 @@ public class EventsDetailsFragment extends DetailsSupportFragment { @Override public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { - row.setImageBitmap(getActivity(), resource); + row.setImageBitmap(requireActivity(), resource); } @Override @@ -286,25 +317,35 @@ public class EventsDetailsFragment extends DetailsSupportFragment { return row; } - private ArrayObjectAdapter getRecordingActionsAdapter(List recordings) { + private ArrayObjectAdapter getRecordingActionsAdapter() { ArrayObjectAdapter actionsAdapter = new ArrayObjectAdapter(); - if(watchlistItem != null){ - actionsAdapter.add(new Action(REMOVE_WATCHLIST_ACTION,getString(R.string.remove_from_watchlist))); - } else { - actionsAdapter.add(new Action(ADD_WATCHLIST_ACTION,getString(R.string.add_to_watchlist))); - } - if (recordings != null) { - for (int i = 0; i < recordings.size(); i++) { - PersistentRecording recording = recordings.get(i); - if (recording.getMimeType().startsWith("video/") && !recording.getMimeType().equals("video/webm")) { - String quality = recording.isHighQuality() ? "HD" : "SD"; - String title = quality + " (" + recording.getLanguage() + ")"; - actionsAdapter.add(new Action(recording.getId(), title, recording.getMimeType().substring(6))); - } + final Action watchlistAction = new Action(ACTION_ADD_WATCHLIST, getString(R.string.add_to_watchlist)); + actionsAdapter.add(watchlistAction); + viewModel.getBookmarkForEvent(event.getGuid()).observe(this, watchlistItem -> { + if (watchlistItem != null) { + watchlistAction.setId(ACTION_REMOVE_WATCHLIST); + watchlistAction.setLabel1(getString(R.string.remove_from_watchlist)); + } else { + watchlistAction.setId(ACTION_ADD_WATCHLIST); + watchlistAction.setLabel1(getString(R.string.add_to_watchlist)); } - } else { - actionsAdapter.add(new Action(DUMMY_ID, "Dummy", "HLS")); - } + }); + + Action playAction = new Action(ACTION_PLAY, "Play"); + actionsAdapter.add(playAction); + +// if (recordings != null) { +// for (int i = 0; i < recordings.size(); i++) { +// PersistentRecording recording = recordings.get(i); +// if (recording.getMimeType().startsWith("video/") && !recording.getMimeType().equals("video/webm")) { +// String quality = recording.isHighQuality() ? "HD" : "SD"; +// String title = quality + " (" + recording.getLanguage() + ")"; +// actionsAdapter.add(new Action(recording.getId(), title, recording.getMimeType().substring(6))); +// } +// } +// } else { +// actionsAdapter.add(new Action(DUMMY_ID, "Dummy", "HLS")); +// } return actionsAdapter; } @@ -377,49 +418,38 @@ public class EventsDetailsFragment extends DetailsSupportFragment { @Override public void onActionClicked(Action action) { Log.d(TAG, "OnActionClicked"); - if (action.getId() == ADD_WATCHLIST_ACTION) { + if (action.getId() == ACTION_ADD_WATCHLIST) { // new WatchlistItem(event.getApiID()).save(); - recordingActionsAdapter.replace(0, new Action(REMOVE_WATCHLIST_ACTION, EventsDetailsFragment.this.getString(R.string.remove_from_watchlist))); - SharedPreferences preferences = getActivity().getSharedPreferences(getString(R.string.watchlist_preferences_key), Context.MODE_PRIVATE); - if(preferences.getBoolean(getString(R.string.watchlist_dialog_needed), true)) { // new item + recordingActionsAdapter.replace(0, new Action(ACTION_REMOVE_WATCHLIST, EventsDetailsFragment.this.getString(R.string.remove_from_watchlist))); + SharedPreferences preferences = requireActivity().getSharedPreferences(getString(R.string.watchlist_preferences_key), Context.MODE_PRIVATE); + if (preferences.getBoolean(getString(R.string.watchlist_dialog_needed), true)) { // new item AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.watchlist_message); - builder.setNegativeButton(R.string.return_to_homescreen,(dialog, which) -> { + builder.setNegativeButton(R.string.return_to_homescreen, (dialog, which) -> { Intent i = new Intent(getActivity(), ConferencesActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); - getActivity().finish(); +// getActivity().finish(); + }); + builder.setPositiveButton("OK", (dialog, which) -> { }); - builder.setPositiveButton("OK",(dialog, which) -> {}); SharedPreferences.Editor edit = preferences.edit(); edit.putBoolean(getString(R.string.watchlist_dialog_needed), false); - edit.commit(); + edit.apply(); builder.create().show(); } - } else if (action.getId() == REMOVE_WATCHLIST_ACTION) { - if (watchlistItem != null) { -// watchlistItem.delete(); - } - recordingActionsAdapter.replace(0, new Action(ADD_WATCHLIST_ACTION, EventsDetailsFragment.this.getString(R.string.add_to_watchlist))); - } else { + } else if (action.getId() == ACTION_REMOVE_WATCHLIST) { + viewModel.removeBookmark(event.getGuid()); + } else if (action.getId() == ACTION_PLAY) { Intent i = new Intent(EventsDetailsFragment.this.getActivity(), PlayerActivity.class); i.putExtra(DetailsActivity.Companion.getTYPE(), eventType); if (eventType == DetailsActivity.Companion.getTYPE_RECORDING()) { i.putExtra(DetailsActivity.Companion.getEVENT(), event); - if (action.getId() == DUMMY_ID) { - PersistentRecording dummy = new PersistentRecording(); - dummy.setRecordingUrl("https://devimages.apple.com.edgekey.net/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8"); - dummy.setMimeType("video/hls"); - dummy.setLanguage("eng"); - dummy.setHighQuality(true); - i.putExtra(DetailsActivity.Companion.getRECORDING(), dummy); - } else { - for (PersistentRecording r : event.getRecordings()) { - if (r.getId() == action.getId()) { - i.putExtra(DetailsActivity.Companion.getRECORDING(), r); - break; - } + for (PersistentRecording r : event.getRecordings()) { + if (r.getId() == action.getId()) { + i.putExtra(DetailsActivity.Companion.getRECORDING(), r); + break; } } } else if (eventType == DetailsActivity.Companion.getTYPE_STREAM()) { diff --git a/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/NewEventsDetailsFragment.kt b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/NewEventsDetailsFragment.kt new file mode 100644 index 00000000..bd352b55 --- /dev/null +++ b/leanback/src/main/java/de/nicidienase/chaosflix/leanback/fragments/NewEventsDetailsFragment.kt @@ -0,0 +1,179 @@ +package de.nicidienase.chaosflix.leanback.fragments + +import android.app.AlertDialog +import android.arch.lifecycle.Observer +import android.arch.lifecycle.ViewModelProviders +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import android.graphics.Bitmap +import android.graphics.drawable.Drawable +import android.os.Bundle +import android.os.Handler +import android.support.design.widget.Snackbar +import android.support.v17.leanback.app.DetailsSupportFragment +import android.support.v17.leanback.widget.Action +import android.support.v17.leanback.widget.ArrayObjectAdapter +import android.support.v17.leanback.widget.ClassPresenterSelector +import android.support.v17.leanback.widget.DetailsOverviewRow +import android.support.v17.leanback.widget.FullWidthDetailsOverviewRowPresenter +import android.support.v17.leanback.widget.FullWidthDetailsOverviewSharedElementHelper +import android.support.v17.leanback.widget.ListRow +import android.support.v17.leanback.widget.ListRowPresenter +import android.support.v17.leanback.widget.OnActionClickedListener +import android.util.Log +import android.widget.Toast +import com.bumptech.glide.Glide +import com.bumptech.glide.request.animation.GlideAnimation +import com.bumptech.glide.request.target.SimpleTarget +import de.nicidienase.chaosflix.R +import de.nicidienase.chaosflix.common.mediadata.entities.recording.persistence.PersistentEvent +import de.nicidienase.chaosflix.common.mediadata.entities.streaming.Room +import de.nicidienase.chaosflix.common.viewmodel.DetailsViewModel +import de.nicidienase.chaosflix.common.viewmodel.ViewModelFactory +import de.nicidienase.chaosflix.leanback.EventDetailsDescriptionPresenter +import de.nicidienase.chaosflix.leanback.activities.ConferencesActivity +import de.nicidienase.chaosflix.leanback.activities.DetailsActivity +import de.nicidienase.chaosflix.leanback.activities.EventDetailsActivity +import de.nicidienase.chaosflix.leanback.activities.PlayerActivity + + +class NewEventsDetailsFragment : DetailsSupportFragment() { + + private lateinit var viewModel: DetailsViewModel + + private var eventType: Int = DetailsActivity.TYPE_RECORDING + private var event: PersistentEvent? = null + + private lateinit var rowsAdapter: ArrayObjectAdapter + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + viewModel = ViewModelProviders.of(this, ViewModelFactory(requireContext())).get(DetailsViewModel::class.java) + + eventType = activity!!.intent.getIntExtra(DetailsActivity.TYPE, -1) + event = requireActivity().intent.getParcelableExtra(DetailsActivity.EVENT) + + if (eventType != DetailsActivity.TYPE_RECORDING || event == null) { + return + } + + title = event?.title + + val selector = ClassPresenterSelector() + val detailsPresenter = FullWidthDetailsOverviewRowPresenter( + EventDetailsDescriptionPresenter(requireContext())) + + val helper = FullWidthDetailsOverviewSharedElementHelper() + helper.setSharedElementEnterTransition(activity, + EventDetailsActivity.SHARED_ELEMENT_NAME) + detailsPresenter.setListener(helper) + prepareEntranceTransition() + + detailsPresenter.onActionClickedListener = DetailActionClickedListener() + + selector.addClassPresenter(DetailsOverviewRow::class.java, detailsPresenter) + selector.addClassPresenter(ListRow::class.java, + ListRowPresenter()) + rowsAdapter = ArrayObjectAdapter(selector) + + val detailsOverview = DetailsOverviewRow(event) + Glide.with(activity) + .load(event?.thumbUrl) + .asBitmap() + .into(object : SimpleTarget(DETAIL_THUMB_WIDTH, DETAIL_THUMB_HEIGHT) { + override fun onResourceReady(resource: Bitmap, + glideAnimation: GlideAnimation) { + detailsOverview.setImageBitmap(requireActivity(), resource) + } + + override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) { + // super.onLoadFailed(e, errorDrawable); + detailsOverview.setImageDrawable(resources.getDrawable(DEFAULT_DRAWABLE)) + } + }) + + val actionAdapter = ArrayObjectAdapter() + + val playAction = Action(ACTION_PLAY, "Play") + actionAdapter.add(playAction) + + val watchlistAction = Action(ACTION_ADD_WATCHLIST, getString(R.string.add_to_watchlist)) + actionAdapter.add(watchlistAction) + event?.guid?.let { + viewModel.getBookmarkForEvent(it).observe(this, Observer { watchlistItem -> + if (watchlistItem != null) { + watchlistAction.id = ACTION_REMOVE_WATCHLIST + watchlistAction.label1 = getString(R.string.remove_from_watchlist) + actionAdapter.notifyItemRangeChanged(actionAdapter.indexOf(watchlistAction),1) + } else { + watchlistAction.id = ACTION_ADD_WATCHLIST + watchlistAction.label1 = getString(R.string.add_to_watchlist) + actionAdapter.notifyItemRangeChanged(actionAdapter.indexOf(watchlistAction),1) + } + }) + } + + detailsOverview.actionsAdapter = actionAdapter + rowsAdapter.add(detailsOverview); + + adapter = rowsAdapter + + Handler().postDelayed(this::startEntranceTransition, 500); + } + + companion object { + @JvmStatic + val TAG = NewEventsDetailsFragment::class.java.simpleName + + @JvmStatic + private val DETAIL_THUMB_WIDTH = 254 + @JvmStatic + private val DETAIL_THUMB_HEIGHT = 143 + @JvmStatic + val DEFAULT_DRAWABLE = R.drawable.default_background + + + @JvmStatic + private val ACTION_PLAY: Long = 0 + @JvmStatic + private val ACTION_ADD_WATCHLIST: Long = 1 + @JvmStatic + private val ACTION_REMOVE_WATCHLIST: Long = 2 + } + + private inner class DetailActionClickedListener : OnActionClickedListener { + override fun onActionClicked(action: Action) { + Log.d(TAG, "OnActionClicked") + if (action.id == ACTION_ADD_WATCHLIST) { + event?.guid?.let { viewModel.createBookmark(it) } + val preferences = requireActivity().getSharedPreferences(getString(R.string.watchlist_preferences_key), Context.MODE_PRIVATE) + if (preferences.getBoolean(getString(R.string.watchlist_dialog_needed), true)) { // new item + showWatchlistInfoDialog(preferences) + } + } else if (action.id == ACTION_REMOVE_WATCHLIST) { + event?.guid?.let { viewModel.removeBookmark(it) } + } else if (action.id == ACTION_PLAY) { + // TODO play! + Toast.makeText(requireContext(), "Play", Toast.LENGTH_SHORT).show() + } + } + + fun showWatchlistInfoDialog(preferences: SharedPreferences) { + val builder = AlertDialog.Builder(activity) + builder.setTitle(R.string.watchlist_message) + builder.setNegativeButton(R.string.return_to_homescreen) { dialog, which -> + val i = Intent(activity, ConferencesActivity::class.java) + i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + startActivity(i) + // getActivity().finish(); + } + builder.setPositiveButton("OK") { dialog, which -> } + val edit = preferences.edit() + edit.putBoolean(getString(R.string.watchlist_dialog_needed), false) + edit.apply() + + builder.create().show() + } + } +} \ No newline at end of file diff --git a/leanback/src/main/res/layout/activity_event_details.xml b/leanback/src/main/res/layout/activity_event_details.xml index a0e39951..0725e3be 100644 --- a/leanback/src/main/res/layout/activity_event_details.xml +++ b/leanback/src/main/res/layout/activity_event_details.xml @@ -1,6 +1,6 @@