From 54641c417e0baa2eb7f1a9db67f23bc9a71a1c14 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 23 Jan 2020 08:46:38 +0100 Subject: [PATCH] Refactoring --- .../java/eu/faircode/email/ApplicationEx.java | 182 +++++++++--------- 1 file changed, 94 insertions(+), 88 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 20ff5e3568..b2dce793ec 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -46,6 +46,18 @@ public class ApplicationEx extends Application { super.attachBaseContext(getLocalizedContext(base)); } + static Context getLocalizedContext(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean english = prefs.getBoolean("english", false); + + if (english) { + Configuration config = new Configuration(context.getResources().getConfiguration()); + config.setLocale(Locale.US); + return context.createConfigurationContext(config); + } else + return context; + } + @Override public void onCreate() { super.onCreate(); @@ -82,6 +94,8 @@ public class ApplicationEx extends Application { createNotificationChannels(); + setupViewInvalidation(); + if (Helper.hasWebView(this)) CookieManager.getInstance().setAcceptCookie(false); @@ -91,85 +105,7 @@ public class ApplicationEx extends Application { WorkerWatchdog.init(this); WorkerCleanup.queue(this); - DB db = DB.getInstance(this); - - db.account().liveAccountView().observeForever(new Observer>() { - private List last = null; - - @Override - public void onChanged(List accounts) { - if (accounts == null) - accounts = new ArrayList<>(); - - boolean changed = false; - if (last == null || last.size() != accounts.size()) - changed = true; - else - for (int i = 0; i < accounts.size(); i++) - if (!accounts.get(i).equals(last.get(i))) { - changed = true; - last = accounts; - } - - if (changed) { - Log.i("Invalidating account view"); - last = accounts; - db.getInvalidationTracker().notifyObserversByTableNames("account_view"); - } - } - }); - - db.identity().liveIdentityView().observeForever(new Observer>() { - private List last = null; - - @Override - public void onChanged(List identities) { - if (identities == null) - identities = new ArrayList<>(); - - boolean changed = false; - if (last == null || last.size() != identities.size()) - changed = true; - else - for (int i = 0; i < identities.size(); i++) - if (!identities.get(i).equals(last.get(i))) { - changed = true; - last = identities; - } - - if (changed) { - Log.i("Invalidating identity view"); - last = identities; - db.getInvalidationTracker().notifyObserversByTableNames("identity_view"); - } - } - }); - - db.folder().liveFolderView().observeForever(new Observer>() { - private List last = null; - - @Override - public void onChanged(List folders) { - if (folders == null) - folders = new ArrayList<>(); - - boolean changed = false; - if (last == null || last.size() != folders.size()) - changed = true; - else - for (int i = 0; i < folders.size(); i++) - if (!folders.get(i).equals(last.get(i))) { - changed = true; - last = folders; - } - - if (changed) { - Log.i("Invalidating folder view"); - last = folders; - db.getInvalidationTracker().notifyObserversByTableNames("folder_view"); - } - } - }); + Log.i("App created"); } @Override @@ -375,15 +311,85 @@ public class ApplicationEx extends Application { } } - static Context getLocalizedContext(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - boolean english = prefs.getBoolean("english", false); + private void setupViewInvalidation() { + DB db = DB.getInstance(this); - if (english) { - Configuration config = new Configuration(context.getResources().getConfiguration()); - config.setLocale(Locale.US); - return context.createConfigurationContext(config); - } else - return context; + db.account().liveAccountView().observeForever(new Observer>() { + private List last = null; + + @Override + public void onChanged(List accounts) { + if (accounts == null) + accounts = new ArrayList<>(); + + boolean changed = false; + if (last == null || last.size() != accounts.size()) + changed = true; + else + for (int i = 0; i < accounts.size(); i++) + if (!accounts.get(i).equals(last.get(i))) { + changed = true; + last = accounts; + } + + if (changed) { + Log.i("Invalidating account view"); + last = accounts; + db.getInvalidationTracker().notifyObserversByTableNames("account_view"); + } + } + }); + + db.identity().liveIdentityView().observeForever(new Observer>() { + private List last = null; + + @Override + public void onChanged(List identities) { + if (identities == null) + identities = new ArrayList<>(); + + boolean changed = false; + if (last == null || last.size() != identities.size()) + changed = true; + else + for (int i = 0; i < identities.size(); i++) + if (!identities.get(i).equals(last.get(i))) { + changed = true; + last = identities; + } + + if (changed) { + Log.i("Invalidating identity view"); + last = identities; + db.getInvalidationTracker().notifyObserversByTableNames("identity_view"); + } + } + }); + + db.folder().liveFolderView().observeForever(new Observer>() { + private List last = null; + + @Override + public void onChanged(List folders) { + if (folders == null) + folders = new ArrayList<>(); + + boolean changed = false; + if (last == null || last.size() != folders.size()) + changed = true; + else + for (int i = 0; i < folders.size(); i++) + if (!folders.get(i).equals(last.get(i))) { + changed = true; + last = folders; + } + + if (changed) { + Log.i("Invalidating folder view"); + last = folders; + db.getInvalidationTracker().notifyObserversByTableNames("folder_view"); + } + } + }); } }