Fix some crashes

This commit is contained in:
inorichi 2015-12-17 15:33:15 +01:00
parent 42cd6ea007
commit c6dc5d344d
4 changed files with 30 additions and 15 deletions

View File

@ -89,19 +89,19 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter> {
CatalogueSpinnerAdapter spinnerAdapter = new CatalogueSpinnerAdapter(themedContext,
android.R.layout.simple_spinner_item, getPresenter().getEnabledSources());
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if (savedState == null) selectedIndex = spinnerAdapter.getEmptyIndex();
spinner.setAdapter(spinnerAdapter);
spinner.setSelection(savedState == null ? spinnerAdapter.getEmptyIndex() : selectedIndex);
spinner.setSelection(selectedIndex);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Source source = spinnerAdapter.getItem(position);
// We add an empty source with id -1 that acts as a placeholder to show a hint
// that asks to select a source
if (source.getId() != -1 && selectedIndex != position) {
if (source.getId() != -1 && (selectedIndex != position || adapter.isEmpty())) {
// Set previous selection if it's not a valid source and notify the user
if (!getPresenter().isValidSource(source)) {
spinner.setSelection(selectedIndex != -1 ? selectedIndex :
spinnerAdapter.getEmptyIndex());
spinner.setSelection(spinnerAdapter.getEmptyIndex());
ToastUtil.showShort(getActivity(), R.string.source_requires_login);
} else {
selectedIndex = position;

View File

@ -50,6 +50,10 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
if (savedState != null) {
onProcessRestart();
}
mangaDetailSubject = PublishSubject.create();
restartableReplay(GET_MANGA_LIST,
@ -80,6 +84,11 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
(view, error) -> Timber.e(error.getMessage()));
}
private void onProcessRestart() {
stop(GET_MANGA_LIST);
stop(GET_MANGA_DETAIL);
}
@Override
protected void onDestroy() {
// Catalogue covers are probably not going to be needed for a long time

View File

@ -181,7 +181,8 @@ public class ChaptersFragment extends BaseRxFragment<ChaptersPresenter> implemen
private void observeChapterDownloadProgress() {
downloadProgressSubscription = getPresenter().getDownloadProgressObs()
.subscribe(this::onDownloadProgressChange);
.subscribe(this::onDownloadProgressChange,
error -> { /* TODO getting a NPE sometimes on 'manga' from presenter */ });
}
private void unsubscribeChapterDownloadProgress() {

View File

@ -78,9 +78,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
if (currentPage != 0)
view.setSelectedPage(currentPage);
},
(view, error) -> {
view.onChapterError();
});
(view, error) -> view.onChapterError());
restartableReplay(GET_PAGE_IMAGES,
() -> getPageImagesObservable()
@ -104,10 +102,8 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
@Override
protected void onDestroy() {
unregisterForEvents();
if (pageList != null && isChapterFinished()) {
updateMangaSyncLastChapterRead();
}
onChapterLeft();
updateMangaSyncLastChapterRead();
super.onDestroy();
}
@ -241,6 +237,9 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
}
private void updateMangaSyncLastChapterRead() {
if (pageList == null)
return;
db.getMangaSync(manga).createObservable()
.take(1)
.flatMap(Observable::from)
@ -249,15 +248,21 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
if (!service.isLogged())
return;
int lastChapterReadLocal = (int) Math.floor(chapter.chapter_number);
int lastChapterReadRemote = mangaSync.last_chapter_read;
int lastChapterReadLocal = 0;
// If the current chapter has been read, we check with this one
if (chapter.read)
lastChapterReadLocal = (int) Math.floor(chapter.chapter_number);
// If not, we check if the previous chapter has been read
else if (previousChapter != null && previousChapter.read)
lastChapterReadLocal = (int) Math.floor(previousChapter.chapter_number);
if (lastChapterReadLocal > lastChapterReadRemote) {
if (lastChapterReadLocal > mangaSync.last_chapter_read) {
mangaSync.last_chapter_read = lastChapterReadLocal;
UpdateMangaSyncService.start(getContext(), mangaSync);
}
})
.subscribe();
.subscribe(next -> {},
error -> Timber.e(error.getCause(), error.getMessage()));
}
public void setCurrentPage(int currentPage) {