Skip to content

Commit

Permalink
Save user preferences on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
usharik committed Mar 14, 2018
1 parent 70200e5 commit 29957ca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ protected void onPause() {
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
MenuItem item = menu.findItem(R.id.offlineMode);
item.setChecked(getViewModel().isOfflineMode());
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.offlineMode:
item.setChecked(!item.isChecked());
item.setChecked(!getViewModel().isOfflineMode());
getViewModel().setOfflineMode(item.isChecked());
updateTitle();
return true;
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/java/com/usharik/seznamslovnik/MainViewModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.usharik.seznamslovnik;

import android.content.ClipboardManager;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.databinding.Bindable;
import android.os.Vibrator;
Expand Down Expand Up @@ -39,6 +40,8 @@

public class MainViewModel extends ViewModelObservable {

private static final String OFFLINE_MODE_PREF_KEY = "isOffline";

private final AppState appState;
private final Retrofit retrofit;
private final TranslationService translationService;
Expand All @@ -47,6 +50,7 @@ public class MainViewModel extends ViewModelObservable {
private final Resources resources;
private final ClipboardManager clipboardManager;
private final Vibrator vibrator;
private final SharedPreferences sharedPreferences;

private String text;
private String word;
Expand All @@ -65,7 +69,8 @@ public MainViewModel(final AppState appState,
final PublishSubject<Action> executeActionSubject,
final Resources resources,
final ClipboardManager clipboardManager,
final Vibrator vibrator) {
final Vibrator vibrator,
final SharedPreferences sharedPreferences) {
this.appState = appState;
this.retrofit = retrofit;
this.translationService = translationService;
Expand All @@ -74,8 +79,10 @@ public MainViewModel(final AppState appState,
this.resources = resources;
this.clipboardManager = clipboardManager;
this.vibrator = vibrator;
this.sharedPreferences = sharedPreferences;
this.adapter = getEmptyAdapter();
this.scrollPosition = 0;
this.appState.isOfflineMode = this.sharedPreferences.getBoolean(OFFLINE_MODE_PREF_KEY, false);
}

@Bindable
Expand Down Expand Up @@ -220,4 +227,12 @@ private MyAdapter getEmptyAdapter() {
String langTo = LANG_ORDER_STR[toLanguageIx];
return new MyAdapter(Collections.EMPTY_LIST, translationService, clipboardManager, vibrator, executeActionSubject, resources, langFrom, langTo);
}

@Override
public void onCleared() {
super.onCleared();
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean(OFFLINE_MODE_PREF_KEY, appState.isOfflineMode);
edit.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Application;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Vibrator;

Expand Down Expand Up @@ -86,4 +87,10 @@ Vibrator provideVibrator(Application application) {
Resources provideResources(Application application) {
return application.getResources();
}

@Provides
@Singleton
SharedPreferences provideSharedPrefences(Application application) {
return application.getSharedPreferences("seznam_slovnik.prefences", Context.MODE_PRIVATE);
}
}

0 comments on commit 29957ca

Please sign in to comment.