mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-04 03:06:41 -04:00
Add BaseContract
This commit is contained in:
parent
0691ac5e5b
commit
05ca65f525
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -18,40 +18,20 @@
|
|||||||
package org.kiwix.kiwixmobile.base;
|
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
|
@Override
|
||||||
public void attachView(T mvpView) {
|
public void attachView(T view) {
|
||||||
this.mMvpView = mvpView;
|
this.view = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void detachView() {
|
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -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();
|
|
||||||
}
|
|
@ -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 {
|
|
||||||
}
|
|
@ -34,13 +34,13 @@ public class BookmarksPresenter extends BasePresenter<BookmarksViewCallback> {
|
|||||||
BookmarksDao bookmarksDao;
|
BookmarksDao bookmarksDao;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public BookmarksPresenter() {
|
BookmarksPresenter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadBookmarks() {
|
public void loadBookmarks() {
|
||||||
ArrayList<String> bookmarks = bookmarksDao.getBookmarkTitles(ZimContentProvider.getId(), ZimContentProvider.getName());
|
ArrayList<String> bookmarks = bookmarksDao.getBookmarkTitles(ZimContentProvider.getId(), ZimContentProvider.getName());
|
||||||
ArrayList<String> bookmarkUrls = bookmarksDao.getBookmarks(ZimContentProvider.getId(), ZimContentProvider.getName());
|
ArrayList<String> bookmarkUrls = bookmarksDao.getBookmarks(ZimContentProvider.getId(), ZimContentProvider.getName());
|
||||||
getMvpView().showBookmarks(bookmarks, bookmarkUrls);
|
view.showBookmarks(bookmarks, bookmarkUrls);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.bookmark;
|
package org.kiwix.kiwixmobile.bookmark;
|
||||||
|
|
||||||
import org.kiwix.kiwixmobile.base.ViewCallback;
|
import org.kiwix.kiwixmobile.base.BaseContract;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ import java.util.ArrayList;
|
|||||||
* Created by EladKeyshawn on 05/04/2017.
|
* 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 showBookmarks(ArrayList<String> bookmarks, ArrayList<String> bookmarkUrls);
|
||||||
|
|
||||||
void popDeleteBookmarksSnackbar();
|
void popDeleteBookmarksSnackbar();
|
||||||
|
@ -32,7 +32,7 @@ public class SearchPresenter extends BasePresenter<SearchViewCallback> {
|
|||||||
RecentSearchDao recentSearchDao;
|
RecentSearchDao recentSearchDao;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public SearchPresenter() {}
|
SearchPresenter() {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void attachView(SearchViewCallback searchViewCallback) {
|
public void attachView(SearchViewCallback searchViewCallback) {
|
||||||
@ -40,7 +40,7 @@ public class SearchPresenter extends BasePresenter<SearchViewCallback> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void getRecentSearches() {
|
void getRecentSearches() {
|
||||||
getMvpView().addRecentSearches(recentSearchDao.getRecentSearches());
|
view.addRecentSearches(recentSearchDao.getRecentSearches());
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveSearch(String title) {
|
void saveSearch(String title) {
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.search;
|
package org.kiwix.kiwixmobile.search;
|
||||||
|
|
||||||
import org.kiwix.kiwixmobile.base.ViewCallback;
|
import org.kiwix.kiwixmobile.base.BaseContract;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -25,6 +25,6 @@ import java.util.List;
|
|||||||
* Created by srv_twry on 14/2/18.
|
* Created by srv_twry on 14/2/18.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface SearchViewCallback extends ViewCallback {
|
public interface SearchViewCallback extends BaseContract.View {
|
||||||
void addRecentSearches(List<String> recentSearches);
|
void addRecentSearches(List<String> recentSearches);
|
||||||
}
|
}
|
||||||
|
@ -17,11 +17,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.zim_manager;
|
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.
|
* Created by srv_twry on 15/2/18.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface ZimManageViewCallback extends ViewCallback {
|
interface ZimManageViewCallback extends BaseContract.View {
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ public class ZimFileSelectPresenter extends BasePresenter<ZimFileSelectViewCallb
|
|||||||
BookDao bookDao;
|
BookDao bookDao;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ZimFileSelectPresenter() {
|
ZimFileSelectPresenter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -44,7 +44,7 @@ public class ZimFileSelectPresenter extends BasePresenter<ZimFileSelectViewCallb
|
|||||||
|
|
||||||
public void loadLocalZimFileFromDb() {
|
public void loadLocalZimFileFromDb() {
|
||||||
ArrayList<LibraryNetworkEntity.Book> books = bookDao.getBooks();
|
ArrayList<LibraryNetworkEntity.Book> books = bookDao.getBooks();
|
||||||
getMvpView().showFiles(books);
|
view.showFiles(books);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.zim_manager.fileselect_view;
|
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 org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -25,6 +25,6 @@ import java.util.ArrayList;
|
|||||||
/**
|
/**
|
||||||
* Created by EladKeyshawn on 06/04/2017.
|
* Created by EladKeyshawn on 06/04/2017.
|
||||||
*/
|
*/
|
||||||
public interface ZimFileSelectViewCallback extends ViewCallback {
|
public interface ZimFileSelectViewCallback extends BaseContract.View {
|
||||||
void showFiles(ArrayList<LibraryNetworkEntity.Book> books);
|
void showFiles(ArrayList<LibraryNetworkEntity.Book> books);
|
||||||
}
|
}
|
||||||
|
@ -42,17 +42,17 @@ public class LibraryPresenter extends BasePresenter<LibraryViewCallback> {
|
|||||||
BookDao bookDao;
|
BookDao bookDao;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public LibraryPresenter() {
|
LibraryPresenter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadBooks() {
|
void loadBooks() {
|
||||||
getMvpView().displayScanningContent();
|
view.displayScanningContent();
|
||||||
kiwixService.getLibrary()
|
kiwixService.getLibrary()
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.subscribe(library -> getMvpView().showBooks(library.getBooks()), error -> {
|
.subscribe(library -> view.showBooks(library.getBooks()), error -> {
|
||||||
String msg = error.getLocalizedMessage();
|
String msg = error.getLocalizedMessage();
|
||||||
Log.w("kiwixLibrary", "Error loading books:" + (msg != null ? msg : "(null)"));
|
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()) {
|
for (LibraryNetworkEntity.Book book : bookDao.getDownloadingBooks()) {
|
||||||
if (!DownloadFragment.mDownloads.containsValue(book)) {
|
if (!DownloadFragment.mDownloads.containsValue(book)) {
|
||||||
book.url = book.remoteUrl;
|
book.url = book.remoteUrl;
|
||||||
getMvpView().downloadFile(book);
|
view.downloadFile(book);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.zim_manager.library_view;
|
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;
|
||||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book;
|
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book;
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ import java.util.LinkedList;
|
|||||||
* Created by EladKeyshawn on 06/04/2017.
|
* Created by EladKeyshawn on 06/04/2017.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface LibraryViewCallback extends ViewCallback {
|
public interface LibraryViewCallback extends BaseContract.View {
|
||||||
|
|
||||||
void showBooks(LinkedList<Book> books);
|
void showBooks(LinkedList<Book> books);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user