Refactor ExploreTabFragment

This commit is contained in:
Alex Sytnyk 2018-05-03 16:17:18 +03:00
parent 9fb6c8a215
commit 50dccc380a

View file

@ -39,14 +39,12 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
private static final String WORLD_WIKIVOYAGE_FILE_NAME = "World_wikivoyage.sqlite"; private static final String WORLD_WIKIVOYAGE_FILE_NAME = "World_wikivoyage.sqlite";
private ExploreRvAdapter adapter = new ExploreRvAdapter(); private ExploreRvAdapter adapter = new ExploreRvAdapter();
private boolean nightMode;
private TravelDownloadUpdateCard downloadUpdateCard; private TravelDownloadUpdateCard downloadUpdateCard;
private boolean nightMode; private IndexItem mainIndexItem;
private boolean waitForIndexes;
private IndexItem indexItem;
private boolean downloadIndexesRequested;
@Nullable @Nullable
@Override @Override
@ -64,35 +62,32 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
@Override @Override
public void newDownloadIndexes() { public void newDownloadIndexes() {
if (downloadIndexesRequested) { if (waitForIndexes) {
downloadIndexesRequested = false; waitForIndexes = false;
indexItem = getMyApplication().getDownloadThread().getIndexes() checkDownloadIndexes();
.getWikivoyageItem(getWikivoyageFileName());
if (downloadUpdateCard == null) {
addDownloadUpdateCard(false);
}
} }
} }
@Override @Override
public void downloadInProgress() { public void downloadInProgress() {
DownloadIndexesThread downloadThread = getMyApplication().getDownloadThread(); if (mainIndexItem != null) {
IndexItem current = downloadThread.getCurrentDownloadingItem(); DownloadIndexesThread downloadThread = getMyApplication().getDownloadThread();
indexItem = downloadThread.getIndexes().getWikivoyageItem(getWikivoyageFileName()); IndexItem current = downloadThread.getCurrentDownloadingItem();
if (current != null && downloadUpdateCard != null if (downloadUpdateCard != null
&& indexItem != null && current != null
&& current == indexItem && current == mainIndexItem
&& (!current.isDownloaded() || current.isOutdated())) { && (!current.isDownloaded() || current.isOutdated())) {
downloadUpdateCard.setProgress(downloadThread.getCurrentDownloadingItemProgress()); downloadUpdateCard.setProgress(downloadThread.getCurrentDownloadingItemProgress());
adapter.updateDownloadUpdateCard(); adapter.updateDownloadUpdateCard();
}
} }
} }
@Override @Override
public void downloadHasFinished() { public void downloadHasFinished() {
final OsmandApplication app = getMyApplication(); final OsmandApplication app = getMyApplication();
File targetFile = indexItem.getTargetFile(app); File targetFile = mainIndexItem.getTargetFile(app);
if (downloadUpdateCard != null && indexItem != null && targetFile.exists()) { if (downloadUpdateCard != null && mainIndexItem != null && targetFile.exists()) {
downloadUpdateCard.setLoadingInProgress(false); downloadUpdateCard.setLoadingInProgress(false);
removeDownloadUpdateCard(); removeDownloadUpdateCard();
TravelDbHelper travelDbHelper = app.getTravelDbHelper(); TravelDbHelper travelDbHelper = app.getTravelDbHelper();
@ -133,43 +128,46 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
items.add(new StartEditingTravelCard(app, nightMode)); items.add(new StartEditingTravelCard(app, nightMode));
adapter.setItems(items); adapter.setItems(items);
checkToAddDownloadTravelCard();
}
private void checkToAddDownloadTravelCard() {
final OsmandApplication app = getMyApplication();
final DownloadIndexesThread downloadThread = app.getDownloadThread(); final DownloadIndexesThread downloadThread = app.getDownloadThread();
if (!downloadThread.getIndexes().isDownloadedFromInternet) { if (!downloadThread.getIndexes().isDownloadedFromInternet) {
downloadIndexesRequested = true; waitForIndexes = true;
downloadThread.runReloadIndexFilesSilent(); downloadThread.runReloadIndexFilesSilent();
} else { } else {
indexItem = downloadThread.getIndexes().getWikivoyageItem(getWikivoyageFileName()); checkDownloadIndexes();
IndexItem current = downloadThread.getCurrentDownloadingItem();
boolean loadingInProgress = current != null && indexItem != null && current == indexItem;
addDownloadUpdateCard(loadingInProgress);
} }
} }
private void addDownloadUpdateCard(boolean loadingInProgress) { private void checkDownloadIndexes() {
final OsmandApplication app = getMyApplication(); DownloadIndexesThread downloadThread = getMyApplication().getDownloadThread();
mainIndexItem = downloadThread.getIndexes().getWikivoyageItem(getWikivoyageFileName());
addDownloadUpdateCard();
}
boolean outdated = indexItem != null && indexItem.isOutdated(); private void addDownloadUpdateCard() {
final OsmandApplication app = getMyApplication();
final DownloadIndexesThread downloadThread = app.getDownloadThread();
boolean outdated = mainIndexItem != null && mainIndexItem.isOutdated();
File selectedTravelBook = app.getTravelDbHelper().getSelectedTravelBook(); File selectedTravelBook = app.getTravelDbHelper().getSelectedTravelBook();
if (selectedTravelBook == null || outdated) { if (selectedTravelBook == null || outdated) {
IndexItem current = downloadThread.getCurrentDownloadingItem();
boolean loadingInProgress = mainIndexItem != null && current != null && mainIndexItem == current;
boolean showOtherMaps = false; boolean showOtherMaps = false;
if (selectedTravelBook == null) { if (selectedTravelBook == null) {
List<IndexItem> items = app.getDownloadThread().getIndexes().getWikivoyageItems(); List<IndexItem> items = downloadThread.getIndexes().getWikivoyageItems();
showOtherMaps = items != null && items.size() > 1; showOtherMaps = items != null && items.size() > 1;
} }
downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated); downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated);
downloadUpdateCard.setShowOtherMapsBtn(showOtherMaps); downloadUpdateCard.setShowOtherMapsBtn(showOtherMaps);
downloadUpdateCard.setLoadingInProgress(loadingInProgress); downloadUpdateCard.setLoadingInProgress(loadingInProgress);
downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() { downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() {
@Override @Override
public void onPrimaryButtonClick() { public void onPrimaryButtonClick() {
if (indexItem != null) { if (mainIndexItem != null) {
new DownloadValidationManager(app).startDownload(getMyActivity(), indexItem); new DownloadValidationManager(app).startDownload(getMyActivity(), mainIndexItem);
downloadUpdateCard.setLoadingInProgress(true); downloadUpdateCard.setLoadingInProgress(true);
adapter.updateDownloadUpdateCard(); adapter.updateDownloadUpdateCard();
} }
@ -178,7 +176,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
@Override @Override
public void onSecondaryButtonClick() { public void onSecondaryButtonClick() {
if (downloadUpdateCard.isLoadingInProgress()) { if (downloadUpdateCard.isLoadingInProgress()) {
app.getDownloadThread().cancelDownload(indexItem); downloadThread.cancelDownload(mainIndexItem);
downloadUpdateCard.setLoadingInProgress(false); downloadUpdateCard.setLoadingInProgress(false);
adapter.updateDownloadUpdateCard(); adapter.updateDownloadUpdateCard();
} else if (!downloadUpdateCard.isDownload()) { } else if (!downloadUpdateCard.isDownload()) {
@ -194,7 +192,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
} }
} }
}); });
downloadUpdateCard.setIndexItem(indexItem); downloadUpdateCard.setIndexItem(mainIndexItem);
adapter.setDownloadUpdateCard(downloadUpdateCard); adapter.setDownloadUpdateCard(downloadUpdateCard);
} }
} }