Use Fragment tags to reference to fragments, hide nav elements

This commit is contained in:
Martin Fietz 2015-04-17 11:21:36 +02:00
parent ded0006913
commit c6b88fe356
6 changed files with 209 additions and 126 deletions

View File

@ -442,10 +442,9 @@ public class AudioplayerActivity extends MediaplayerActivity implements ItemDesc
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int viewType = parent.getAdapter().getItemViewType(position);
if (viewType != NavListAdapter.VIEW_TYPE_SECTION_DIVIDER) {
int relPos = (viewType == NavListAdapter.VIEW_TYPE_NAV) ? position : position - NavListAdapter.SUBSCRIPTION_OFFSET;
Intent intent = new Intent(AudioplayerActivity.this, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_NAV_TYPE, viewType);
intent.putExtra(MainActivity.EXTRA_NAV_INDEX, relPos);
intent.putExtra(MainActivity.EXTRA_NAV_INDEX, position);
startActivity(intent);
}
drawerLayout.closeDrawer(navDrawer);

View File

@ -1,5 +1,7 @@
package de.danoeh.antennapod.activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
@ -49,7 +51,9 @@ import de.greenrobot.event.EventBus;
* The activity that is shown when the user launches the app.
*/
public class MainActivity extends ActionBarActivity implements NavDrawerActivity {
private static final String TAG = "MainActivity";
private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED
| EventDistributor.DOWNLOAD_QUEUED
| EventDistributor.FEED_LIST_UPDATE
@ -59,21 +63,23 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
public static final String PREF_IS_FIRST_LAUNCH = "prefMainActivityIsFirstLaunch";
public static final String PREF_LAST_FRAGMENT = "prefMainActivityLastFragment";
public static final String EXTRA_NAV_INDEX = "nav_index";
public static final String EXTRA_NAV_TYPE = "nav_type";
public static final String EXTRA_NAV_INDEX = "nav_index";
public static final String EXTRA_FRAGMENT_TAG = "fragment_tag";
public static final String EXTRA_FRAGMENT_ARGS = "fragment_args";
public static final String SAVE_BACKSTACK_COUNT = "backstackCount";
public static final String SAVE_SELECTED_NAV_INDEX = "selectedNavIndex";
public static final String SAVE_TITLE = "title";
public static final int POS_QUEUE = 0,
POS_NEW = 1,
POS_ALL_EPISODES = 2,
POS_DOWNLOADS = 3,
POS_HISTORY = 4,
POS_ADD = 5;
public static final String[] NAV_DRAWER_TAGS = {
QueueFragment.TAG,
NewEpisodesFragment.TAG,
AllEpisodesFragment.TAG,
DownloadsFragment.TAG,
PlaybackHistoryFragment.TAG,
AddFeedFragment.TAG
};
private Toolbar toolbar;
private ExternalPlayerFragment externalPlayerFragment;
@ -88,7 +94,6 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
private CharSequence drawerTitle;
private CharSequence currentTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getNoTitleTheme());
@ -141,19 +146,6 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
}
});
FragmentTransaction transaction = fm.beginTransaction();
Fragment mainFragment = fm.findFragmentByTag("main");
if (mainFragment != null) {
transaction.replace(R.id.main_view, mainFragment);
} else {
loadFragment(NavListAdapter.VIEW_TYPE_NAV, getLastNavFragment(), null);
}
externalPlayerFragment = new ExternalPlayerFragment();
transaction.replace(R.id.playerFragment, externalPlayerFragment);
transaction.commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
@ -170,19 +162,32 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
}
});
FragmentTransaction transaction = fm.beginTransaction();
Fragment mainFragment = fm.findFragmentByTag("main");
if (mainFragment != null) {
transaction.replace(R.id.main_view, mainFragment);
} else {
loadFragment(getLastNavFragment(), null);
}
externalPlayerFragment = new ExternalPlayerFragment();
transaction.replace(R.id.playerFragment, externalPlayerFragment);
transaction.commit();
checkFirstLaunch();
}
private void saveLastNavFragment(int relPos) {
private void saveLastNavFragment(String tag) {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putInt(PREF_LAST_FRAGMENT, relPos);
edit.putString(PREF_LAST_FRAGMENT, tag);
edit.commit();
}
private int getLastNavFragment() {
private String getLastNavFragment() {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
return prefs.getInt(PREF_LAST_FRAGMENT, POS_QUEUE);
return prefs.getString(PREF_LAST_FRAGMENT, QueueFragment.TAG);
}
private void checkFirstLaunch() {
@ -248,76 +253,83 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
return (navDrawerData != null) ? navDrawerData.feeds : null;
}
private void loadFragment(int viewType, int relPos, Bundle args) {
public void loadFragment(int index, Bundle args) {
int numTags = navAdapter.getTags().size();
if (index <= numTags) {
String tag = navAdapter.getTags().get(index);
loadFragment(tag, args);
} else {
int pos = index - numTags;
loadFeedFragmentByPosition(pos, args);
}
}
public void loadFragment(final String tag, Bundle args) {
Fragment fragment = null;
switch (tag) {
case NewEpisodesFragment.TAG:
fragment = new NewEpisodesFragment();
break;
case QueueFragment.TAG:
fragment = new QueueFragment();
break;
case DownloadsFragment.TAG:
fragment = new DownloadsFragment();
break;
case PlaybackHistoryFragment.TAG:
fragment = new PlaybackHistoryFragment();
break;
case AddFeedFragment.TAG:
fragment = new AddFeedFragment();
break;
}
String title = navAdapter.getLabel(tag);
getSupportActionBar().setTitle(title);
selectedNavListIndex = navAdapter.getTags().indexOf(tag);
if (args != null) {
fragment.setArguments(args);
}
loadFragment(fragment);
}
private void loadFeedFragmentByPosition(int relPos, Bundle args) {
if(relPos < 0) {
return;
}
Feed feed = itemAccess.getItem(relPos);
Fragment fragment = ItemlistFragment.newInstance(feed.getId());
selectedNavListIndex = navAdapter.getSubscriptionOffset() + relPos;
getSupportActionBar().setTitle("");
loadFragment(fragment);
}
public void loadFeedFragmentById(long feedId) {
if (navDrawerData != null) {
int relPos = -1;
for (int i = 0; relPos < 0 && i < navDrawerData.feeds.size(); i++) {
if (navDrawerData.feeds.get(i).getId() == feedId) {
relPos = i;
}
}
loadFeedFragmentByPosition(relPos, null);
}
}
private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
// clear back stack
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
fragmentManager.popBackStack();
}
FragmentTransaction fT = fragmentManager.beginTransaction();
Fragment fragment = null;
if (viewType == NavListAdapter.VIEW_TYPE_NAV) {
switch (relPos) {
case POS_NEW:
fragment = new NewEpisodesFragment();
break;
case POS_ALL_EPISODES:
fragment = new AllEpisodesFragment();
break;
case POS_QUEUE:
fragment = new QueueFragment();
break;
case POS_DOWNLOADS:
fragment = new DownloadsFragment();
break;
case POS_HISTORY:
fragment = new PlaybackHistoryFragment();
break;
case POS_ADD:
fragment = new AddFeedFragment();
break;
}
currentTitle = getString(NavListAdapter.NAV_TITLES[relPos]);
selectedNavListIndex = relPos;
saveLastNavFragment(relPos);
} else if (viewType == NavListAdapter.VIEW_TYPE_SUBSCRIPTION) {
Feed feed = itemAccess.getItem(relPos);
currentTitle = "";
fragment = ItemlistFragment.newInstance(feed.getId());
selectedNavListIndex = NavListAdapter.SUBSCRIPTION_OFFSET + relPos;
}
if (fragment != null) {
if (args != null) {
fragment.setArguments(args);
}
fT.replace(R.id.main_view, fragment, "main");
FragmentTransaction t = fragmentManager.beginTransaction();
t.replace(R.id.main_view, fragment, "main");
fragmentManager.popBackStack();
}
fT.commit();
getSupportActionBar().setTitle(currentTitle);
t.commit();
if (navAdapter != null) {
navAdapter.notifyDataSetChanged();
}
}
public void loadNavFragment(int position, Bundle args) {
loadFragment(NavListAdapter.VIEW_TYPE_NAV, position, args);
}
public void loadFeedFragment(long feedID) {
if (navDrawerData != null) {
for (int i = 0; i < navDrawerData.feeds.size(); i++) {
if (navDrawerData.feeds.get(i).getId() == feedID) {
loadFragment(NavListAdapter.VIEW_TYPE_SUBSCRIPTION, i, null);
break;
}
}
}
}
public void loadChildFragment(Fragment fragment) {
Validate.notNull(fragment);
@ -341,8 +353,7 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int viewType = parent.getAdapter().getItemViewType(position);
if (viewType != NavListAdapter.VIEW_TYPE_SECTION_DIVIDER && position != selectedNavListIndex) {
int relPos = (viewType == NavListAdapter.VIEW_TYPE_NAV) ? position : position - NavListAdapter.SUBSCRIPTION_OFFSET;
loadFragment(viewType, relPos, null);
loadFragment(position, null);
selectedNavListIndex = position;
navAdapter.notifyDataSetChanged();
}
@ -529,10 +540,14 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
private void handleNavIntent() {
Intent intent = getIntent();
if (intent.hasExtra(EXTRA_NAV_INDEX) && intent.hasExtra(EXTRA_NAV_TYPE)) {
int index = intent.getIntExtra(EXTRA_NAV_INDEX, 0);
int type = intent.getIntExtra(EXTRA_NAV_TYPE, NavListAdapter.VIEW_TYPE_NAV);
int index = intent.getIntExtra(EXTRA_NAV_INDEX, -1);
String tag = intent.getStringExtra(EXTRA_FRAGMENT_TAG);
Bundle args = intent.getBundleExtra(EXTRA_FRAGMENT_ARGS);
loadFragment(type, index, args);
if (index >= 0) {
loadFragment(index, args);
} else if (tag != null) {
loadFragment(tag, args);
}
}
setIntent(new Intent(MainActivity.this, MainActivity.class)); // to avoid handling the intent twice when the configuration changes
}

View File

@ -1,9 +1,11 @@
package de.danoeh.antennapod.adapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -13,8 +15,23 @@ import android.widget.TextView;
import com.squareup.picasso.Picasso;
import org.apache.commons.lang3.ArrayUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.fragment.AddFeedFragment;
import de.danoeh.antennapod.fragment.AllEpisodesFragment;
import de.danoeh.antennapod.fragment.DownloadsFragment;
import de.danoeh.antennapod.fragment.NewEpisodesFragment;
import de.danoeh.antennapod.fragment.PlaybackHistoryFragment;
import de.danoeh.antennapod.fragment.QueueFragment;
/**
* BaseAdapter for the navigation drawer
@ -25,47 +42,94 @@ public class NavListAdapter extends BaseAdapter {
public static final int VIEW_TYPE_SECTION_DIVIDER = 1;
public static final int VIEW_TYPE_SUBSCRIPTION = 2;
public static final int[] NAV_TITLES = {
R.string.queue_label,
R.string.new_episodes_label,
R.string.all_episodes_label,
R.string.downloads_label,
R.string.playback_history_label,
R.string.add_feed_label};
private final Drawable[] drawables;
public static final int SUBSCRIPTION_OFFSET = 1 + NAV_TITLES.length;
private static List<String> tags;
private static String[] titles;
private ItemAccess itemAccess;
private Context context;
private SharedPreferences.OnSharedPreferenceChangeListener listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals(UserPreferences.PREF_HIDDEN_DRAWER_ITEMS)) {
loadItems();
}
}
};
public NavListAdapter(ItemAccess itemAccess, Context context) {
this.itemAccess = itemAccess;
this.context = context;
TypedArray ta = context.obtainStyledAttributes(new int[]{
R.attr.stat_playlist,
R.attr.ic_new,
R.attr.feed,
R.attr.av_download,
R.attr.ic_history,
R.attr.content_new});
drawables = new Drawable[]{ta.getDrawable(0), ta.getDrawable(1), ta.getDrawable(2),
ta.getDrawable(3), ta.getDrawable(4), ta.getDrawable(5)};
ta.recycle();
titles = context.getResources().getStringArray(R.array.nav_drawer_titles);
loadItems();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(listener);
}
private void loadItems() {
List<String> newTags = new ArrayList<String>(Arrays.asList(MainActivity.NAV_DRAWER_TAGS));
List<String> hiddenFragments = UserPreferences.getHiddenDrawerItems();
for(String hidden : hiddenFragments) {
newTags.remove(hidden);
}
tags = newTags;
notifyDataSetChanged();
}
public String getLabel(String tag) {
int index = ArrayUtils.indexOf(MainActivity.NAV_DRAWER_TAGS, tag);
return titles[index];
}
private Drawable getDrawable(String tag) {
int icon;
switch (tag) {
case QueueFragment.TAG:
icon = R.attr.stat_playlist;
break;
case NewEpisodesFragment.TAG:
icon = R.attr.ic_new;
break;
case AllEpisodesFragment.TAG:
icon = R.attr.feed;
break;
case DownloadsFragment.TAG:
icon = R.attr.av_download;
break;
case PlaybackHistoryFragment.TAG:
icon = R.attr.ic_history;
break;
case AddFeedFragment.TAG:
icon = R.attr.content_new;
break;
default:
return null;
}
TypedArray ta = context.obtainStyledAttributes(new int[] { icon } );
Drawable result = ta.getDrawable(0);
ta.recycle();
return result;
}
public List<String> getTags() {
return Collections.unmodifiableList(tags);
}
@Override
public int getCount() {
return NAV_TITLES.length + 1 + itemAccess.getCount();
return tags.size() + itemAccess.getCount() + (tags.size() > 0 ? 1 : 0);
}
@Override
public Object getItem(int position) {
int viewType = getItemViewType(position);
if (viewType == VIEW_TYPE_NAV) {
return context.getString(NAV_TITLES[position]);
return getLabel(tags.get(position));
} else if (viewType == VIEW_TYPE_SECTION_DIVIDER) {
return "";
} else {
@ -80,9 +144,9 @@ public class NavListAdapter extends BaseAdapter {
@Override
public int getItemViewType(int position) {
if (0 <= position && position < NAV_TITLES.length) {
if (0 <= position && position < tags.size()) {
return VIEW_TYPE_NAV;
} else if (position < NAV_TITLES.length + 1) {
} else if (position < tags.size() + 1) {
return VIEW_TYPE_SECTION_DIVIDER;
} else {
return VIEW_TYPE_SUBSCRIPTION;
@ -94,6 +158,11 @@ public class NavListAdapter extends BaseAdapter {
return VIEW_TYPE_COUNT;
}
public int getSubscriptionOffset() {
return tags.size() + 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int viewType = getItemViewType(position);
@ -103,7 +172,7 @@ public class NavListAdapter extends BaseAdapter {
} else if (viewType == VIEW_TYPE_SECTION_DIVIDER) {
v = getSectionDividerView(convertView, parent);
} else {
v = getFeedView(position - SUBSCRIPTION_OFFSET, convertView, parent);
v = getFeedView(position - getSubscriptionOffset(), convertView, parent);
}
if (v != null && viewType != VIEW_TYPE_SECTION_DIVIDER) {
TextView txtvTitle = (TextView) v.findViewById(R.id.txtvTitle);
@ -135,7 +204,7 @@ public class NavListAdapter extends BaseAdapter {
holder.title.setText(title);
if (NAV_TITLES[position] == R.string.queue_label) {
if (tags.get(position).equals(QueueFragment.TAG)) {
int queueSize = itemAccess.getQueueSize();
if (queueSize > 0) {
holder.count.setVisibility(View.VISIBLE);
@ -143,7 +212,7 @@ public class NavListAdapter extends BaseAdapter {
} else {
holder.count.setVisibility(View.GONE);
}
} else if (NAV_TITLES[position] == R.string.new_episodes_label) {
} else if (tags.get(position).equals(NewEpisodesFragment.TAG)) {
int unreadItems = itemAccess.getNumberOfUnreadItems();
if (unreadItems > 0) {
holder.count.setVisibility(View.VISIBLE);
@ -155,7 +224,7 @@ public class NavListAdapter extends BaseAdapter {
holder.count.setVisibility(View.GONE);
}
holder.image.setImageDrawable(drawables[position]);
holder.image.setImageDrawable(getDrawable(tags.get(position)));
return convertView;
}

View File

@ -20,7 +20,7 @@ public class DownloadServiceCallbacksImpl implements DownloadServiceCallbacks {
public PendingIntent getNotificationContentIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_NAV_TYPE, NavListAdapter.VIEW_TYPE_NAV);
intent.putExtra(MainActivity.EXTRA_NAV_INDEX, MainActivity.POS_DOWNLOADS);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, DownloadsFragment.TAG);
Bundle args = new Bundle();
args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_RUNNING);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args);
@ -41,7 +41,7 @@ public class DownloadServiceCallbacksImpl implements DownloadServiceCallbacks {
public PendingIntent getReportNotificationContentIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_NAV_TYPE, NavListAdapter.VIEW_TYPE_NAV);
intent.putExtra(MainActivity.EXTRA_NAV_INDEX, MainActivity.POS_DOWNLOADS);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, DownloadsFragment.TAG);
Bundle args = new Bundle();
args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_LOG);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args);

View File

@ -224,7 +224,7 @@ public class ItemlistFragment extends ListFragment {
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
((MainActivity) getActivity()).loadNavFragment(MainActivity.POS_NEW, null);
((MainActivity) getActivity()).loadFragment(NewEpisodesFragment.TAG, null);
}
};
ConfirmationDialog conDialog = new ConfirmationDialog(getActivity(),

View File

@ -122,7 +122,7 @@ public class SearchFragment extends ListFragment {
SearchResult result = (SearchResult) l.getAdapter().getItem(position);
FeedComponent comp = result.getComponent();
if (comp.getClass() == Feed.class) {
((MainActivity) getActivity()).loadFeedFragment(comp.getId());
((MainActivity) getActivity()).loadFeedFragmentById(comp.getId());
} else {
if (comp.getClass() == FeedItem.class) {
FeedItem item = (FeedItem) comp;