Add BaseContract

This commit is contained in:
Abdul Wadood 2018-05-30 03:38:57 +05:30 committed by Isaac Hutt
parent 0691ac5e5b
commit 05ca65f525
13 changed files with 56 additions and 100 deletions

View File

@ -0,0 +1,28 @@
package org.kiwix.kiwixmobile.base;
public class BaseContract {
/**
* The contract for a view must extend this interface.
*
* @param <T> the type of presenter associated with the view
*/
public interface View<T> {
}
public interface Presenter<T> {
/**
* Binds presenter with a view when resumed. The Presenter will perform initialization here.
*
* @param view the view associated with this presenter
*/
void attachView(T view);
/**
* Drops the reference to the view when destroyed
*/
void detachView();
}
}

View File

@ -18,40 +18,20 @@
package org.kiwix.kiwixmobile.base;
/**
* Created by EladKeyshawn on 05/04/2017.
* All presenters should inherit from this presenter.
*/
public class BasePresenter<T extends ViewCallback> implements Presenter<T> {
public abstract class BasePresenter<T extends BaseContract.View> implements BaseContract.Presenter<T> {
private T mMvpView;
protected T view;
@Override
public void attachView(T mvpView) {
this.mMvpView = mvpView;
public void attachView(T view) {
this.view = view;
}
@Override
public void detachView() {
mMvpView = null;
view = null;
}
public boolean isViewAttached() {
return mMvpView != null;
}
public T getMvpView() {
return mMvpView;
}
public void checkViewAttached() {
if (!isViewAttached()) throw new MvpViewNotAttachedException();
}
public static class MvpViewNotAttachedException extends RuntimeException {
public MvpViewNotAttachedException() {
super("Please call Presenter.attachView(MvpView) before" +
" requesting data to the Presenter");
}
}
}
}

View File

@ -1,28 +0,0 @@
/*
* Kiwix Android
* Copyright (C) 2018 Kiwix <android.kiwix.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kiwix.kiwixmobile.base;
/**
* Created by EladKeyshawn on 05/04/2017.
*/
public interface Presenter<V extends ViewCallback> {
void attachView(V mvpView);
void detachView();
}

View File

@ -1,24 +0,0 @@
/*
* Kiwix Android
* Copyright (C) 2018 Kiwix <android.kiwix.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kiwix.kiwixmobile.base;
/**
* Created by EladKeyshawn on 05/04/2017.
*/
public interface ViewCallback {
}

View File

@ -34,13 +34,13 @@ public class BookmarksPresenter extends BasePresenter<BookmarksViewCallback> {
BookmarksDao bookmarksDao;
@Inject
public BookmarksPresenter() {
BookmarksPresenter() {
}
public void loadBookmarks() {
ArrayList<String> bookmarks = bookmarksDao.getBookmarkTitles(ZimContentProvider.getId(), ZimContentProvider.getName());
ArrayList<String> bookmarkUrls = bookmarksDao.getBookmarks(ZimContentProvider.getId(), ZimContentProvider.getName());
getMvpView().showBookmarks(bookmarks, bookmarkUrls);
view.showBookmarks(bookmarks, bookmarkUrls);
}

View File

@ -17,7 +17,7 @@
*/
package org.kiwix.kiwixmobile.bookmark;
import org.kiwix.kiwixmobile.base.ViewCallback;
import org.kiwix.kiwixmobile.base.BaseContract;
import java.util.ArrayList;
@ -25,7 +25,7 @@ import java.util.ArrayList;
* Created by EladKeyshawn on 05/04/2017.
*/
public interface BookmarksViewCallback extends ViewCallback {
public interface BookmarksViewCallback extends BaseContract.View {
void showBookmarks(ArrayList<String> bookmarks, ArrayList<String> bookmarkUrls);
void popDeleteBookmarksSnackbar();

View File

@ -32,7 +32,7 @@ public class SearchPresenter extends BasePresenter<SearchViewCallback> {
RecentSearchDao recentSearchDao;
@Inject
public SearchPresenter() {}
SearchPresenter() {}
@Override
public void attachView(SearchViewCallback searchViewCallback) {
@ -40,7 +40,7 @@ public class SearchPresenter extends BasePresenter<SearchViewCallback> {
}
void getRecentSearches() {
getMvpView().addRecentSearches(recentSearchDao.getRecentSearches());
view.addRecentSearches(recentSearchDao.getRecentSearches());
}
void saveSearch(String title) {

View File

@ -17,7 +17,7 @@
*/
package org.kiwix.kiwixmobile.search;
import org.kiwix.kiwixmobile.base.ViewCallback;
import org.kiwix.kiwixmobile.base.BaseContract;
import java.util.List;
@ -25,6 +25,6 @@ import java.util.List;
* Created by srv_twry on 14/2/18.
*/
public interface SearchViewCallback extends ViewCallback {
public interface SearchViewCallback extends BaseContract.View {
void addRecentSearches(List<String> recentSearches);
}

View File

@ -17,11 +17,11 @@
*/
package org.kiwix.kiwixmobile.zim_manager;
import org.kiwix.kiwixmobile.base.ViewCallback;
import org.kiwix.kiwixmobile.base.BaseContract;
/**
* Created by srv_twry on 15/2/18.
*/
public interface ZimManageViewCallback extends ViewCallback {
interface ZimManageViewCallback extends BaseContract.View {
}

View File

@ -34,7 +34,7 @@ public class ZimFileSelectPresenter extends BasePresenter<ZimFileSelectViewCallb
BookDao bookDao;
@Inject
public ZimFileSelectPresenter() {
ZimFileSelectPresenter() {
}
@Override
@ -44,7 +44,7 @@ public class ZimFileSelectPresenter extends BasePresenter<ZimFileSelectViewCallb
public void loadLocalZimFileFromDb() {
ArrayList<LibraryNetworkEntity.Book> books = bookDao.getBooks();
getMvpView().showFiles(books);
view.showFiles(books);
}
}

View File

@ -17,7 +17,7 @@
*/
package org.kiwix.kiwixmobile.zim_manager.fileselect_view;
import org.kiwix.kiwixmobile.base.ViewCallback;
import org.kiwix.kiwixmobile.base.BaseContract;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
import java.util.ArrayList;
@ -25,6 +25,6 @@ import java.util.ArrayList;
/**
* Created by EladKeyshawn on 06/04/2017.
*/
public interface ZimFileSelectViewCallback extends ViewCallback {
public interface ZimFileSelectViewCallback extends BaseContract.View {
void showFiles(ArrayList<LibraryNetworkEntity.Book> books);
}

View File

@ -42,17 +42,17 @@ public class LibraryPresenter extends BasePresenter<LibraryViewCallback> {
BookDao bookDao;
@Inject
public LibraryPresenter() {
LibraryPresenter() {
}
void loadBooks() {
getMvpView().displayScanningContent();
view.displayScanningContent();
kiwixService.getLibrary()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(library -> getMvpView().showBooks(library.getBooks()), error -> {
.subscribe(library -> view.showBooks(library.getBooks()), error -> {
String msg = error.getLocalizedMessage();
Log.w("kiwixLibrary", "Error loading books:" + (msg != null ? msg : "(null)"));
getMvpView().displayNoItemsFound();
view.displayNoItemsFound();
});
}
@ -60,7 +60,7 @@ public class LibraryPresenter extends BasePresenter<LibraryViewCallback> {
for (LibraryNetworkEntity.Book book : bookDao.getDownloadingBooks()) {
if (!DownloadFragment.mDownloads.containsValue(book)) {
book.url = book.remoteUrl;
getMvpView().downloadFile(book);
view.downloadFile(book);
}
}
}

View File

@ -17,7 +17,7 @@
*/
package org.kiwix.kiwixmobile.zim_manager.library_view;
import org.kiwix.kiwixmobile.base.ViewCallback;
import org.kiwix.kiwixmobile.base.BaseContract;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book;
@ -27,7 +27,7 @@ import java.util.LinkedList;
* Created by EladKeyshawn on 06/04/2017.
*/
public interface LibraryViewCallback extends ViewCallback {
public interface LibraryViewCallback extends BaseContract.View {
void showBooks(LinkedList<Book> books);