Code cleanup

This commit is contained in:
Somethingweirdhere 2018-06-16 13:11:45 +02:00 committed by Christian Schabesberger
parent 6d64215614
commit 072e27ed27
3 changed files with 74 additions and 75 deletions

View File

@ -87,6 +87,16 @@ public class MainActivity extends AppCompatActivity {
private boolean servicesShown = false; private boolean servicesShown = false;
private ImageView serviceArrow; private ImageView serviceArrow;
private static final int ITEM_ID_SUBSCRIPTIONS = - 1;
private static final int ITEM_ID_FEED = - 2;
private static final int ITEM_ID_BOOKMARKS = - 3;
private static final int ITEM_ID_DOWNLOADS = - 4;
private static final int ITEM_ID_HISTORY = - 5;
private static final int ITEM_ID_SETTINGS = 0;
private static final int ITEM_ID_ABOUT = 1;
private static final int ORDER = 0;
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Activity's LifeCycle // Activity's LifeCycle
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@ -136,27 +146,27 @@ public class MainActivity extends AppCompatActivity {
} }
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -1, 0, R.string.tab_subscriptions) .add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER, R.string.tab_subscriptions)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_channel)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_channel));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -2, 0, R.string.fragment_whats_new) .add(R.id.menu_tabs_group, ITEM_ID_FEED, ORDER, R.string.fragment_whats_new)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.rss)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.rss));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -3, 0, R.string.tab_bookmarks) .add(R.id.menu_tabs_group, ITEM_ID_BOOKMARKS, ORDER, R.string.tab_bookmarks)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_bookmark)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_bookmark));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -4, 0, R.string.downloads) .add(R.id.menu_tabs_group, ITEM_ID_DOWNLOADS, ORDER, R.string.downloads)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.download)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.download));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -5, 0, R.string.action_history) .add(R.id.menu_tabs_group, ITEM_ID_HISTORY, ORDER, R.string.action_history)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.history)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.history));
//Settings and About //Settings and About
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_options_about_group, 0, 0, R.string.settings) .add(R.id.menu_options_about_group, ITEM_ID_SETTINGS, ORDER, R.string.settings)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.settings)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.settings));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_options_about_group, 1, 0, R.string.tab_about) .add(R.id.menu_options_about_group, ITEM_ID_ABOUT, ORDER, R.string.tab_about)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.info)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.info));
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close); toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
@ -216,19 +226,19 @@ public class MainActivity extends AppCompatActivity {
private void tabSelected(MenuItem item) throws ExtractionException { private void tabSelected(MenuItem item) throws ExtractionException {
switch(item.getItemId()) { switch(item.getItemId()) {
case -1: case ITEM_ID_SUBSCRIPTIONS:
NavigationHelper.openSubscriptionFragment(getSupportFragmentManager()); NavigationHelper.openSubscriptionFragment(getSupportFragmentManager());
break; break;
case -2: case ITEM_ID_FEED:
NavigationHelper.openWhatsNewFragment(getSupportFragmentManager()); NavigationHelper.openWhatsNewFragment(getSupportFragmentManager());
break; break;
case -3: case ITEM_ID_BOOKMARKS:
NavigationHelper.openBookmarksFragment(getSupportFragmentManager()); NavigationHelper.openBookmarksFragment(getSupportFragmentManager());
break; break;
case -4: case ITEM_ID_DOWNLOADS:
NavigationHelper.openDownloads(this); NavigationHelper.openDownloads(this);
break; break;
case -5: case ITEM_ID_HISTORY:
NavigationHelper.openStatisticFragment(getSupportFragmentManager()); NavigationHelper.openStatisticFragment(getSupportFragmentManager());
break; break;
default: default:
@ -251,10 +261,10 @@ public class MainActivity extends AppCompatActivity {
private void optionsAboutSelected(MenuItem item) { private void optionsAboutSelected(MenuItem item) {
switch(item.getItemId()) { switch(item.getItemId()) {
case 0: case ITEM_ID_SETTINGS:
NavigationHelper.openSettings(this); NavigationHelper.openSettings(this);
break; break;
case 1: case ITEM_ID_ABOUT:
NavigationHelper.openAbout(this); NavigationHelper.openAbout(this);
break; break;
} }
@ -298,7 +308,7 @@ public class MainActivity extends AppCompatActivity {
(ServiceHelper.isBeta(s) ? " (beta)" : ""); (ServiceHelper.isBeta(s) ? " (beta)" : "");
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_services_group, s.getServiceId(), 0, title) .add(R.id.menu_services_group, s.getServiceId(), ORDER, title)
.setIcon(ServiceHelper.getIcon(s.getServiceId())); .setIcon(ServiceHelper.getIcon(s.getServiceId()));
} }
drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true); drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true);
@ -315,33 +325,33 @@ public class MainActivity extends AppCompatActivity {
for (final String ks : service.getKioskList().getAvailableKiosks()) { for (final String ks : service.getKioskList().getAvailableKiosks()) {
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, kioskId, 0, KioskTranslator.getTranslatedKioskName(ks, this)) .add(R.id.menu_tabs_group, kioskId, ORDER, KioskTranslator.getTranslatedKioskName(ks, this))
.setIcon(KioskTranslator.getKioskIcons(ks, this)); .setIcon(KioskTranslator.getKioskIcons(ks, this));
kioskId ++; kioskId ++;
} }
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -1, 0, R.string.tab_subscriptions) .add(R.id.menu_tabs_group, ITEM_ID_SUBSCRIPTIONS, ORDER, R.string.tab_subscriptions)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_channel)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_channel));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -2, 0, R.string.fragment_whats_new) .add(R.id.menu_tabs_group, ITEM_ID_FEED, ORDER, R.string.fragment_whats_new)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.rss)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.rss));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -3, 0, R.string.tab_bookmarks) .add(R.id.menu_tabs_group, ITEM_ID_BOOKMARKS, ORDER, R.string.tab_bookmarks)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_bookmark)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.ic_bookmark));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -4, 0, R.string.downloads) .add(R.id.menu_tabs_group, ITEM_ID_DOWNLOADS, ORDER, R.string.downloads)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.download)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.download));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_tabs_group, -5, 0, R.string.action_history) .add(R.id.menu_tabs_group, ITEM_ID_HISTORY, ORDER, R.string.action_history)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.history)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.history));
//Settings and About //Settings and About
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_options_about_group, 0, 0, R.string.settings) .add(R.id.menu_options_about_group, ITEM_ID_SETTINGS, ORDER, R.string.settings)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.settings)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.settings));
drawerItems.getMenu() drawerItems.getMenu()
.add(R.id.menu_options_about_group, 1, 0, R.string.tab_about) .add(R.id.menu_options_about_group, ITEM_ID_ABOUT, ORDER, R.string.tab_about)
.setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.info)); .setIcon(ThemeHelper.resolveResourceIdFromAttr(this, R.attr.info));
} }

View File

@ -60,6 +60,14 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
private SharedPreferences prefs; private SharedPreferences prefs;
private Bundle savedInstanceStateBundle; private Bundle savedInstanceStateBundle;
private static final String TAB_NUMBER_BLANK = "0";
private static final String TAB_NUMBER_KIOSK = "1";
private static final String TAB_NUMBER_SUBSCIRPTIONS = "2";
private static final String TAB_NUMBER_FEED = "3";
private static final String TAB_NUMBER_BOOKMARKS = "4";
private static final String TAB_NUMBER_HISTORY = "5";
private static final String TAB_NUMBER_CHANNEL = "6";
SharedPreferences.OnSharedPreferenceChangeListener listener = (prefs, key) -> { SharedPreferences.OnSharedPreferenceChangeListener listener = (prefs, key) -> {
if(key.equals("service")||key.equals("saveUsedTabs")) { if(key.equals("service")||key.equals("saveUsedTabs")) {
mainPageChanged(); mainPageChanged();
@ -127,7 +135,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
if((tabs.size()>0)&&activity != null) { if((tabs.size()>0)&&activity != null) {
String tabNumber = tabs.get(0); String tabNumber = tabs.get(0);
if (tabNumber.startsWith("1\t")) { if (tabNumber.startsWith(TAB_NUMBER_KIOSK + "\t")) {
String kiosk[] = tabNumber.split("\t"); String kiosk[] = tabNumber.split("\t");
if (kiosk.length == 2) { if (kiosk.length == 2) {
try { try {
@ -136,7 +144,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
//ignore this. It WILL be thrown while the service is changed. //ignore this. It WILL be thrown while the service is changed.
} }
} }
} else if (tabNumber.startsWith("6\t")) { } else if (tabNumber.startsWith(TAB_NUMBER_CHANNEL + "\t")) {
String channelInfo[] = tabNumber.split("\t"); String channelInfo[] = tabNumber.split("\t");
if(channelInfo.length==4) { if(channelInfo.length==4) {
@ -144,19 +152,19 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
} }
} else { } else {
switch (tabNumber) { switch (tabNumber) {
case "0": case TAB_NUMBER_BLANK:
setTitle(getString(R.string.app_name)); setTitle(getString(R.string.app_name));
break; break;
case "2": case TAB_NUMBER_SUBSCIRPTIONS:
setTitle(getString(R.string.tab_subscriptions)); setTitle(getString(R.string.tab_subscriptions));
break; break;
case "3": case TAB_NUMBER_FEED:
setTitle(getString(R.string.fragment_whats_new)); setTitle(getString(R.string.fragment_whats_new));
break; break;
case "4": case TAB_NUMBER_BOOKMARKS:
setTitle(getString(R.string.tab_bookmarks)); setTitle(getString(R.string.tab_bookmarks));
break; break;
case "5": case TAB_NUMBER_HISTORY:
setTitle(getString(R.string.title_activity_history)); setTitle(getString(R.string.title_activity_history));
break; break;
} }
@ -175,7 +183,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
if (tabToSet != null && c != null) { if (tabToSet != null && c != null) {
if (tabNumber.startsWith("1\t")) { if (tabNumber.startsWith(TAB_NUMBER_KIOSK + "\t")) {
String kiosk[] = tabNumber.split("\t"); String kiosk[] = tabNumber.split("\t");
if (kiosk.length == 2) { if (kiosk.length == 2) {
try { try {
@ -184,23 +192,23 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
//ignore this. It WILL be thrown while the service is changed. //ignore this. It WILL be thrown while the service is changed.
} }
} }
} else if (tabNumber.startsWith("6\t")) { } else if (tabNumber.startsWith(TAB_NUMBER_CHANNEL + "\t")) {
tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_channel)); tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_channel));
} else { } else {
switch (tabNumber) { switch (tabNumber) {
case "0": case TAB_NUMBER_BLANK:
tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_hot)); tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_hot));
break; break;
case "2": case TAB_NUMBER_SUBSCIRPTIONS:
tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_channel)); tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_channel));
break; break;
case "3": case TAB_NUMBER_FEED:
tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.rss)); tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.rss));
break; break;
case "4": case TAB_NUMBER_BOOKMARKS:
tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_bookmark)); tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.ic_bookmark));
break; break;
case "5": case TAB_NUMBER_HISTORY:
tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.history)); tabToSet.setIcon(ThemeHelper.resolveResourceIdFromAttr(getContext(), R.attr.history));
break; break;
} }
@ -231,7 +239,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
} }
for(String tabNumber:tabsArray) { for(String tabNumber:tabsArray) {
if(tabNumber.equals("1")) { if(tabNumber.equals(TAB_NUMBER_KIOSK)) {
if (kl != null) { if (kl != null) {
for(String ks : kl.getAvailableKiosks()) { for(String ks : kl.getAvailableKiosks()) {
tabs.add(tabNumber+"\t"+ks); tabs.add(tabNumber+"\t"+ks);
@ -302,7 +310,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
public Fragment getItem(int position) { public Fragment getItem(int position) {
String tabNumber = tabs.get(position); String tabNumber = tabs.get(position);
if(tabNumber.startsWith("1\t")) { if(tabNumber.startsWith(TAB_NUMBER_KIOSK + "\t")) {
String kiosk[] = tabNumber.split("\t"); String kiosk[] = tabNumber.split("\t");
if(kiosk.length==2) { if(kiosk.length==2) {
KioskFragment fragment = null; KioskFragment fragment = null;
@ -318,7 +326,7 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
"none", "", R.string.app_ui_crash)); "none", "", R.string.app_ui_crash));
} }
} }
} else if(tabNumber.startsWith("6\t")) { } else if(tabNumber.startsWith(TAB_NUMBER_CHANNEL + "\t")) {
String channelInfo[] = tabNumber.split("\t"); String channelInfo[] = tabNumber.split("\t");
if(channelInfo.length==4) { if(channelInfo.length==4) {
ChannelFragment fragment = ChannelFragment.getInstance(Integer.parseInt(channelInfo[3]), channelInfo[1], channelInfo[2]); ChannelFragment fragment = ChannelFragment.getInstance(Integer.parseInt(channelInfo[3]), channelInfo[1], channelInfo[2]);
@ -329,21 +337,21 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
} }
} else { } else {
switch (tabNumber) { switch (tabNumber) {
case "0": case TAB_NUMBER_BLANK:
return new BlankFragment(); return new BlankFragment();
case "2": case TAB_NUMBER_SUBSCIRPTIONS:
SubscriptionFragment sFragment = new SubscriptionFragment(); SubscriptionFragment sFragment = new SubscriptionFragment();
sFragment.useAsFrontPage(true); sFragment.useAsFrontPage(true);
return sFragment; return sFragment;
case "3": case TAB_NUMBER_FEED:
FeedFragment fFragment = new FeedFragment(); FeedFragment fFragment = new FeedFragment();
fFragment.useAsFrontPage(true); fFragment.useAsFrontPage(true);
return fFragment; return fFragment;
case "4": case TAB_NUMBER_BOOKMARKS:
BookmarkFragment bFragment = new BookmarkFragment(); BookmarkFragment bFragment = new BookmarkFragment();
bFragment.useAsFrontPage(true); bFragment.useAsFrontPage(true);
return bFragment; return bFragment;
case "5": case TAB_NUMBER_HISTORY:
StatisticsPlaylistFragment cFragment = new StatisticsPlaylistFragment(); StatisticsPlaylistFragment cFragment = new StatisticsPlaylistFragment();
cFragment.useAsFrontPage(true); cFragment.useAsFrontPage(true);
return cFragment; return cFragment;

View File

@ -242,14 +242,7 @@ public class SubscriptionFragment extends BaseStateFragment<List<SubscriptionEnt
} }
private void onImportFromServiceSelected(int serviceId) { private void onImportFromServiceSelected(int serviceId) {
FragmentManager fragmentManager; FragmentManager fragmentManager = getFM();
if (getParentFragment() == null)
{
fragmentManager = getFragmentManager();
} else {
fragmentManager = getParentFragment().getFragmentManager();
}
NavigationHelper.openSubscriptionsImportFragment(fragmentManager, serviceId); NavigationHelper.openSubscriptionsImportFragment(fragmentManager, serviceId);
} }
@ -328,39 +321,27 @@ public class SubscriptionFragment extends BaseStateFragment<List<SubscriptionEnt
infoListAdapter.setOnChannelSelectedListener(new OnClickGesture<ChannelInfoItem>() { infoListAdapter.setOnChannelSelectedListener(new OnClickGesture<ChannelInfoItem>() {
@Override @Override
public void selected(ChannelInfoItem selectedItem) { public void selected(ChannelInfoItem selectedItem) {
try { FragmentManager fragmentManager = getFM();
final FragmentManager fragmentManager = getParentFragment() == null
? getFragmentManager()
: getParentFragment().getFragmentManager();
// Requires the parent fragment to find holder for fragment replacement
NavigationHelper.openChannelFragment(fragmentManager, NavigationHelper.openChannelFragment(fragmentManager,
selectedItem.getServiceId(), selectedItem.getServiceId(), selectedItem.getUrl(), selectedItem.getName());
selectedItem.getUrl(),
selectedItem.getName());
} catch (Exception e) {
ErrorActivity.reportUiError((AppCompatActivity) getActivity(), e);
}
} }
}); });
//noinspection ConstantConditions //noinspection ConstantConditions
whatsNewItemListHeader.setOnClickListener(v -> whatsNewItemListHeader.setOnClickListener(v ->
{ {
FragmentManager fragmentManager; FragmentManager fragmentManager = getFM();
if (getParentFragment() == null)
{
fragmentManager = getFragmentManager();
} else {
fragmentManager = getParentFragment().getFragmentManager();
}
NavigationHelper.openWhatsNewFragment(fragmentManager); NavigationHelper.openWhatsNewFragment(fragmentManager);
}); });
importExportListHeader.setOnClickListener(v -> importExportOptions.switchState()); importExportListHeader.setOnClickListener(v -> importExportOptions.switchState());
} }
private FragmentManager getFM() {
return getParentFragment() == null
? getFragmentManager()
: getParentFragment().getFragmentManager();
}
private void resetFragment() { private void resetFragment() {
if (disposables != null) disposables.clear(); if (disposables != null) disposables.clear();
if (infoListAdapter != null) infoListAdapter.clearStreamItemList(); if (infoListAdapter != null) infoListAdapter.clearStreamItemList();