1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-12-22 19:08:03 +01:00

Merge AppSettings & PrefManager

This commit is contained in:
Gregor Santner 2016-03-24 11:58:28 +01:00
parent 809c4a8de7
commit 7f780ac3ce
3 changed files with 48 additions and 70 deletions

View file

@ -2,6 +2,7 @@ package de.baumann.diaspora;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
/**
* Created by de-live-gdev on 20.03.16. Part of Diaspora WebApp.
@ -9,23 +10,55 @@ import android.content.SharedPreferences;
class AppSettings {
private final SharedPreferences pref;
public AppSettings(Context context){
public AppSettings(Context context) {
Context context1 = context.getApplicationContext();
pref = context1.getSharedPreferences("app", Context.MODE_PRIVATE);
}
private void setString(String key, String value){
pref.edit().putString(key,value).apply();
private void setString(String key, String value) {
pref.edit().putString(key, value).apply();
}
private void setInt(String key, int value) {
pref.edit().putInt(key, value).apply();
}
private void setBool(String key, boolean value) {
pref.edit().putBoolean(key, value).apply();
}
/*
// Preferences
*/
private static final String PREF_PROFILE_ID = "profileID";
private static final String PREF_IS_LOAD_IMAGES = "loadImages";
private static final String PREF_MINIMUM_FONT_SIZE = "minimumFontSize";
/*
// Setters & Getters
*/
private static final String PREF_PROFILE_ID = "profileID";
public String getProfileId(){
public String getProfileId() {
return pref.getString(PREF_PROFILE_ID, "");
}
public void setProfileId(String profileId){
public void setProfileId(String profileId) {
setString(PREF_PROFILE_ID, profileId);
}
public boolean isLoadImages() {
return pref.getBoolean(PREF_IS_LOAD_IMAGES, true);
}
public void setLoadImages(boolean loadImages) {
setBool(PREF_IS_LOAD_IMAGES, loadImages);
}
public int getMinimumFontSize() {
return pref.getInt(PREF_MINIMUM_FONT_SIZE, 8);
}
public void setMinimumFontSize(int minimumFontSize) {
setInt(PREF_MINIMUM_FONT_SIZE, minimumFontSize);
}
}

View file

@ -75,7 +75,6 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import de.baumann.diaspora.utils.Helpers;
import de.baumann.diaspora.utils.PrefManager;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@ -85,7 +84,6 @@ public class MainActivity extends AppCompatActivity
private static final int REQUEST_CODE_ASK_PERMISSIONS = 123;
private static final String URL_MESSAGE = "URL_MESSAGE";
private AppSettings appSettings;
private final Handler myHandler = new Handler();
private WebView webView;
private String podDomain;
@ -98,7 +96,7 @@ public class MainActivity extends AppCompatActivity
private com.getbase.floatingactionbutton.FloatingActionsMenu fab;
private ProgressBar progressBar;
private WebSettings wSettings;
private PrefManager pm;
private AppSettings appSettings;
private SwipeRefreshLayout swipeView;
@SuppressLint("SetJavaScriptEnabled")
@ -140,7 +138,6 @@ public class MainActivity extends AppCompatActivity
navigationView.setNavigationItemSelectedListener(this);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
pm = new PrefManager(MainActivity.this);
SharedPreferences config = getSharedPreferences("PodSettings", MODE_PRIVATE);
podDomain = config.getString("podDomain", null);
@ -164,8 +161,8 @@ public class MainActivity extends AppCompatActivity
wSettings.setUseWideViewPort(true);
wSettings.setLoadWithOverviewMode(true);
wSettings.setDomStorageEnabled(true);
wSettings.setMinimumFontSize(pm.getMinimumFontSize());
wSettings.setLoadsImagesAutomatically(pm.getLoadImages());
wSettings.setMinimumFontSize(appSettings.getMinimumFontSize());
wSettings.setLoadsImagesAutomatically(appSettings.isLoadImages());
if (android.os.Build.VERSION.SDK_INT >= 21)
wSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
@ -667,14 +664,14 @@ public class MainActivity extends AppCompatActivity
.findViewById(selectedId);
if (selectedRadioButton.getId() == R.id.radNormal) {
pm.setMinimumFontSize(8);
appSettings.setMinimumFontSize(8);
} else if (selectedRadioButton.getId() == R.id.radLarge) {
pm.setMinimumFontSize(16);
appSettings.setMinimumFontSize(16);
} else if (selectedRadioButton.getId() == R.id.radLarger) {
pm.setMinimumFontSize(20);
appSettings.setMinimumFontSize(20);
}
wSettings.setMinimumFontSize(pm.getMinimumFontSize());
wSettings.setMinimumFontSize(appSettings.getMinimumFontSize());
if (Helpers.isOnline(MainActivity.this)) {
webView.loadUrl(webView.getUrl());
@ -878,8 +875,8 @@ public class MainActivity extends AppCompatActivity
if (options[item].equals(getString(R.string.settings_view)))
webView.loadUrl("https://" + podDomain + "/mobile/toggle");
if (options[item].equals(getString(R.string.settings_image)))
wSettings.setLoadsImagesAutomatically(!pm.getLoadImages());
pm.setLoadImages(!pm.getLoadImages());
wSettings.setLoadsImagesAutomatically(!appSettings.isLoadImages());
appSettings.setLoadImages(!appSettings.isLoadImages());
webView.loadUrl(webView.getUrl());
}
}).show();

View file

@ -1,52 +0,0 @@
package de.baumann.diaspora.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
public class PrefManager {
private final Context context;
private boolean loadImages = true;
private int minimumFontSize = 0;
public PrefManager(Context ctx) {
SharedPreferences sp = null;
this.context = ctx;
try {
sp = PreferenceManager.getDefaultSharedPreferences(context);
} catch (Exception e) {
e.printStackTrace();
}
if (sp != null) {
loadImages = sp.getBoolean("loadImages", true);
minimumFontSize = sp.getInt("minimumFontSize", 8);
}
}
public boolean getLoadImages() {
return loadImages;
}
public void setLoadImages(boolean loadImages) {
this.loadImages = loadImages;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
Editor edit = sp.edit();
edit.putBoolean("loadImages", loadImages);
edit.apply();
}
public int getMinimumFontSize() {
return minimumFontSize;
}
public void setMinimumFontSize(int minimumFontSize) {
this.minimumFontSize = minimumFontSize;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
Editor edit = sp.edit();
edit.putInt("minimumFontSize", minimumFontSize);
edit.apply();
}
}