Refactor ExploreTabFragment

This commit is contained in:
Alex Sytnyk 2018-04-25 19:07:44 +03:00
parent 607a32ad46
commit e17cf751a7

View file

@ -23,7 +23,6 @@ import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard;
import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard;
import net.osmand.plus.wikivoyage.explore.travelcards.TravelDownloadUpdateCard; import net.osmand.plus.wikivoyage.explore.travelcards.TravelDownloadUpdateCard;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -33,8 +32,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
private ExploreRvAdapter adapter = new ExploreRvAdapter(); private ExploreRvAdapter adapter = new ExploreRvAdapter();
private AddDownloadUpdateCardTask addDownloadUpdateCardTask;
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -49,19 +46,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
return mainView; return mainView;
} }
@Override
public void onDestroyView() {
cancelAddDownloadUpdateCardTask();
super.onDestroyView();
}
private void cancelAddDownloadUpdateCardTask() {
if (addDownloadUpdateCardTask != null) {
addDownloadUpdateCardTask.cancel(true);
addDownloadUpdateCardTask = null;
}
}
private List<Object> generateItems() { private List<Object> generateItems() {
final List<Object> items = new ArrayList<>(); final List<Object> items = new ArrayList<>();
final OsmandApplication app = getMyApplication(); final OsmandApplication app = getMyApplication();
@ -75,9 +59,19 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
return items; return items;
} }
private void addDownloadUpdateCard(boolean nightMode) { private void addDownloadUpdateCard(final boolean nightMode) {
addDownloadUpdateCardTask = new AddDownloadUpdateCardTask(getMyApplication(), adapter, nightMode); final OsmandApplication app = getMyApplication();
addDownloadUpdateCardTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); new CheckWorldWikivoyageTask(app, new CheckWorldWikivoyageTask.Callback() {
@Override
public void onCheckFinished(boolean worldWikivoyageDownloaded) {
if (!worldWikivoyageDownloaded && adapter != null) {
TravelDownloadUpdateCard card = new TravelDownloadUpdateCard(app, nightMode, true);
if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) {
adapter.notifyDataSetChanged();
}
}
}
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
private void addPopularDestinations(@NonNull List<Object> items, boolean nightMode) { private void addPopularDestinations(@NonNull List<Object> items, boolean nightMode) {
@ -91,23 +85,19 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
} }
} }
private static class AddDownloadUpdateCardTask extends AsyncTask<Void, Void, TravelDownloadUpdateCard> { private static class CheckWorldWikivoyageTask extends AsyncTask<Void, Void, Boolean> {
private OsmandApplication app; private OsmandApplication app;
private WeakReference<ExploreRvAdapter> adapterWr; private Callback callback;
private boolean nightMode; CheckWorldWikivoyageTask(OsmandApplication app, Callback callback) {
AddDownloadUpdateCardTask(OsmandApplication app, ExploreRvAdapter adapter, boolean nightMode) {
this.app = app; this.app = app;
this.adapterWr = new WeakReference<>(adapter); this.callback = callback;
this.nightMode = nightMode;
} }
@Override @Override
protected TravelDownloadUpdateCard doInBackground(Void... voids) { protected Boolean doInBackground(Void... voids) {
final boolean[] worldWikivoyageDownloaded = new boolean[1]; final boolean[] worldWikivoyageDownloaded = new boolean[1];
new LocalIndexHelper(app).getLocalTravelFiles(new AbstractLoadLocalIndexTask() { new LocalIndexHelper(app).getLocalTravelFiles(new AbstractLoadLocalIndexTask() {
@Override @Override
public void loadFile(LocalIndexInfo... loaded) { public void loadFile(LocalIndexInfo... loaded) {
@ -118,25 +108,19 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
} }
} }
}); });
return worldWikivoyageDownloaded[0];
if (!worldWikivoyageDownloaded[0] && !isCancelled()) {
TravelDownloadUpdateCard card = new TravelDownloadUpdateCard(app, nightMode, true);
return card;
}
return null;
} }
@Override @Override
protected void onPostExecute(TravelDownloadUpdateCard card) { protected void onPostExecute(Boolean worldWikivoyageDownloaded) {
if (!isCancelled() && card != null) { if (callback != null) {
ExploreRvAdapter adapter = adapterWr.get(); callback.onCheckFinished(worldWikivoyageDownloaded);
if (adapter != null) {
if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) {
adapter.notifyItemInserted(DOWNLOAD_UPDATE_CARD_POSITION);
}
} }
callback = null;
} }
interface Callback {
void onCheckFinished(boolean worldWikivoyageDownloaded);
} }
} }
} }