mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-20 10:23:30 -04:00
basic mvp for old (bookmarks)
This commit is contained in:
parent
53bfd184f3
commit
8c84688108
@ -146,7 +146,7 @@
|
||||
|
||||
<activity android:name=".settings.KiwixSettingsActivity" />
|
||||
<activity android:name=".SearchActivity" />
|
||||
<activity android:name=".BookmarksActivity" />
|
||||
<activity android:name=".bookmarksView.BookmarksActivity" />
|
||||
|
||||
<provider
|
||||
android:name=".ZimContentProvider"
|
||||
|
@ -82,6 +82,7 @@ import java.util.Locale;
|
||||
import javax.inject.Inject;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.json.JSONArray;
|
||||
import org.kiwix.kiwixmobile.bookmarksView.BookmarksActivity;
|
||||
import org.kiwix.kiwixmobile.database.BookmarksDao;
|
||||
import org.kiwix.kiwixmobile.database.KiwixDatabase;
|
||||
import org.kiwix.kiwixmobile.di.components.ApplicationComponent;
|
||||
|
@ -0,0 +1,40 @@
|
||||
package org.kiwix.kiwixmobile.bookmarksView;
|
||||
|
||||
/**
|
||||
* Created by EladKeyshawn on 05/04/2017.
|
||||
*/
|
||||
|
||||
public class BasePresenter<T extends ViewCallback> implements Presenter<T> {
|
||||
|
||||
private T mMvpView;
|
||||
@Override
|
||||
public void attachView(T mvpView) {
|
||||
this.mMvpView = mvpView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detachView() {
|
||||
mMvpView = 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");
|
||||
}
|
||||
}
|
||||
}
|
@ -18,16 +18,17 @@
|
||||
*/
|
||||
|
||||
|
||||
package org.kiwix.kiwixmobile;
|
||||
package org.kiwix.kiwixmobile.bookmarksView;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.CoordinatorLayout;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.SparseBooleanArray;
|
||||
import android.view.ActionMode;
|
||||
@ -42,29 +43,31 @@ import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ListView;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.kiwix.kiwixmobile.database.BookmarksDao;
|
||||
import org.kiwix.kiwixmobile.database.KiwixDatabase;
|
||||
import org.kiwix.kiwixmobile.utils.DimenUtils;
|
||||
import org.kiwix.kiwixmobile.BaseActivity;
|
||||
import org.kiwix.kiwixmobile.KiwixMobileActivity;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
import org.kiwix.kiwixmobile.di.components.ApplicationComponent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BookmarksActivity extends AppCompatActivity
|
||||
implements AdapterView.OnItemClickListener {
|
||||
import javax.inject.Inject;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class BookmarksActivity extends BaseActivity
|
||||
implements AdapterView.OnItemClickListener, BookmarksViewCallback {
|
||||
|
||||
private SparseBooleanArray sparseBooleanArray;
|
||||
private ArrayList<String> bookmarks;
|
||||
private ArrayList<String> bookmarkUrls;
|
||||
private ArrayList<String> tempContents;
|
||||
private ListView bookmarksList;
|
||||
private ArrayAdapter adapter;
|
||||
private ArrayList<String> selected;
|
||||
private int numOfSelected;
|
||||
private CoordinatorLayout snackbarLayout;
|
||||
private LinearLayout noBookmarksLayout;
|
||||
private BookmarksDao bookmarksDao;
|
||||
@BindView(R.id.bookmarks_list) ListView bookmarksList;
|
||||
private BookmarksArrayAdapter adapter;
|
||||
@BindView(R.id.bookmarks_activity_layout) CoordinatorLayout snackbarLayout;
|
||||
@BindView(R.id.bookmarks_none_linlayout) LinearLayout noBookmarksLayout;
|
||||
@Inject BookmarksPresenter presenter;
|
||||
private ActionModeListener actionModeListener;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -74,84 +77,29 @@ public class BookmarksActivity extends AppCompatActivity
|
||||
}
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_bookmarks);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
setUpToolbar();
|
||||
snackbarLayout = (CoordinatorLayout) findViewById(R.id.bookmarks_activity_layout);
|
||||
selected = new ArrayList<>();
|
||||
bookmarksList = (ListView) findViewById(R.id.bookmarks_list);
|
||||
noBookmarksLayout = (LinearLayout) findViewById(R.id.bookmarks_none_linlayout);
|
||||
bookmarks = new ArrayList<>();
|
||||
bookmarkUrls = new ArrayList<>();
|
||||
|
||||
bookmarksDao = new BookmarksDao(KiwixDatabase.getInstance(this));
|
||||
bookmarks = bookmarksDao.getBookmarkTitles(ZimContentProvider.getId(), ZimContentProvider.getName());
|
||||
bookmarkUrls = bookmarksDao.getBookmarks(ZimContentProvider.getId(), ZimContentProvider.getName());
|
||||
|
||||
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.bookmarks_row, R.id.bookmark_title, bookmarks) {
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
if(convertView == null){
|
||||
LayoutInflater inflater = getLayoutInflater();
|
||||
convertView = inflater.inflate(R.layout.bookmarks_row, null);
|
||||
}
|
||||
return super.getView(position, convertView, parent);
|
||||
}
|
||||
};
|
||||
actionModeListener = new ActionModeListener();
|
||||
adapter = new BookmarksArrayAdapter(getApplicationContext(), R.layout.bookmarks_row, R.id.bookmark_title, bookmarks);
|
||||
bookmarksList.setAdapter(adapter);
|
||||
setNoBookmarksState();
|
||||
|
||||
bookmarksList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
|
||||
bookmarksList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
|
||||
@Override
|
||||
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
|
||||
boolean checked) {
|
||||
if (checked) {
|
||||
selected.add(bookmarks.get(position));
|
||||
numOfSelected++;
|
||||
mode.setTitle(Integer.toString(numOfSelected));
|
||||
} else if (selected.contains(bookmarks.get(position))) {
|
||||
selected.remove(bookmarks.get(position));
|
||||
numOfSelected--;
|
||||
if (numOfSelected == 0) {
|
||||
mode.finish();
|
||||
} else {
|
||||
mode.setTitle(Integer.toString(numOfSelected));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
MenuInflater inflater = mode.getMenuInflater();
|
||||
inflater.inflate(R.menu.menu_bookmarks, menu);
|
||||
numOfSelected = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_bookmarks_delete:
|
||||
deleteSelectedItems();
|
||||
popDeleteBookmarksSnackbar();
|
||||
mode.finish();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode mode) {
|
||||
|
||||
}
|
||||
});
|
||||
bookmarksList.setMultiChoiceModeListener(actionModeListener);
|
||||
bookmarksList.setOnItemClickListener(this);
|
||||
|
||||
presenter.attachView(this);
|
||||
presenter.loadBookmarks(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setupDagger(ApplicationComponent appComponent) {
|
||||
appComponent.inject(this);
|
||||
}
|
||||
|
||||
|
||||
private void setNoBookmarksState() {
|
||||
if (bookmarksList.getCount() == 0) {
|
||||
noBookmarksLayout.setVisibility(View.VISIBLE);
|
||||
@ -160,31 +108,9 @@ public class BookmarksActivity extends AppCompatActivity
|
||||
}
|
||||
}
|
||||
|
||||
private void popDeleteBookmarksSnackbar() {
|
||||
Snackbar bookmarkDeleteSnackbar =
|
||||
Snackbar.make(snackbarLayout, numOfSelected + " " + getString(R.string.deleted_message), Snackbar.LENGTH_LONG);
|
||||
// .setAction(ShortcutUtils.stringsGetter(R.string.undo, this), v -> {
|
||||
// restoreBookmarks();
|
||||
// setNoBookmarksState();
|
||||
// Toast.makeText(getApplicationContext(), ShortcutUtils.stringsGetter(R.string.bookmarks_restored, getBaseContext()), Toast.LENGTH_SHORT)
|
||||
// .show();
|
||||
// });
|
||||
|
||||
bookmarkDeleteSnackbar.setActionTextColor(getResources().getColor(R.color.white));
|
||||
bookmarkDeleteSnackbar.show();
|
||||
}
|
||||
|
||||
/*private void restoreBookmarks() {
|
||||
bookmarksDao.resetBookmarksToPrevious(tempContents);
|
||||
bookmarks = bookmarksDao.getBookmarks();
|
||||
adapter.notifyDataSetChanged();
|
||||
setNoBookmarksState();
|
||||
refreshBookmarksList();
|
||||
}*/
|
||||
|
||||
private void deleteSelectedItems() {
|
||||
sparseBooleanArray = bookmarksList.getCheckedItemPositions();
|
||||
tempContents = new ArrayList<>(bookmarks);
|
||||
SparseBooleanArray sparseBooleanArray = bookmarksList.getCheckedItemPositions();
|
||||
for (int i = sparseBooleanArray.size() - 1; i >= 0; i--) {
|
||||
deleteBookmark(bookmarkUrls.get(sparseBooleanArray.keyAt(i)));
|
||||
bookmarks.remove(sparseBooleanArray.keyAt(i));
|
||||
@ -195,15 +121,9 @@ public class BookmarksActivity extends AppCompatActivity
|
||||
}
|
||||
|
||||
private void deleteBookmark(String article) {
|
||||
bookmarksDao.deleteBookmark(article, ZimContentProvider.getId(), ZimContentProvider.getName());
|
||||
presenter.deleteBookmark(article);
|
||||
}
|
||||
|
||||
/*private void refreshBookmarksList() {
|
||||
bookmarks.clear();
|
||||
bookmarks = bookmarksDao.getBookmarks();
|
||||
adapter.notifyDataSetChanged();
|
||||
setNoBookmarksState();
|
||||
}*/
|
||||
|
||||
private void setUpToolbar() {
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
@ -243,9 +163,118 @@ public class BookmarksActivity extends AppCompatActivity
|
||||
startActivity(startIntent);
|
||||
|
||||
} else { // we have a parent activity waiting...
|
||||
setResult(RESULT_OK,startIntent );
|
||||
setResult(RESULT_OK, startIntent);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showBookmarks(ArrayList<String> bookmarks, ArrayList<String> bookmarkUrls) {
|
||||
this.bookmarks.clear();
|
||||
this.bookmarkUrls.clear();
|
||||
this.bookmarks.addAll(bookmarks);
|
||||
this.bookmarkUrls.addAll(bookmarkUrls);
|
||||
adapter.notifyDataSetChanged();
|
||||
setNoBookmarksState();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateAdapter() {
|
||||
adapter.notifyDataSetChanged();
|
||||
setNoBookmarksState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void popDeleteBookmarksSnackbar() {
|
||||
Snackbar bookmarkDeleteSnackbar =
|
||||
Snackbar.make(snackbarLayout, actionModeListener.getNumOfSelected() + " " + getString(R.string.deleted_message), Snackbar.LENGTH_LONG);
|
||||
|
||||
|
||||
bookmarkDeleteSnackbar.setActionTextColor(getResources().getColor(R.color.white));
|
||||
bookmarkDeleteSnackbar.show();
|
||||
}
|
||||
|
||||
|
||||
class ActionModeListener implements AbsListView.MultiChoiceModeListener {
|
||||
private ArrayList<String> selected = new ArrayList<>();
|
||||
private int numOfSelected = 0;
|
||||
|
||||
public ActionModeListener() {
|
||||
|
||||
}
|
||||
|
||||
public int getNumOfSelected() {
|
||||
return numOfSelected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
|
||||
boolean checked) {
|
||||
if (checked) {
|
||||
selected.add(bookmarks.get(position));
|
||||
numOfSelected++;
|
||||
mode.setTitle(Integer.toString(numOfSelected));
|
||||
} else if (selected.contains(bookmarks.get(position))) {
|
||||
selected.remove(bookmarks.get(position));
|
||||
numOfSelected--;
|
||||
if (numOfSelected == 0) {
|
||||
mode.finish();
|
||||
} else {
|
||||
mode.setTitle(Integer.toString(numOfSelected));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
MenuInflater inflater = mode.getMenuInflater();
|
||||
inflater.inflate(R.menu.menu_bookmarks, menu);
|
||||
numOfSelected = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_bookmarks_delete:
|
||||
deleteSelectedItems();
|
||||
popDeleteBookmarksSnackbar();
|
||||
mode.finish();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode mode) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class BookmarksArrayAdapter extends ArrayAdapter<String> {
|
||||
|
||||
public BookmarksArrayAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
|
||||
super(context, resource, textViewResourceId, objects);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
if(convertView == null){
|
||||
LayoutInflater inflater = getLayoutInflater();
|
||||
convertView = inflater.inflate(R.layout.bookmarks_row, null);
|
||||
}
|
||||
return super.getView(position, convertView, parent);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package org.kiwix.kiwixmobile.bookmarksView;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.kiwix.kiwixmobile.ZimContentProvider;
|
||||
import org.kiwix.kiwixmobile.database.BookmarksDao;
|
||||
import org.kiwix.kiwixmobile.database.KiwixDatabase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by EladKeyshawn on 05/04/2017.
|
||||
*/
|
||||
|
||||
public class BookmarksPresenter extends BasePresenter<BookmarksViewCallback> {
|
||||
|
||||
|
||||
private BookmarksDao bookmarksDao;
|
||||
private ArrayList<String> bookmarks;
|
||||
private ArrayList<String> bookmarkUrls;
|
||||
|
||||
@Inject
|
||||
public BookmarksPresenter() {
|
||||
}
|
||||
|
||||
public void loadBookmarks(Context context) {
|
||||
bookmarksDao = new BookmarksDao(KiwixDatabase.getInstance(context));
|
||||
bookmarks = bookmarksDao.getBookmarkTitles(ZimContentProvider.getId(), ZimContentProvider.getName());
|
||||
bookmarkUrls = bookmarksDao.getBookmarks(ZimContentProvider.getId(), ZimContentProvider.getName());
|
||||
getMvpView().showBookmarks(bookmarks,bookmarkUrls);
|
||||
}
|
||||
|
||||
|
||||
public void deleteBookmark(String article) {
|
||||
bookmarksDao.deleteBookmark(article, ZimContentProvider.getId(), ZimContentProvider.getName());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void attachView(BookmarksViewCallback mvpView) {
|
||||
super.attachView(mvpView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detachView() {
|
||||
super.detachView();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package org.kiwix.kiwixmobile.bookmarksView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by EladKeyshawn on 05/04/2017.
|
||||
*/
|
||||
|
||||
public interface BookmarksViewCallback extends ViewCallback{
|
||||
void showBookmarks(ArrayList<String> bookmarks, ArrayList<String> bookmarkUrls);
|
||||
void updateAdapter();
|
||||
|
||||
void popDeleteBookmarksSnackbar();
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package org.kiwix.kiwixmobile.bookmarksView;
|
||||
|
||||
/**
|
||||
* Created by EladKeyshawn on 05/04/2017.
|
||||
*/
|
||||
public interface Presenter<V extends ViewCallback> {
|
||||
|
||||
void attachView(V mvpView);
|
||||
|
||||
void detachView();
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package org.kiwix.kiwixmobile.bookmarksView;
|
||||
|
||||
/**
|
||||
* Created by EladKeyshawn on 05/04/2017.
|
||||
*/
|
||||
public interface ViewCallback {
|
||||
}
|
@ -4,6 +4,7 @@ import dagger.Component;
|
||||
import javax.inject.Singleton;
|
||||
import org.kiwix.kiwixmobile.KiwixMobileActivity;
|
||||
import org.kiwix.kiwixmobile.LibraryFragment;
|
||||
import org.kiwix.kiwixmobile.bookmarksView.BookmarksActivity;
|
||||
import org.kiwix.kiwixmobile.di.modules.ApplicationModule;
|
||||
import org.kiwix.kiwixmobile.di.modules.NetworkModule;
|
||||
import org.kiwix.kiwixmobile.downloader.DownloadService;
|
||||
@ -19,4 +20,6 @@ public interface ApplicationComponent {
|
||||
void inject(DownloadService service);
|
||||
|
||||
void inject(LibraryFragment libraryFragment);
|
||||
|
||||
void inject(BookmarksActivity bookmarksActivity);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user