Refactored AbstractDetailsFragment

This introduces the MVC model to details fragments. It moves as much
view and control code out of the concrete fragments into the abstract
classes.

   * Added UML class and sequence diagrams under doc/diagrams to clarify
     the new setup

   * Introduces new abstract classes
      * AbstractFragment class to hold the DataHolder
      * AbstractInfoFragment class to display media information
      * AbstractAddtionalInfoFragment class to allow *InfoFragments
        to add additional UI elements and propagate refresh requests.
        See for an example TVShowInfoFragment which adds
        TVShowProgressFragment to display next episodes and season
        progression.

   * Introduces new RefreshItem class to encapsulate all refresh
     functionality from AbstractDetailsFragment

   * Introduces new SharedElementTransition utility class to encapsulate
     all shared element transition code

   * Introduces new CastFragment class to encapsulate all code for
     displaying casts reducing code duplication

   * Introduces DataHolder class to replace passing the ViewHolder from
     the *ListFragment to the *DetailsFragment or *InfoFragment

   * Refactored AbstractDetailsFragment into two classes:

     o AbstractDetailsFragment: for fragments requiring a tab bar
     o AbstractInfoFragment:    for fragments showing media information

     We used to use <NAME>DetailsFragments for both fragments that show
     generic
     info about some media item and fragments that hold all details for
     some media item.
     For example, artist details showed artist info and used tabs to
     show artist albums and songs as well. Now Details fragments are
     used to show all details, Info fragments only show media item
     information like description, title, rating, etc.

   * Moved swiperefreshlayout code from AbstractCursorListFragment to
     AbstractListFragment
This commit is contained in:
Martijn Brekhof 2016-12-30 09:27:24 +01:00
parent d3a68b73c9
commit 412931b8db
67 changed files with 5914 additions and 7153 deletions

View File

@ -49,8 +49,8 @@ public class RestoreSearchQueryViewPagerTest {
private final int ARTIST_SEARCH_QUERY_LIST_SIZE = 2;
private final String ALBUMS_SEARCH_QUERY = "tes";
private final int ALBUM_SEARCH_QUERY_LIST_SIZE = 3;
private final int ARTIST_COMPLETE_LIST_SIZE = 228;
private final int ALBUM_COMPLETE_LIST_SIZE = 234;
private final int ARTIST_COMPLETE_LIST_SIZE = 229;
private final int ALBUM_COMPLETE_LIST_SIZE = 235;
private LoaderIdlingResource loaderIdlingResource;

View File

@ -0,0 +1,21 @@
/*
* Copyright 2017 Martijn Brekhof. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xbmc.kore.ui;
abstract public class AbstractAdditionalInfoFragment extends AbstractFragment {
abstract protected void refresh();
}

View File

@ -27,7 +27,6 @@ import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.LayoutInflater;
@ -55,19 +54,15 @@ import de.greenrobot.event.EventBus;
public abstract class AbstractCursorListFragment extends AbstractListFragment
implements LoaderManager.LoaderCallbacks<Cursor>,
SyncUtils.OnServiceListener,
SearchView.OnQueryTextListener,
SwipeRefreshLayout.OnRefreshListener {
SearchView.OnQueryTextListener {
private static final String TAG = LogUtils.makeLogTag(AbstractCursorListFragment.class);
private final String BUNDLE_KEY_SEARCH_QUERY = "search_query";
private ServiceConnection serviceConnection;
private HostInfo hostInfo;
private EventBus bus;
private CursorAdapter adapter;
// Loader IDs
private static final int LOADER = 0;
@ -89,13 +84,6 @@ public abstract class AbstractCursorListFragment extends AbstractListFragment
View root = super.onCreateView(inflater, container, savedInstanceState);
bus = EventBus.getDefault();
HostManager hostManager = HostManager.getInstance(getActivity());
hostInfo = hostManager.getHostInfo();
swipeRefreshLayout.setEnabled(true);
swipeRefreshLayout.setOnRefreshListener(this);
adapter = (CursorAdapter) getAdapter();
if (savedInstanceState != null) {
savedSearchFilter = savedInstanceState.getString(BUNDLE_KEY_SEARCH_QUERY);
@ -177,22 +165,6 @@ public abstract class AbstractCursorListFragment extends AbstractListFragment
return super.onOptionsItemSelected(item);
}
/**
* Swipe refresh layout callback
*/
/** {@inheritDoc} */
@Override
public void onRefresh() {
if (hostInfo != null) {
showRefreshAnimation();
onSwipeRefresh();
} else {
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(getActivity(), R.string.no_xbmc_configured, Toast.LENGTH_SHORT)
.show();
}
}
/**
* Should return the {@link LibrarySyncService} SyncType that
* this list initiates
@ -237,7 +209,7 @@ public abstract class AbstractCursorListFragment extends AbstractListFragment
}
if (event.syncType.equals(getListSyncType())) {
swipeRefreshLayout.setRefreshing(false);
hideRefreshAnimation();
if (event.status == MediaSyncEvent.STATUS_SUCCESS) {
refreshList();
if (!silentSync) {
@ -273,10 +245,10 @@ public abstract class AbstractCursorListFragment extends AbstractListFragment
this.supportsSearch = supportsSearch;
}
protected void onSwipeRefresh() {
LogUtils.LOGD(TAG, "Swipe, starting sync for: " + getListSyncType());
// Start the syncing process
Intent syncIntent = new Intent(this.getActivity(), LibrarySyncService.class);
@Override
public void onRefresh() {
showRefreshAnimation();
Intent syncIntent = new Intent(this.getActivity(), LibrarySyncService.class);
syncIntent.putExtra(getListSyncType(), true);
String syncID = getSyncID();
@ -327,16 +299,18 @@ public abstract class AbstractCursorListFragment extends AbstractListFragment
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
loaderLoading = false;
adapter.swapCursor(cursor);
// To prevent the empty text from appearing on the first load, set it now
emptyView.setText(getString(R.string.swipe_down_to_refresh));
((CursorAdapter) getAdapter()).swapCursor(cursor);
if (TextUtils.isEmpty(searchFilter)) {
// To prevent the empty text from appearing on the first load, set it now
emptyView.setText(getString(R.string.swipe_down_to_refresh));
}
loaderLoading = false;
}
/** {@inheritDoc} */
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
adapter.swapCursor(null);
((CursorAdapter) getAdapter()).swapCursor(null);
}
/**

View File

@ -1,321 +0,0 @@
/*
* Copyright 2015 Martijn Brekhof. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xbmc.kore.ui;
import android.Manifest;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v13.app.FragmentCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import org.xbmc.kore.R;
import org.xbmc.kore.Settings;
import org.xbmc.kore.host.HostInfo;
import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.ApiException;
import org.xbmc.kore.jsonrpc.event.MediaSyncEvent;
import org.xbmc.kore.service.library.LibrarySyncService;
import org.xbmc.kore.service.library.SyncUtils;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.UIUtils;
import org.xbmc.kore.utils.Utils;
import butterknife.OnClick;
import butterknife.Optional;
import de.greenrobot.event.EventBus;
abstract public class AbstractDetailsFragment extends Fragment
implements SwipeRefreshLayout.OnRefreshListener,
SyncUtils.OnServiceListener {
private static final String TAG = LogUtils.makeLogTag(AbstractDetailsFragment.class);
private HostManager hostManager;
private HostInfo hostInfo;
private EventBus bus;
private String syncType;
private SwipeRefreshLayout swipeRefreshLayout;
private ServiceConnection serviceConnection;
// Used to hide the refresh animation when a silent refresh is issued
private boolean silentRefresh = false;
abstract protected View createView(LayoutInflater inflater, ViewGroup container);
/**
* Should return {@link LibrarySyncService} SyncType that
* this fragment initiates
* @return {@link LibrarySyncService} SyncType
*/
abstract protected String getSyncType();
/**
* Should return the {@link LibrarySyncService} syncID if this fragment
* synchronizes a single item. The itemId that should be synced must returned by {@link #getSyncItemID()}
* @return {@link LibrarySyncService} SyncID
*/
abstract protected String getSyncID();
/**
* Should return the item ID for SyncID returned by {@link #getSyncID()}
* @return -1 if not used.
*/
abstract protected int getSyncItemID();
/**
* Should return the SwipeRefreshLayout if the fragment's view uses one.
* Used to notify the user if a sync for synctype returned by {@link #getSyncType()}
* is currently in progress
* @return
*/
abstract protected SwipeRefreshLayout getSwipeRefreshLayout();
/**
* When the view created in {@link #createView(LayoutInflater, ViewGroup)} contains a button
* with resource identifier R.id.download this will be called to initiate the download
*/
abstract protected void onDownload();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bus = EventBus.getDefault();
hostManager = HostManager.getInstance(getActivity());
hostInfo = hostManager.getHostInfo();
syncType = getSyncType();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
// We're not being shown or there's nothing to show
return null;
}
View view = createView(inflater, container);
if( view != null ) {
swipeRefreshLayout = getSwipeRefreshLayout();
if( swipeRefreshLayout != null ) {
swipeRefreshLayout.setOnRefreshListener(this);
}
}
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onStart() {
super.onStart();
serviceConnection = SyncUtils.connectToLibrarySyncService(getActivity(), this);
}
@Override
public void onResume() {
bus.register(this);
super.onResume();
}
@Override
public void onPause() {
bus.unregister(this);
super.onPause();
}
@Override
public void onStop() {
super.onStop();
SyncUtils.disconnectFromLibrarySyncService(getActivity(), serviceConnection);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.refresh_item, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
onRefresh();
}
return super.onOptionsItemSelected(item);
}
@Optional
@OnClick(R.id.download)
public void onDownloadClicked(View v) {
boolean hasStoragePermission =
ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if (!hasStoragePermission) {
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
Utils.PERMISSION_REQUEST_WRITE_STORAGE);
return;
}
if (Settings.allowedDownloadNetworkTypes(getActivity()) != 0) {
onDownload();
} else {
Toast.makeText(getActivity(), R.string.no_connection_type_selected, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case Utils.PERMISSION_REQUEST_WRITE_STORAGE:
// If request is cancelled, the result arrays are empty.
if ((grantResults.length > 0) &&
(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
onDownloadClicked(null);
} else {
Toast.makeText(getActivity(), R.string.write_storage_permission_denied, Toast.LENGTH_SHORT)
.show();
}
break;
}
}
protected void startSync(boolean silentRefresh) {
this.silentRefresh = silentRefresh;
LogUtils.LOGD(TAG, "Starting syc. Silent? " + silentRefresh);
if (getHostInfo() != null) {
if ((swipeRefreshLayout != null) && (!silentRefresh)) {
UIUtils.showRefreshAnimation(swipeRefreshLayout);
}
// Start the syncing process
Intent syncIntent = new Intent(this.getActivity(), LibrarySyncService.class);
if (syncType != null) {
syncIntent.putExtra(syncType, true);
}
String syncID = getSyncID();
int itemId = getSyncItemID();
if ((syncID != null) && (itemId != -1)) {
syncIntent.putExtra(syncID, itemId);
}
Bundle syncExtras = new Bundle();
syncExtras.putBoolean(LibrarySyncService.SILENT_SYNC, silentRefresh);
syncIntent.putExtra(LibrarySyncService.SYNC_EXTRAS, syncExtras);
getActivity().startService(syncIntent);
} else {
if (swipeRefreshLayout != null) {
swipeRefreshLayout.setRefreshing(false);
}
Toast.makeText(getActivity(), R.string.no_xbmc_configured, Toast.LENGTH_SHORT)
.show();
}
}
/**
* Swipe refresh layout callback
*/
/** {@inheritDoc} */
@Override
public void onRefresh () {
startSync(false);
}
/**
* Event bus post. Called when the syncing process ended
*
* @param event Refreshes data
*/
public void onEventMainThread(MediaSyncEvent event) {
if ((syncType == null) || (! event.syncType.equals(syncType)))
return;
boolean silentSync = false;
if (event.syncExtras != null) {
silentSync = event.syncExtras.getBoolean(LibrarySyncService.SILENT_SYNC, false);
}
if( swipeRefreshLayout != null ) {
swipeRefreshLayout.setRefreshing(false);
}
onSyncProcessEnded(event);
if (event.status == MediaSyncEvent.STATUS_SUCCESS) {
if (!silentSync) {
Toast.makeText(getActivity(),
R.string.sync_successful, Toast.LENGTH_SHORT)
.show();
}
} else if (!silentSync) {
String msg = (event.errorCode == ApiException.API_ERROR) ?
String.format(getString(R.string.error_while_syncing), event.errorMessage) :
getString(R.string.unable_to_connect_to_xbmc);
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onServiceConnected(LibrarySyncService librarySyncService) {
if (syncType == null)
return;
if (!silentRefresh &&
(swipeRefreshLayout != null) &&
SyncUtils.isLibrarySyncing(librarySyncService,
HostManager.getInstance(getActivity()).getHostInfo(),
syncType)) {
LogUtils.LOGD(TAG, "Showing refresh animation");
UIUtils.showRefreshAnimation(swipeRefreshLayout);
}
}
/**
* Called when sync process for type set through {@link #getSyncType()} ends
* @param event
*/
abstract protected void onSyncProcessEnded(MediaSyncEvent event);
protected HostManager getHostManager() {
return hostManager;
}
protected HostInfo getHostInfo() {
return hostInfo;
}
}

View File

@ -0,0 +1,183 @@
/*
* Copyright 2017 Martijn Brekhof. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xbmc.kore.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
public class AbstractFragment extends Fragment {
private AbstractInfoFragment.DataHolder dataHolder;
public void setDataHolder(AbstractInfoFragment.DataHolder dataHolder) {
this.dataHolder = dataHolder;
Bundle bundle = getArguments();
if (bundle == null) {
setArguments(dataHolder.getBundle());
} else {
bundle.putAll(dataHolder.getBundle());
}
}
public AbstractInfoFragment.DataHolder getDataHolder() {
return dataHolder;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( this.dataHolder == null ) {
this.dataHolder = new AbstractInfoFragment.DataHolder(-1);
}
this.dataHolder.setBundle(getArguments());
}
public static class DataHolder {
static final String POSTER_TRANS_NAME = "POSTER_TRANS_NAME";
static final String BUNDLE_KEY_ID = "id";
static final String BUNDLE_KEY_TITLE = "title";
static final String BUNDLE_KEY_UNDERTITLE = "undertitle";
static final String BUNDLE_KEY_DESCRIPTION = "description";
static final String BUNDLE_KEY_DETAILS = "details";
static final String BUNDLE_KEY_POSTERURL = "poster";
static final String BUNDLE_KEY_FANARTURL = "fanart";
static final String BUNDLE_KEY_SQUAREPOSTER = "squareposter";
static final String BUNDLE_KEY_RATING = "rating";
static final String BUNDLE_KEY_MAXRATING = "maxrating";
static final String BUNDLE_KEY_VOTES = "votes";
private Bundle bundle;
private DataHolder() {}
public DataHolder(Bundle bundle) {
setBundle(bundle);
}
public DataHolder(int itemId) {
bundle = new Bundle();
bundle.putInt(BUNDLE_KEY_ID, itemId);
}
public void setBundle(Bundle bundle) {
this.bundle = bundle;
}
public void setPosterTransitionName(String posterTransitionName) {
bundle.putString(POSTER_TRANS_NAME, posterTransitionName);
}
public void setSquarePoster(boolean squarePoster) {
bundle.putBoolean(BUNDLE_KEY_SQUAREPOSTER, squarePoster);
}
public void setRating(double rating) {
bundle.putDouble(BUNDLE_KEY_RATING, rating);
}
public void setMaxRating(int maxRating) {
bundle.putInt(BUNDLE_KEY_MAXRATING, maxRating);
}
public void setVotes(int votes) {
bundle.putInt(BUNDLE_KEY_VOTES, votes);
}
public void setPosterUrl(String posterUrl) {
bundle.putString(BUNDLE_KEY_POSTERURL, posterUrl);
}
public void setTitle(String title) {
bundle.putString(BUNDLE_KEY_TITLE, title);
}
public void setUndertitle(String underTitle) {
bundle.putString(BUNDLE_KEY_UNDERTITLE, underTitle);
}
public void setDescription(String description) {
bundle.putString(BUNDLE_KEY_DESCRIPTION, description);
}
public void setDetails(String details) {
bundle.putString(BUNDLE_KEY_DETAILS, details);
}
public void setFanArtUrl(String fanArtUrl) {
bundle.putString(BUNDLE_KEY_FANARTURL, fanArtUrl);
}
public void setId(int id) {
bundle.putInt(BUNDLE_KEY_ID, id);
}
public String getPosterTransitionName() {
return bundle.getString(POSTER_TRANS_NAME);
}
public boolean getSquarePoster() {
return bundle.getBoolean(BUNDLE_KEY_SQUAREPOSTER);
}
public double getRating() {
return bundle.getDouble(BUNDLE_KEY_RATING);
}
public int getMaxRating() {
return bundle.getInt(BUNDLE_KEY_MAXRATING);
}
public int getVotes() {
return bundle.getInt(BUNDLE_KEY_VOTES);
}
public String getPosterUrl() {
return bundle.getString(BUNDLE_KEY_POSTERURL);
}
public String getTitle() {
return bundle.getString(BUNDLE_KEY_TITLE);
}
public String getUnderTitle() {
return bundle.getString(BUNDLE_KEY_UNDERTITLE);
}
public String getDescription() {
return bundle.getString(BUNDLE_KEY_DESCRIPTION);
}
public String getDetails() {
return bundle.getString(BUNDLE_KEY_DETAILS);
}
public String getFanArtUrl() {
return bundle.getString(BUNDLE_KEY_FANARTURL);
}
public int getId() {
return bundle.getInt(BUNDLE_KEY_ID);
}
public Bundle getBundle() {
return bundle;
}
}
}

View File

@ -0,0 +1,597 @@
/*
* Copyright 2015 Martijn Brekhof. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.xbmc.kore.ui;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.melnykov.fab.FloatingActionButton;
import com.melnykov.fab.ObservableScrollView;
import org.xbmc.kore.R;
import org.xbmc.kore.Settings;
import org.xbmc.kore.host.HostInfo;
import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.ApiCallback;
import org.xbmc.kore.jsonrpc.method.Player;
import org.xbmc.kore.jsonrpc.type.PlaylistType;
import org.xbmc.kore.service.library.LibrarySyncService;
import org.xbmc.kore.service.library.SyncUtils;
import org.xbmc.kore.ui.generic.RefreshItem;
import org.xbmc.kore.utils.LogUtils;
import org.xbmc.kore.utils.SharedElementTransition;
import org.xbmc.kore.utils.UIUtils;
import org.xbmc.kore.utils.Utils;
import java.util.Locale;
import at.blogc.android.views.ExpandableTextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import static android.view.View.GONE;
abstract public class AbstractInfoFragment extends AbstractFragment
implements SwipeRefreshLayout.OnRefreshListener,
SyncUtils.OnServiceListener,
SharedElementTransition.SharedElement {
private static final String TAG = LogUtils.makeLogTag(AbstractInfoFragment.class);
// Detail views
@InjectView(R.id.swipe_refresh_layout) SwipeRefreshLayout swipeRefreshLayout;
@InjectView(R.id.media_panel) ScrollView panelScrollView;
@InjectView(R.id.art) ImageView artImageView;
@InjectView(R.id.poster) ImageView posterImageView;
@InjectView(R.id.media_title) TextView titleTextView;
@InjectView(R.id.media_undertitle) TextView underTitleTextView;
@InjectView(R.id.rating_container) LinearLayout ratingContainer;
@InjectView(R.id.rating) TextView ratingTextView;
@InjectView(R.id.rating_votes) TextView ratingVotesTextView;
@InjectView(R.id.max_rating) TextView maxRatingTextView;
@InjectView(R.id.media_details_right) TextView detailsRightTextView;
@InjectView(R.id.media_details) LinearLayout mediaDetailsContainer;
@InjectView(R.id.media_action_download) ImageButton downloadButton;
@InjectView(R.id.media_action_pin_unpin) ImageButton pinUnpinButton;
@InjectView(R.id.media_action_add_to_playlist) ImageButton addToPlaylistButton;
@InjectView(R.id.media_action_seen) ImageButton seenButton;
@InjectView(R.id.media_action_go_to_imdb) ImageButton imdbButton;
@InjectView(R.id.media_actions_bar) LinearLayout mediaActionsBar;
@InjectView(R.id.media_description) ExpandableTextView descriptionExpandableTextView;
@InjectView(R.id.media_description_container) LinearLayout descriptionContainer;
@InjectView(R.id.show_all) ImageView expansionImage;
@InjectView(R.id.fab) ImageButton fabButton;
@InjectView(R.id.exit_transition_view) View exitTransitionView;
private HostManager hostManager;
private HostInfo hostInfo;
private ServiceConnection serviceConnection;
private RefreshItem refreshItem;
private boolean expandDescription;
/**
* Handler on which to post RPC callbacks
*/
private Handler callbackHandler = new Handler();
/**
* Use {@link #setDataHolder(DataHolder)}
* to provide the required info after creating a new instance of this Fragment
*/
public AbstractInfoFragment() {
super();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hostManager = HostManager.getInstance(getActivity());
hostInfo = hostManager.getHostInfo();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
// We're not being shown or there's nothing to show
return null;
}
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_info, container, false);
ButterKnife.inject(this, root);
// Setup dim the fanart when scroll changes. Full dim on 4 * iconSize dp
Resources resources = getActivity().getResources();
final int pixelsToTransparent = 4 * resources.getDimensionPixelSize(R.dimen.default_icon_size);
panelScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
float y = panelScrollView.getScrollY();
float newAlpha = Math.min(1, Math.max(0, 1 - (y / pixelsToTransparent)));
artImageView.setAlpha(newAlpha);
}
});
DataHolder dataHolder = getDataHolder();
if(!dataHolder.getSquarePoster()) {
posterImageView.getLayoutParams().width =
resources.getDimensionPixelSize(R.dimen.detail_poster_width_nonsquare);
posterImageView.getLayoutParams().height =
resources.getDimensionPixelSize(R.dimen.detail_poster_height_nonsquare);
}
if(getRefreshItem() != null) {
swipeRefreshLayout.setOnRefreshListener(this);
} else {
swipeRefreshLayout.setEnabled(false);
}
FloatingActionButton fab = (FloatingActionButton)fabButton;
fab.attachToScrollView((ObservableScrollView) panelScrollView);
if(Utils.isLollipopOrLater()) {
posterImageView.setTransitionName(dataHolder.getPosterTransitionName());
}
if (savedInstanceState == null) {
FragmentManager fragmentManager = getChildFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.media_additional_info);
if (fragment == null) {
fragment = getAdditionalInfoFragment();
if (fragment != null) {
fragmentManager.beginTransaction()
.add(R.id.media_additional_info, fragment)
.commit();
}
}
}
if(setupMediaActionBar()) {
mediaActionsBar.setVisibility(View.VISIBLE);
}
if(setupFAB(fabButton)) {
fabButton.setVisibility(View.VISIBLE);
}
updateView(dataHolder);
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.refresh_item, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onStart() {
super.onStart();
serviceConnection = SyncUtils.connectToLibrarySyncService(getActivity(), this);
}
@Override
public void onResume() {
// Force the exit view to invisible
exitTransitionView.setVisibility(View.INVISIBLE);
if ( refreshItem != null ) {
refreshItem.register();
}
super.onResume();
}
@Override
public void onPause() {
if ( refreshItem != null ) {
refreshItem.unregister();
}
super.onPause();
}
@Override
public void onStop() {
super.onStop();
SyncUtils.disconnectFromLibrarySyncService(getActivity(), serviceConnection);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
onRefresh();
}
return super.onOptionsItemSelected(item);
}
/**
* Swipe refresh layout callback
*/
/** {@inheritDoc} */
@Override
public void onRefresh () {
if (getRefreshItem() == null) {
Toast.makeText(getActivity(), R.string.Refreshing_not_implemented_for_this_item,
Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
return;
}
refreshItem.setSwipeRefreshLayout(swipeRefreshLayout);
refreshItem.startSync(false);
}
@Override
public void onServiceConnected(LibrarySyncService librarySyncService) {
if (getRefreshItem() == null) {
return;
}
if (SyncUtils.isLibrarySyncing(librarySyncService,
HostManager.getInstance(getActivity()).getHostInfo(),
refreshItem.getSyncType())) {
UIUtils.showRefreshAnimation(swipeRefreshLayout);
refreshItem.setSwipeRefreshLayout(swipeRefreshLayout);
refreshItem.register();
}
}
protected void setFabButtonState(boolean enable) {
if(enable) {
fabButton.setVisibility(View.VISIBLE);
} else {
fabButton.setVisibility(GONE);
}
}
protected void fabActionPlayItem(PlaylistType.Item item) {
if (item == null) {
Toast.makeText(getActivity(), R.string.no_item_available_to_play, Toast.LENGTH_SHORT).show();
return;
}
Player.Open action = new Player.Open(item);
action.execute(HostManager.getInstance(getActivity()).getConnection(), new ApiCallback<String>() {
@Override
public void onSuccess(String result) {
if (!isAdded()) return;
// Check whether we should switch to the remote
boolean switchToRemote = PreferenceManager
.getDefaultSharedPreferences(getActivity())
.getBoolean(Settings.KEY_PREF_SWITCH_TO_REMOTE_AFTER_MEDIA_START,
Settings.DEFAULT_PREF_SWITCH_TO_REMOTE_AFTER_MEDIA_START);
if (switchToRemote) {
int cx = (fabButton.getLeft() + fabButton.getRight()) / 2;
int cy = (fabButton.getTop() + fabButton.getBottom()) / 2;
UIUtils.switchToRemoteWithAnimation(getActivity(), cx, cy, exitTransitionView);
}
}
@Override
public void onError(int errorCode, String description) {
if (!isAdded()) return;
// Got an error, show toast
Toast.makeText(getActivity(), R.string.unable_to_connect_to_xbmc, Toast.LENGTH_SHORT)
.show();
}
}, callbackHandler);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case Utils.PERMISSION_REQUEST_WRITE_STORAGE:
// If request is cancelled, the result arrays are empty.
if ((grantResults.length > 0) &&
(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
downloadButton.performClick();
} else {
Toast.makeText(getActivity(), R.string.write_storage_permission_denied, Toast.LENGTH_SHORT)
.show();
}
break;
}
}
@Override
@TargetApi(21)
public boolean isSharedElementVisible() {
return UIUtils.isViewInBounds(panelScrollView, posterImageView);
}
protected void refreshAdditionInfoFragment() {
Fragment fragment = getChildFragmentManager().findFragmentById(R.id.media_additional_info);
if (fragment != null)
((AbstractAdditionalInfoFragment) fragment).refresh();
}
protected HostManager getHostManager() {
return hostManager;
}
protected HostInfo getHostInfo() {
return hostInfo;
}
/**
* Call this when you are ready to provide the titleTextView, undertitle, details, descriptionExpandableTextView, etc. etc.
*/
protected void updateView(DataHolder dataHolder) {
titleTextView.setText(dataHolder.getTitle());
underTitleTextView.setText(dataHolder.getUnderTitle());
detailsRightTextView.setText(dataHolder.getDetails());
if (!TextUtils.isEmpty(dataHolder.getDescription())) {
Resources.Theme theme = getActivity().getTheme();
TypedArray styledAttributes = theme.obtainStyledAttributes(new int[]{
R.attr.iconExpand,
R.attr.iconCollapse
});
final int iconCollapseResId =
styledAttributes.getResourceId(styledAttributes.getIndex(0), R.drawable.ic_expand_less_white_24dp);
final int iconExpandResId =
styledAttributes.getResourceId(styledAttributes.getIndex(1), R.drawable.ic_expand_more_white_24dp);
styledAttributes.recycle();
descriptionContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
descriptionExpandableTextView.toggle();
expansionImage.setImageResource(descriptionExpandableTextView.isExpanded() ? iconCollapseResId : iconExpandResId);
}
});
descriptionExpandableTextView.setText(dataHolder.getDescription());
if (expandDescription) {
descriptionExpandableTextView.expand();
expansionImage.setImageResource(iconExpandResId);
}
descriptionContainer.setVisibility(View.VISIBLE);
} else {
descriptionContainer.setVisibility(GONE);
}
// Images
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
Resources resources = getActivity().getResources();
if (dataHolder.getPosterUrl() != null) {
int posterWidth;
int posterHeight;
if (dataHolder.getSquarePoster()) {
posterWidth = resources.getDimensionPixelOffset(R.dimen.detail_poster_width_square);
posterHeight = resources.getDimensionPixelOffset(R.dimen.detail_poster_height_square);
} else {
posterWidth = resources.getDimensionPixelOffset(R.dimen.detail_poster_width_nonsquare);
posterHeight = resources.getDimensionPixelOffset(R.dimen.detail_poster_height_nonsquare);
}
UIUtils.loadImageWithCharacterAvatar(getActivity(), hostManager,
dataHolder.getPosterUrl(), dataHolder.getTitle(),
posterImageView, posterWidth, posterHeight);
} else {
posterImageView.setVisibility(GONE);
int padding = getActivity().getResources().getDimensionPixelSize(R.dimen.default_padding);
titleTextView.setPadding(padding, padding, 0, 0);
underTitleTextView.setPadding(padding, padding, 0, 0);
}
int artHeight = resources.getDimensionPixelOffset(R.dimen.detail_art_height);
int artWidth = displayMetrics.widthPixels;
UIUtils.loadImageIntoImageview(hostManager,
TextUtils.isEmpty(dataHolder.getFanArtUrl()) ?
dataHolder.getPosterUrl() : dataHolder.getFanArtUrl(),
artImageView, artWidth, artHeight);
if (dataHolder.getRating() > 0) {
ratingTextView.setText(String.format(Locale.getDefault(), "%01.01f", dataHolder.getRating()));
if (dataHolder.getMaxRating() > 0) {
maxRatingTextView.setText(String.format(getString(R.string.max_rating),
String.valueOf(dataHolder.getMaxRating())));
}
if (dataHolder.getVotes() > 0 ) {
ratingVotesTextView.setText(String.format(getString(R.string.votes),
String.valueOf(dataHolder.getVotes())));
}
ratingContainer.setVisibility(View.VISIBLE);
} else if (TextUtils.isEmpty(dataHolder.getDetails())) {
mediaDetailsContainer.setVisibility(View.GONE);
}
}
/**
* Setting a listener for downloads will add the download button to the UI
* @param listener to be called when user clicks the download button. Note that the View passed
* into onClick from {@link android.view.View.OnClickListener} will be null
* when the user is asked for storage permissions
*/
protected void setOnDownloadListener(final View.OnClickListener listener) {
downloadButton.setVisibility(View.VISIBLE);
downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkStoragePermission()) {
if (Settings.allowedDownloadNetworkTypes(getActivity()) != 0) {
listener.onClick(view);
setButtonState(downloadButton, true);
} else {
Toast.makeText(getActivity(), R.string.no_connection_type_selected, Toast.LENGTH_SHORT).show();
}
}
}
});
}
protected void setOnAddToPlaylistListener(View.OnClickListener listener) {
addToPlaylistButton.setVisibility(View.VISIBLE);
addToPlaylistButton.setOnClickListener(listener);
}
protected void setOnGoToImdbListener(View.OnClickListener listener) {
imdbButton.setVisibility(View.VISIBLE);
imdbButton.setOnClickListener(listener);
}
/**
* Use {@link #setSeenButtonState(boolean)} to set the state of the seen button
* @param listener
*/
protected void setOnSeenListener(final View.OnClickListener listener) {
setupToggleButton(seenButton, listener);
}
protected void setOnPinClickedListener(final View.OnClickListener listener) {
setupToggleButton(pinUnpinButton, listener);
}
/**
* Uses colors to show to the user the item has been downloaded
* @param state true if item has been watched/listened too, false otherwise
*/
protected void setDownloadButtonState(boolean state) {
setButtonState(downloadButton, state);
}
/**
* Uses colors to show the seen state to the user
* @param state true if item has been watched/listened too, false otherwise
*/
protected void setSeenButtonState(boolean state) {
setToggleButtonState(seenButton, state);
}
protected void setPinButtonState(boolean state) {
setToggleButtonState(pinUnpinButton, state);
}
private void setButtonState(ImageButton button, boolean state) {
if (state) {
UIUtils.highlightImageView(getActivity(), button);
} else {
button.clearColorFilter();
}
}
private void setToggleButtonState(ImageButton button, boolean state) {
setButtonState(button, state);
button.setTag(state);
}
private void setupToggleButton(final ImageButton button, final View.OnClickListener listener) {
button.setVisibility(View.VISIBLE);
button.setTag(false);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onClick(view);
// Boldly invert the state. We depend on the observer to correct the state
// if Kodi or other service didn't honour our request
setToggleButtonState(button, ! (boolean) button.getTag());
}
});
}
private boolean checkStoragePermission() {
boolean hasStoragePermission =
ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if (!hasStoragePermission) {
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
Utils.PERMISSION_REQUEST_WRITE_STORAGE);
return false;
}
return true;
}
protected RefreshItem getRefreshItem() {
if (refreshItem == null) {
refreshItem = createRefreshItem();
}
return refreshItem;
}
protected void setExpandDescription(boolean expandDescription) {
this.expandDescription = expandDescription;
}
abstract protected AbstractAdditionalInfoFragment getAdditionalInfoFragment();
/**
* Called when user commands the information to be renewed. Either through a swipe down
* or a menu call.
* <br/>
* Note, that {@link AbstractAdditionalInfoFragment#refresh()} will be called for an
* additional fragment, if available, automatically.
* @return
*/
abstract protected RefreshItem createRefreshItem();
/**
* Called when the media action bar actions are available and
* you can use {@link #setOnAddToPlaylistListener(View.OnClickListener)},
* {@link #setOnSeenListener(View.OnClickListener)},
* {@link #setOnDownloadListener(View.OnClickListener)},
* {@link #setOnGoToImdbListener(View.OnClickListener)},
* and {@link #setOnPinClickedListener(View.OnClickListener)} to enable
* one or more actions.
* @return true if media action bar should be visible, false otherwise
*/
abstract protected boolean setupMediaActionBar();
/**
* Called when the fab button is available
* @return true to enable the Floating Action Button, false otherwise
*/
abstract protected boolean setupFAB(ImageButton FAB);
}

View File

@ -43,7 +43,8 @@ import org.xbmc.kore.utils.Utils;
import butterknife.ButterKnife;
import butterknife.InjectView;
public abstract class AbstractListFragment extends Fragment {
public abstract class AbstractListFragment extends Fragment implements
SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = LogUtils.makeLogTag(AbstractListFragment.class);
private BaseAdapter adapter;
@ -58,6 +59,12 @@ public abstract class AbstractListFragment extends Fragment {
abstract protected AdapterView.OnItemClickListener createOnItemClickListener();
abstract protected BaseAdapter createAdapter();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = createAdapter();
}
@TargetApi(16)
@Nullable
@Override
@ -65,13 +72,10 @@ public abstract class AbstractListFragment extends Fragment {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_generic_media_list, container, false);
ButterKnife.inject(this, root);
swipeRefreshLayout.setEnabled(false);
swipeRefreshLayout.setOnRefreshListener(this);
gridView.setEmptyView(emptyView);
gridView.setOnItemClickListener(createOnItemClickListener());
// Configure the adapter and start the loader
adapter = createAdapter();
gridView.setAdapter(adapter);
if (savedInstanceState != null) {
@ -99,7 +103,7 @@ public abstract class AbstractListFragment extends Fragment {
gridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
//Make sure menu is updated if it was already created
//Make sure menu is update d if it was already created
getActivity().invalidateOptionsMenu();
}
});
@ -184,6 +188,10 @@ public abstract class AbstractListFragment extends Fragment {
});
}
public void hideRefreshAnimation() {
swipeRefreshLayout.setRefreshing(false);
}
public BaseAdapter getAdapter() {
return adapter;
}