Convert BookmarkActivity to kotlin

This commit is contained in:
Sonu Sourav 2020-04-10 20:55:07 +05:30
parent faa8e272a4
commit dff32eb535
3 changed files with 278 additions and 289 deletions

View File

@ -1,288 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 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.core.bookmark;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.view.ActionMode;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import org.kiwix.kiwixmobile.core.Intents;
import org.kiwix.kiwixmobile.core.R;
import org.kiwix.kiwixmobile.core.R2;
import org.kiwix.kiwixmobile.core.base.BaseActivity;
import org.kiwix.kiwixmobile.core.di.components.CoreComponent;
import org.kiwix.kiwixmobile.core.extensions.ImageViewExtensionsKt;
import org.kiwix.kiwixmobile.core.main.CoreMainActivity;
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer;
import org.kiwix.kiwixmobile.core.utils.DialogShower;
import org.kiwix.kiwixmobile.core.utils.KiwixDialog;
import static org.kiwix.kiwixmobile.core.utils.ConstantsKt.EXTRA_CHOSE_X_FILE;
import static org.kiwix.kiwixmobile.core.utils.ConstantsKt.EXTRA_CHOSE_X_TITLE;
import static org.kiwix.kiwixmobile.core.utils.ConstantsKt.EXTRA_CHOSE_X_URL;
public class BookmarksActivity extends BaseActivity implements BookmarksContract.View,
BookmarksAdapter.OnItemClickListener {
private final List<BookmarkItem> bookmarksList = new ArrayList<>();
private final List<BookmarkItem> allBookmarks = new ArrayList<>();
private final List<BookmarkItem> deleteList = new ArrayList<>();
@BindView(R2.id.toolbar)
Toolbar toolbar;
@BindView(R2.id.recycler_view)
RecyclerView recyclerView;
@BindView(R2.id.no_bookmarks)
TextView noBookmarks;
@BindView(R2.id.bookmarks_switch)
Switch bookmarksSwitch;
@Inject
BookmarksContract.Presenter presenter;
@Inject
ZimReaderContainer zimReaderContainer;
@Inject
DialogShower dialogShower;
private boolean refreshAdapter = true;
private BookmarksAdapter bookmarksAdapter;
private ActionMode actionMode;
private final ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_context_delete, menu);
bookmarksSwitch.setEnabled(false);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
refreshAdapter = false;
if (item.getItemId() == R.id.menu_context_delete) {
dialogShower.show(KiwixDialog.DeleteBookmarks.INSTANCE,(Function0<Unit>)() ->{
allBookmarks.removeAll(deleteList);
for (BookmarkItem bookmark : deleteList) {
int position = bookmarksList.indexOf(bookmark);
bookmarksList.remove(bookmark);
bookmarksAdapter.notifyItemRemoved(position);
bookmarksAdapter.notifyItemRangeChanged(position, bookmarksAdapter.getItemCount());
}
presenter.deleteBookmarks(new ArrayList<>(deleteList));
mode.finish();
return Unit.INSTANCE;
});
return true;
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
if (deleteList.size() != 0) {
deleteList.clear();
}
actionMode = null;
if (refreshAdapter) {
bookmarksAdapter.notifyDataSetChanged();
}
bookmarksSwitch.setEnabled(true);
}
};
@Override protected void injection(CoreComponent coreComponent) {
coreComponent.activityComponentBuilder().activity(this).build().inject(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter.attachView(this);
setContentView(R.layout.activity_bookmarks);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(R.string.bookmarks);
}
setupBookmarksAdapter();
recyclerView.setAdapter(bookmarksAdapter);
bookmarksSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
sharedPreferenceUtil.setShowBookmarksCurrentBook(!isChecked);
presenter.loadBookmarks(sharedPreferenceUtil.getShowBookmarksCurrentBook());
});
bookmarksSwitch.setChecked(!sharedPreferenceUtil.getShowBookmarksCurrentBook());
}
private void setupBookmarksAdapter() {
bookmarksAdapter = new BookmarksAdapter(bookmarksList, deleteList, this);
bookmarksAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override public void onChanged() {
super.onChanged();
noBookmarks.setVisibility(bookmarksList.size() == 0 ? View.VISIBLE : View.GONE);
}
});
}
@Override
protected void onResume() {
super.onResume();
presenter.loadBookmarks(sharedPreferenceUtil.getShowBookmarksCurrentBook());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_bookmarks, menu);
SearchView search = (SearchView) menu.findItem(R.id.menu_bookmarks_search).getActionView();
search.setQueryHint(getString(R.string.search_bookmarks));
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
bookmarksList.clear();
bookmarksList.addAll(allBookmarks);
if ("".equals(newText)) {
bookmarksAdapter.notifyDataSetChanged();
return true;
}
presenter.filterBookmarks(bookmarksList, newText);
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
onBackPressed();
return true;
} else if (itemId == R.id.menu_bookmarks_clear) {
dialogShower.show(KiwixDialog.DeleteBookmarks.INSTANCE, (Function0<Unit>) () -> {
presenter.deleteBookmarks(new ArrayList<>(allBookmarks));
allBookmarks.clear();
bookmarksList.clear();
bookmarksAdapter.notifyDataSetChanged();
Snackbar.make(noBookmarks, R.string.all_bookmarks_cleared, Snackbar.LENGTH_SHORT).show();
return Unit.INSTANCE;
});
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
presenter.detachView();
super.onDestroy();
}
@Override
public void updateBookmarksList(List<BookmarkItem> bookmarksList) {
allBookmarks.clear();
allBookmarks.addAll(bookmarksList);
notifyBookmarksListFiltered(bookmarksList);
}
@Override
public void notifyBookmarksListFiltered(List<BookmarkItem> bookmarksList) {
this.bookmarksList.clear();
this.bookmarksList.addAll(bookmarksList);
bookmarksAdapter.notifyDataSetChanged();
}
@Override
public void onItemClick(ImageView favicon, BookmarkItem bookmark) {
if (actionMode == null) {
Intent intent = Intents.internal(CoreMainActivity.class);
if ("null".equals(bookmark.getBookmarkUrl())) {
intent.putExtra(EXTRA_CHOSE_X_TITLE, bookmark.getBookmarkTitle());
} else {
intent.putExtra(EXTRA_CHOSE_X_URL, bookmark.getBookmarkUrl());
}
if (bookmark.getZimFilePath() != null && !bookmark.getZimFilePath()
.equals(zimReaderContainer.getZimCanonicalPath())) {
intent.putExtra(EXTRA_CHOSE_X_FILE, bookmark.getZimFilePath());
}
setResult(RESULT_OK, intent);
finish();
} else {
toggleSelection(favicon, bookmark);
}
}
@Override
public boolean onItemLongClick(ImageView favicon, BookmarkItem bookmark) {
if (actionMode != null) {
return false;
}
actionMode = startSupportActionMode(actionModeCallback);
refreshAdapter = true;
toggleSelection(favicon, bookmark);
return true;
}
private void toggleSelection(ImageView favicon, BookmarkItem bookmark) {
if (deleteList.remove(bookmark)) {
ImageViewExtensionsKt.setBitmapFromString(favicon, bookmark.getFavicon());
} else {
favicon.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_check_circle_blue_24dp));
deleteList.add(bookmark);
}
actionMode.setTitle(getString(R.string.selected_items, deleteList.size()));
if (deleteList.size() == 0) {
actionMode.finish();
}
}
}

View File

@ -0,0 +1,275 @@
/*
* Kiwix Android
* Copyright (c) 2019 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.core.bookmark
import android.app.Activity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.CompoundButton
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import androidx.appcompat.view.ActionMode
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
import butterknife.BindView
import com.google.android.material.snackbar.Snackbar
import org.kiwix.kiwixmobile.core.Intents.internal
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.R2
import org.kiwix.kiwixmobile.core.base.BaseActivity
import org.kiwix.kiwixmobile.core.di.components.CoreComponent
import org.kiwix.kiwixmobile.core.extensions.setBitmapFromString
import org.kiwix.kiwixmobile.core.main.CoreMainActivity
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.DialogShower
import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_FILE
import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_TITLE
import org.kiwix.kiwixmobile.core.utils.EXTRA_CHOSE_X_URL
import org.kiwix.kiwixmobile.core.utils.KiwixDialog
import java.util.ArrayList
import javax.inject.Inject
class BookmarksActivity : BaseActivity(),
BookmarksContract.View, BookmarksAdapter.OnItemClickListener {
private val bookmarksList: MutableList<BookmarkItem> = ArrayList()
private val allBookmarks: MutableList<BookmarkItem> = ArrayList()
private val deleteList: MutableList<BookmarkItem> = ArrayList()
@JvmField @BindView(R2.id.toolbar)
var toolbar: Toolbar? = null
@JvmField @BindView(R2.id.recycler_view)
var recyclerView: RecyclerView? = null
@JvmField @BindView(R2.id.no_bookmarks)
var noBookmarks: TextView? = null
@JvmField @BindView(R2.id.bookmarks_switch)
var bookmarksSwitch: Switch? = null
@Inject
private var presenter: BookmarksContract.Presenter? = null
@Inject
var zimReaderContainer: ZimReaderContainer? = null
@Inject
var dialogShower: DialogShower? = null
private var refreshAdapter = true
private var bookmarksAdapter: BookmarksAdapter? = null
private var actionMode: ActionMode? = null
private val actionModeCallback: ActionMode.Callback = object : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
mode.menuInflater.inflate(R.menu.menu_context_delete, menu)
bookmarksSwitch!!.isEnabled = false
return true
}
override fun onPrepareActionMode(
mode: ActionMode,
menu: Menu
): Boolean = false
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
refreshAdapter = false
if (item.itemId == R.id.menu_context_delete) {
dialogShower!!.show(KiwixDialog.DeleteBookmarks, {
allBookmarks.removeAll(deleteList)
for (bookmark in deleteList) {
val position = bookmarksList.indexOf(bookmark)
bookmarksList.remove(bookmark)
bookmarksAdapter!!.notifyItemRemoved(position)
bookmarksAdapter!!.notifyItemRangeChanged(position, bookmarksAdapter!!.itemCount)
}
presenter!!.deleteBookmarks(ArrayList(deleteList))
mode.finish()
})
return true
}
return false
}
override fun onDestroyActionMode(mode: ActionMode) {
if (deleteList.size != 0) {
deleteList.clear()
}
actionMode = null
if (refreshAdapter) {
bookmarksAdapter!!.notifyDataSetChanged()
}
bookmarksSwitch!!.isEnabled = true
}
}
override fun injection(coreComponent: CoreComponent) {
coreComponent.activityComponentBuilder().activity(this).build().inject(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter!!.attachView(this)
setContentView(R.layout.activity_bookmarks)
setSupportActionBar(toolbar)
val actionBar = supportActionBar
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true)
actionBar.setTitle(R.string.bookmarks)
}
setupBookmarksAdapter()
recyclerView!!.adapter = bookmarksAdapter
bookmarksSwitch!!.setOnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean ->
sharedPreferenceUtil.showBookmarksCurrentBook = !isChecked
presenter!!.loadBookmarks(sharedPreferenceUtil.showBookmarksCurrentBook)
}
bookmarksSwitch!!.isChecked = !sharedPreferenceUtil.showBookmarksCurrentBook
}
private fun setupBookmarksAdapter() {
bookmarksAdapter = BookmarksAdapter(bookmarksList, deleteList, this)
bookmarksAdapter!!.registerAdapterDataObserver(object : AdapterDataObserver() {
override fun onChanged() {
super.onChanged()
noBookmarks!!.visibility = if (bookmarksList.size == 0) View.VISIBLE else View.GONE
}
})
}
override fun onResume() {
super.onResume()
presenter!!.loadBookmarks(sharedPreferenceUtil.showBookmarksCurrentBook)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_bookmarks, menu)
val search = menu.findItem(R.id.menu_bookmarks_search)
.actionView as SearchView
search.queryHint = getString(R.string.search_bookmarks)
search.setOnQueryTextListener(object :
SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean = false
override fun onQueryTextChange(newText: String): Boolean {
bookmarksList.clear()
bookmarksList.addAll(allBookmarks)
if ("" == newText) {
bookmarksAdapter!!.notifyDataSetChanged()
return true
}
presenter!!.filterBookmarks(bookmarksList, newText)
return true
}
})
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val itemId = item.itemId
if (itemId == android.R.id.home) {
onBackPressed()
return true
} else if (itemId == R.id.menu_bookmarks_clear) {
dialogShower!!.show(KiwixDialog.DeleteBookmarks, {
presenter!!.deleteBookmarks(ArrayList(allBookmarks))
allBookmarks.clear()
bookmarksList.clear()
bookmarksAdapter!!.notifyDataSetChanged()
Snackbar.make(noBookmarks!!, R.string.all_bookmarks_cleared, Snackbar.LENGTH_SHORT).show()
})
return true
}
return super.onOptionsItemSelected(item)
}
override fun onDestroy() {
presenter!!.detachView()
super.onDestroy()
}
override fun updateBookmarksList(bookmarksList: List<BookmarkItem>) {
allBookmarks.clear()
allBookmarks.addAll(bookmarksList)
notifyBookmarksListFiltered(bookmarksList)
}
override fun notifyBookmarksListFiltered(bookmarksList: List<BookmarkItem>) {
this.bookmarksList.clear()
this.bookmarksList.addAll(bookmarksList)
bookmarksAdapter!!.notifyDataSetChanged()
}
override fun onItemClick(
favicon: ImageView,
bookmark: BookmarkItem
) {
if (actionMode == null) {
val intent = internal(
CoreMainActivity::class.java
)
if ("null" == bookmark.bookmarkUrl) {
intent.putExtra(EXTRA_CHOSE_X_TITLE, bookmark.bookmarkTitle)
} else {
intent.putExtra(EXTRA_CHOSE_X_URL, bookmark.bookmarkUrl)
}
if (bookmark.zimFilePath != null && bookmark.zimFilePath != zimReaderContainer!!.zimCanonicalPath
) {
intent.putExtra(EXTRA_CHOSE_X_FILE, bookmark.zimFilePath)
}
setResult(Activity.RESULT_OK, intent)
finish()
} else {
toggleSelection(favicon, bookmark)
}
}
override fun onItemLongClick(
favicon: ImageView,
bookmark: BookmarkItem
): Boolean {
if (actionMode != null) {
return false
}
actionMode = startSupportActionMode(actionModeCallback)
refreshAdapter = true
toggleSelection(favicon, bookmark)
return true
}
private fun toggleSelection(
favicon: ImageView,
bookmark: BookmarkItem
) {
if (deleteList.remove(bookmark)) {
favicon.setBitmapFromString(bookmark.favicon)
} else {
favicon.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.ic_check_circle_blue_24dp)
)
deleteList.add(bookmark)
}
actionMode!!.title = getString(R.string.selected_items, deleteList.size)
if (deleteList.size == 0) {
actionMode!!.finish()
}
}
}

View File

@ -39,6 +39,7 @@ import org.kiwix.kiwixmobile.core.data.local.dao.BookmarksDao
import org.kiwix.kiwixmobile.core.data.remote.KiwixService
import org.kiwix.kiwixmobile.core.di.modules.ApplicationModule
import org.kiwix.kiwixmobile.core.di.modules.CoreViewModelModule
import org.kiwix.kiwixmobile.core.di.modules.DialogModule
import org.kiwix.kiwixmobile.core.di.modules.JNIModule
import org.kiwix.kiwixmobile.core.di.modules.NetworkModule
import org.kiwix.kiwixmobile.core.di.modules.SearchModule
@ -66,7 +67,8 @@ import javax.inject.Singleton
JNIModule::class,
DataModule::class,
CoreViewModelModule::class,
SearchModule::class
SearchModule::class,
DialogModule::class
]
)
interface CoreComponent {