From 8ec5b17599f13cadbdb4638c346c3a69b58b90c0 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Sat, 28 Apr 2018 19:16:58 +0300 Subject: [PATCH] Format and refactor code --- .../fragment_wikivoyage_explore_dialog.xml | 12 ++--- OsmAnd/res/layout/wikivoyage_list_header.xml | 1 - .../plus/wikivoyage/data/TravelDbHelper.java | 4 +- .../explore/ExploreTabFragment.java | 45 +++++++++---------- .../WikivoyageExploreDialogFragment.java | 28 +++++++----- .../explore/travelcards/HeaderTravelCard.java | 3 -- 6 files changed, 46 insertions(+), 47 deletions(-) diff --git a/OsmAnd/res/layout/fragment_wikivoyage_explore_dialog.xml b/OsmAnd/res/layout/fragment_wikivoyage_explore_dialog.xml index 4f7fd59209..4c834efca3 100644 --- a/OsmAnd/res/layout/fragment_wikivoyage_explore_dialog.xml +++ b/OsmAnd/res/layout/fragment_wikivoyage_explore_dialog.xml @@ -61,18 +61,18 @@ + - - - + + diff --git a/OsmAnd/res/layout/wikivoyage_list_header.xml b/OsmAnd/res/layout/wikivoyage_list_header.xml index f4114a7d16..eadb03d8cf 100644 --- a/OsmAnd/res/layout/wikivoyage_list_header.xml +++ b/OsmAnd/res/layout/wikivoyage_list_header.xml @@ -46,5 +46,4 @@ - diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java index 6320fc652c..f354d27e42 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java @@ -121,7 +121,7 @@ public class TravelDbHelper { selectedTravelBook = null; } } - + public void loadDataForSelectedTravelBook() { localDataHelper.refreshCachedData(); loadPopularArticles(); @@ -212,7 +212,7 @@ public class TravelDbHelper { public List getPopularArticles() { return popularArticles; } - + @NonNull public List loadPopularArticles() { List res = new ArrayList<>(); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java index ffbf8d57a0..3f6b7f6ded 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java @@ -1,19 +1,20 @@ package net.osmand.plus.wikivoyage.explore; -import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; + import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.Version; -import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.download.DownloadValidationManager; @@ -28,7 +29,6 @@ import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.TravelDownloadUpdateCard; import java.io.File; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; @@ -63,8 +63,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn return mainView; } - - public void populateData() { final List items = new ArrayList<>(); final OsmandApplication app = getMyApplication(); @@ -74,18 +72,20 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn if (app.getTravelDbHelper().getSelectedTravelBook() != null) { items.add(new HeaderTravelCard(app, nightMode, getString(R.string.popular_destinations))); List popularArticles = app.getTravelDbHelper().getPopularArticles(); - for (TravelArticle article : popularArticles) { - items.add(new ArticleTravelCard(getMyApplication(), nightMode, article, - getActivity().getSupportFragmentManager())); + FragmentActivity activity = getActivity(); + if (activity != null) { + for (TravelArticle article : popularArticles) { + items.add(new ArticleTravelCard(getMyApplication(), nightMode, article, + activity.getSupportFragmentManager())); + } } - } items.add(startEditingTravelCard); adapter.setItems(items); - + checkToAddDownloadTravelCard(); } - + private void checkToAddDownloadTravelCard() { final OsmandApplication app = getMyApplication(); final DownloadIndexesThread downloadThread = app.getDownloadThread(); @@ -100,8 +100,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn addDownloadUpdateCard(loadingInProgress); } } - - @Override public void newDownloadIndexes() { @@ -109,7 +107,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn downloadIndexesRequested = false; indexItem = getMyApplication().getDownloadThread().getIndexes() .getWikivoyageItem(getWikivoyageFileName()); - if(downloadUpdateCard == null) { + if (downloadUpdateCard == null) { addDownloadUpdateCard(false); } } @@ -131,13 +129,18 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn @Override public void downloadHasFinished() { - File targetFile = indexItem.getTargetFile(getMyApplication()); + final OsmandApplication app = getMyApplication(); + File targetFile = indexItem.getTargetFile(app); if (downloadUpdateCard != null && indexItem != null && targetFile.exists()) { downloadUpdateCard.setLoadingInProgress(false); removeDownloadUpdateCard(); - getMyApplication().getTravelDbHelper().initTravelBooks(); - getMyApplication().getTravelDbHelper().selectTravelBook(targetFile); - ((WikivoyageExploreDialogFragment)getParentFragment()).populateData(); + TravelDbHelper travelDbHelper = app.getTravelDbHelper(); + travelDbHelper.initTravelBooks(); + travelDbHelper.selectTravelBook(targetFile); + Fragment parent = getParentFragment(); + if (parent != null && parent instanceof WikivoyageExploreDialogFragment) { + ((WikivoyageExploreDialogFragment) parent).populateData(); + } } } @@ -188,14 +191,10 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn downloadUpdateCard = null; } - - - private void addOpenBetaTravelCard(List items, final boolean nightMode) { final OsmandApplication app = getMyApplication(); if (!Version.isPaidVersion(app)) { items.add(new OpenBetaTravelCard(app, nightMode, getFragmentManager())); } } - -} \ No newline at end of file +} diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java index 94f49df409..9edf933371 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java @@ -19,6 +19,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; + import net.osmand.AndroidUtils; import net.osmand.PicassoUtils; import net.osmand.plus.LockableViewPager; @@ -58,7 +59,6 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - FragmentManager childFm = getChildFragmentManager(); List fragments = childFm.getFragments(); if (fragments != null) { @@ -136,37 +136,41 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen return false; } }); + updateSearchVisibility(); populateData(); + return mainView; } - + protected void onDataLoaded() { mainView.findViewById(R.id.progress_bar).setVisibility(View.GONE); updateSearchVisibility(); - if(exploreTabFragment != null) { + if (exploreTabFragment != null) { exploreTabFragment.populateData(); } - if(savedArticlesTabFragment != null) { + if (savedArticlesTabFragment != null) { savedArticlesTabFragment.savedArticlesUpdated(); } } private void updateSearchVisibility() { - mainView.findViewById(R.id.search_box).setVisibility(getMyApplication().getTravelDbHelper().getSelectedTravelBook() == null ? View.GONE : View.VISIBLE); + mainView.findViewById(R.id.search_box).setVisibility( + getMyApplication().getTravelDbHelper().getSelectedTravelBook() == null ? View.GONE : View.VISIBLE + ); } public void populateData() { mainView.findViewById(R.id.progress_bar).setVisibility(View.VISIBLE); new LoadWikivoyageData(this).execute(); } - + private static class LoadWikivoyageData extends AsyncTask { - + private WeakReference weakReference; private TravelDbHelper travelDbHelper; - public LoadWikivoyageData(WikivoyageExploreDialogFragment fragment) { + LoadWikivoyageData(WikivoyageExploreDialogFragment fragment) { travelDbHelper = fragment.getMyApplication().getTravelDbHelper(); weakReference = new WeakReference(fragment); } @@ -176,15 +180,16 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen travelDbHelper.loadDataForSelectedTravelBook(); return null; } + @Override protected void onPostExecute(Void result) { WikivoyageExploreDialogFragment option = weakReference.get(); - if(option != null && option.isResumed()) { + if (option != null && option.isResumed()) { option.onDataLoaded(); } } - - }; + + } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -254,5 +259,4 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen return fragments.size(); } } - } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/HeaderTravelCard.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/HeaderTravelCard.java index bf43e6cbf3..ec25a2904e 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/HeaderTravelCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/HeaderTravelCard.java @@ -3,7 +3,6 @@ package net.osmand.plus.wikivoyage.explore.travelcards; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.View; -import android.widget.ProgressBar; import android.widget.TextView; import net.osmand.plus.OsmandApplication; @@ -16,7 +15,6 @@ public class HeaderTravelCard extends BaseTravelCard { private int articleItemCount; private String title; - public HeaderTravelCard(OsmandApplication app, boolean nightMode, String title) { super(app, nightMode); this.title = title; @@ -57,4 +55,3 @@ public class HeaderTravelCard extends BaseTravelCard { return TYPE; } } -