mirror of
https://github.com/M66B/FairEmail.git
synced 2025-03-06 11:39:49 +00:00
Refactoring
This commit is contained in:
parent
8a74617b1f
commit
33d6a7d99c
2 changed files with 102 additions and 103 deletions
|
@ -33,13 +33,10 @@ import android.content.res.Configuration;
|
|||
import android.util.Printer;
|
||||
import android.webkit.CookieManager;
|
||||
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -107,7 +104,7 @@ public class ApplicationEx extends Application {
|
|||
|
||||
createNotificationChannels();
|
||||
|
||||
setupViewInvalidation();
|
||||
DB.setupViewInvalidation(this);
|
||||
|
||||
if (Helper.hasWebView(this))
|
||||
CookieManager.getInstance().setAcceptCookie(false);
|
||||
|
@ -344,88 +341,6 @@ public class ApplicationEx extends Application {
|
|||
}
|
||||
}
|
||||
|
||||
private void setupViewInvalidation() {
|
||||
DB db = DB.getInstance(this);
|
||||
|
||||
db.account().liveAccountView().observeForever(new Observer<List<TupleAccountView>>() {
|
||||
private List<TupleAccountView> last = null;
|
||||
|
||||
@Override
|
||||
public void onChanged(List<TupleAccountView> 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("message");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
db.identity().liveIdentityView().observeForever(new Observer<List<TupleIdentityView>>() {
|
||||
private List<TupleIdentityView> last = null;
|
||||
|
||||
@Override
|
||||
public void onChanged(List<TupleIdentityView> 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("message");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
db.folder().liveFolderView().observeForever(new Observer<List<TupleFolderView>>() {
|
||||
private List<TupleFolderView> last = null;
|
||||
|
||||
@Override
|
||||
public void onChanged(List<TupleFolderView> 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("message");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private BroadcastReceiver onScreenOff = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
|
|
@ -6,6 +6,7 @@ import android.database.Cursor;
|
|||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.room.Database;
|
||||
import androidx.room.DatabaseConfiguration;
|
||||
|
@ -105,23 +106,6 @@ public abstract class DB extends RoomDatabase {
|
|||
|
||||
public abstract DaoLog log();
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected InvalidationTracker createInvalidationTracker() {
|
||||
final HashMap<String, String> _shadowTablesMap = new HashMap<String, String>(0);
|
||||
HashMap<String, Set<String>> _viewTables = new HashMap<String, Set<String>>(3);
|
||||
HashSet<String> _tables = new HashSet<String>(1);
|
||||
//_tables.add("account");
|
||||
_viewTables.put("account_view", _tables);
|
||||
HashSet<String> _tables_1 = new HashSet<String>(1);
|
||||
//_tables_1.add("identity");
|
||||
_viewTables.put("identity_view", _tables_1);
|
||||
HashSet<String> _tables_2 = new HashSet<String>(1);
|
||||
//_tables_2.add("folder");
|
||||
_viewTables.put("folder_view", _tables_2);
|
||||
return new InvalidationTracker(this, _shadowTablesMap, _viewTables, DB_TABLES);
|
||||
}
|
||||
|
||||
private static DB sInstance;
|
||||
private static final ExecutorService executor =
|
||||
Helper.getBackgroundExecutor(1, "query");
|
||||
|
@ -150,6 +134,106 @@ public abstract class DB extends RoomDatabase {
|
|||
super.init(configuration);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected InvalidationTracker createInvalidationTracker() {
|
||||
final HashMap<String, String> _shadowTablesMap = new HashMap<String, String>(0);
|
||||
HashMap<String, Set<String>> _viewTables = new HashMap<String, Set<String>>(3);
|
||||
HashSet<String> _tables = new HashSet<String>(1);
|
||||
//_tables.add("account");
|
||||
_viewTables.put("account_view", _tables);
|
||||
HashSet<String> _tables_1 = new HashSet<String>(1);
|
||||
//_tables_1.add("identity");
|
||||
_viewTables.put("identity_view", _tables_1);
|
||||
HashSet<String> _tables_2 = new HashSet<String>(1);
|
||||
//_tables_2.add("folder");
|
||||
_viewTables.put("folder_view", _tables_2);
|
||||
return new InvalidationTracker(this, _shadowTablesMap, _viewTables, DB_TABLES);
|
||||
}
|
||||
|
||||
static void setupViewInvalidation(Context context) {
|
||||
// This needs to be done on a foreground thread
|
||||
DB db = DB.getInstance(context);
|
||||
|
||||
db.account().liveAccountView().observeForever(new Observer<List<TupleAccountView>>() {
|
||||
private List<TupleAccountView> last = null;
|
||||
|
||||
@Override
|
||||
public void onChanged(List<TupleAccountView> 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("message");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
db.identity().liveIdentityView().observeForever(new Observer<List<TupleIdentityView>>() {
|
||||
private List<TupleIdentityView> last = null;
|
||||
|
||||
@Override
|
||||
public void onChanged(List<TupleIdentityView> 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("message");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
db.folder().liveFolderView().observeForever(new Observer<List<TupleFolderView>>() {
|
||||
private List<TupleFolderView> last = null;
|
||||
|
||||
@Override
|
||||
public void onChanged(List<TupleFolderView> 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("message");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static synchronized DB getInstance(Context context) {
|
||||
if (sInstance == null) {
|
||||
Context acontext = context.getApplicationContext();
|
||||
|
|
Loading…
Add table
Reference in a new issue