Add clear history option

This commit is contained in:
Abdul Wadood 2018-07-08 04:54:42 +05:30 committed by Isaac Hutt
parent a086bb74ec
commit 0947fe9acc
8 changed files with 102 additions and 22 deletions

View File

@ -25,4 +25,6 @@ public interface DataSource {
Completable saveHistory(History history);
Completable deleteHistory(List<History> historyList);
Completable clearHistory();
}

View File

@ -3,6 +3,7 @@ package org.kiwix.kiwixmobile.data;
import org.kiwix.kiwixmobile.data.local.dao.BookDao;
import org.kiwix.kiwixmobile.data.local.dao.HistoryDao;
import org.kiwix.kiwixmobile.data.local.dao.NetworkLanguageDao;
import org.kiwix.kiwixmobile.data.local.dao.RecentSearchDao;
import org.kiwix.kiwixmobile.data.local.entity.History;
import org.kiwix.kiwixmobile.di.qualifiers.IO;
import org.kiwix.kiwixmobile.di.qualifiers.MainThread;
@ -29,19 +30,24 @@ import io.reactivex.Single;
public class Repository implements DataSource {
private final BookDao bookDao;
private final NetworkLanguageDao languageDao;
private final HistoryDao historyDao;
private final NetworkLanguageDao languageDao;
private final RecentSearchDao recentSearchDao;
private final Scheduler io;
private final Scheduler mainThread;
@Inject
Repository(@IO Scheduler io, @MainThread Scheduler mainThread,
BookDao bookDao, NetworkLanguageDao languageDao, HistoryDao historyDao) {
BookDao bookDao,
HistoryDao historyDao,
NetworkLanguageDao languageDao,
RecentSearchDao recentSearchDao) {
this.io = io;
this.mainThread = mainThread;
this.bookDao = bookDao;
this.languageDao = languageDao;
this.historyDao = historyDao;
this.recentSearchDao = recentSearchDao;
}
@Override
@ -115,4 +121,13 @@ public class Repository implements DataSource {
return Completable.fromAction(() -> historyDao.deleteHistory(historyList))
.subscribeOn(io);
}
@Override
public Completable clearHistory() {
return Completable.fromAction(() -> {
historyDao.deleteHistory(historyDao.getHistoryList(false));
recentSearchDao.deleteSearchHistory();
})
.subscribeOn(io);
}
}

View File

@ -67,7 +67,9 @@ public class HistoryDao {
public void deleteHistory(List<History> historyList) {
for (History history : historyList) {
kiwixDatabase.deleteWhere(History.class, History.TIME_STAMP.eq(history.getTimeStamp()));
if (history != null) {
kiwixDatabase.deleteWhere(History.class, History.TIME_STAMP.eq(history.getTimeStamp()));
}
}
}
}

View File

@ -13,6 +13,7 @@ import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.base.BaseActivity;
@ -158,11 +159,20 @@ public class HistoryActivity extends BaseActivity implements HistoryContract.Vie
case android.R.id.home:
onBackPressed();
return true;
case R.id.menu_history_toggle:
item.setChecked(!item.isChecked());
sharedPreferenceUtil.setShowHistoryCurrentBook(item.isChecked());
presenter.loadHistory(sharedPreferenceUtil.getShowHistoryCurrentBook());
return true;
case R.id.menu_history_clear:
presenter.deleteHistory(new ArrayList<>(fullHistory));
fullHistory.clear();
historyList.clear();
historyAdapter.notifyDataSetChanged();
Toast.makeText(this, R.string.all_history_cleared_toast, Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}

View File

@ -41,7 +41,6 @@ import org.kiwix.kiwixmobile.BuildConfig;
import org.kiwix.kiwixmobile.KiwixApplication;
import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.base.BaseActivity;
import org.kiwix.kiwixmobile.data.local.dao.RecentSearchDao;
import org.kiwix.kiwixmobile.main.MainActivity;
import org.kiwix.kiwixmobile.utils.LanguageUtils;
import org.kiwix.kiwixmobile.utils.SharedPreferenceUtil;
@ -115,23 +114,20 @@ public class KiwixSettingsActivity extends BaseActivity {
}
public static class PrefsFragment extends PreferenceFragment implements
SettingsContract.View,
SharedPreferences.OnSharedPreferenceChangeListener, StorageSelectDialog.OnSelectListener {
@Inject
SettingsPresenter presenter;
@Inject
SharedPreferenceUtil sharedPreferenceUtil;
private SliderPreference mSlider;
@Inject
RecentSearchDao recentSearchDao;
@Inject SharedPreferenceUtil sharedPreferenceUtil;
void setupDagger() {
KiwixApplication.getApplicationComponent().inject(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
KiwixApplication.getApplicationComponent().inject(this);
super.onCreate(savedInstanceState);
setupDagger();
addPreferencesFromResource(R.xml.preferences);
boolean auto_night_mode = sharedPreferenceUtil.getPrefAutoNightMode();
@ -158,10 +154,6 @@ public class KiwixSettingsActivity extends BaseActivity {
new LanguageUtils(getActivity()).changeFont(getActivity().getLayoutInflater(), sharedPreferenceUtil);
}
private void deleteSearchHistoryFromDb() {
recentSearchDao.deleteSearchHistory();
}
private void setStorage() {
if (BuildConfig.IS_CUSTOM_APP) {
getPreferenceScreen().removePreference(findPreference("pref_storage"));
@ -274,15 +266,14 @@ public class KiwixSettingsActivity extends BaseActivity {
int warningResId;
if (sharedPreferenceUtil.nightMode()) {
warningResId = R.drawable.ic_warning_white;
}
else {
} else {
warningResId = R.drawable.ic_warning_black;
}
new AlertDialog.Builder(getActivity(), dialogStyle())
.setTitle(getResources().getString(R.string.clear_all_history_dialog_title))
.setMessage(getResources().getString(R.string.clear_recent_and_tabs_history_dialog))
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
deleteSearchHistoryFromDb();
presenter.clearHistory();
allHistoryCleared = true;
Toast.makeText(getActivity(), getResources().getString(R.string.all_history_cleared_toast), Toast.LENGTH_SHORT).show();
})
@ -321,7 +312,7 @@ public class KiwixSettingsActivity extends BaseActivity {
return true;
}
public void openFolderSelect(){
public void openFolderSelect() {
FragmentManager fm = getFragmentManager();
StorageSelectDialog dialogFragment = new StorageSelectDialog();
Bundle b = new Bundle();

View File

@ -0,0 +1,13 @@
package org.kiwix.kiwixmobile.settings;
import org.kiwix.kiwixmobile.base.BaseContract;
interface SettingsContract {
interface View extends BaseContract.View<Presenter> {
}
interface Presenter extends BaseContract.Presenter<View> {
void clearHistory();
}
}

View File

@ -0,0 +1,42 @@
package org.kiwix.kiwixmobile.settings;
import android.util.Log;
import org.kiwix.kiwixmobile.base.BasePresenter;
import org.kiwix.kiwixmobile.data.DataSource;
import javax.inject.Inject;
import io.reactivex.CompletableObserver;
import io.reactivex.disposables.Disposable;
class SettingsPresenter extends BasePresenter<SettingsContract.View> implements SettingsContract.Presenter {
private final DataSource dataSource;
@Inject
SettingsPresenter(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void clearHistory() {
dataSource.clearHistory()
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
Log.e("SettingsPresenter", e.toString());
}
});
}
}

View File

@ -15,4 +15,9 @@
android:checkable="true"
android:title="@string/history_from_current_book"
app:showAsAction="never" />
<item
android:id="@+id/menu_history_clear"
android:title="@string/pref_clear_all_history_title"
app:showAsAction="never" />
</menu>