Refactoring

pull/146/head
M66B 6 years ago
parent b9fef727af
commit 884615a269

@ -38,9 +38,6 @@ public interface DaoAnswer {
@Query("SELECT * FROM answer") @Query("SELECT * FROM answer")
LiveData<List<EntityAnswer>> liveAnswers(); LiveData<List<EntityAnswer>> liveAnswers();
@Query("SELECT * FROM answer WHERE id = :id")
LiveData<EntityAnswer> liveAnswer(long id);
@Insert @Insert
long insertAnswer(EntityAnswer answer); long insertAnswer(EntityAnswer answer);

@ -72,9 +72,6 @@ public interface DaoFolder {
" GROUP BY folder.id") " GROUP BY folder.id")
LiveData<List<TupleFolderEx>> liveUnified(); LiveData<List<TupleFolderEx>> liveUnified();
@Query("SELECT folder.* FROM folder WHERE folder.id = :id")
LiveData<EntityFolder> liveFolder(long id);
@Query("SELECT folder.*, account.name AS accountName" + @Query("SELECT folder.*, account.name AS accountName" +
", COUNT(message.id) AS messages" + ", COUNT(message.id) AS messages" +
", SUM(CASE WHEN message.content = 1 THEN 1 ELSE 0 END) AS content" + ", SUM(CASE WHEN message.content = 1 THEN 1 ELSE 0 END) AS content" +

@ -52,9 +52,6 @@ public interface DaoIdentity {
@Query("SELECT * FROM identity WHERE account = :account AND email = :email") @Query("SELECT * FROM identity WHERE account = :account AND email = :email")
EntityIdentity getIdentity(long account, String email); EntityIdentity getIdentity(long account, String email);
@Query("SELECT * FROM identity WHERE id = :id")
LiveData<EntityIdentity> liveIdentity(long id);
@Query("SELECT COUNT(*) FROM identity WHERE synchronize") @Query("SELECT COUNT(*) FROM identity WHERE synchronize")
int getSynchronizingIdentityCount(); int getSynchronizingIdentityCount();

@ -87,7 +87,6 @@ import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import static android.accounts.AccountManager.newChooseAccountIntent; import static android.accounts.AccountManager.newChooseAccountIntent;
@ -899,18 +898,18 @@ public class FragmentAccount extends FragmentEx {
public void onActivityCreated(@Nullable final Bundle savedInstanceState) { public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
final DB db = DB.getInstance(getContext()); Bundle args = new Bundle();
args.putLong("id", id);
// Observe
db.account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() {
private boolean once = false;
new SimpleTask<EntityAccount>() {
@Override @Override
public void onChanged(@Nullable EntityAccount account) { protected EntityAccount onLoad(Context context, Bundle args) throws Throwable {
if (once) long id = args.getLong("id");
return; return DB.getInstance(context).account().getAccount(id);
once = true; }
@Override
protected void onLoaded(Bundle args, EntityAccount account) {
// Get providers // Get providers
List<Provider> providers = Provider.loadProfiles(getContext()); List<Provider> providers = Provider.loadProfiles(getContext());
providers.add(0, new Provider(getString(R.string.title_select))); providers.add(0, new Provider(getString(R.string.title_select)));
@ -986,24 +985,24 @@ public class FragmentAccount extends FragmentEx {
ibDelete.setVisibility(account == null ? View.GONE : View.VISIBLE); ibDelete.setVisibility(account == null ? View.GONE : View.VISIBLE);
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
if (account != null) { args.putLong("account", account == null ? -1 : account.id);
db.folder().liveFolders(account.id).observe(getViewLifecycleOwner(), new Observer<List<TupleFolderEx>>() {
@Override new SimpleTask<List<EntityFolder>>() {
public void onChanged(final List<TupleFolderEx> _folders) { @Override
new Handler().post(new Runnable() { protected List<EntityFolder> onLoad(Context context, Bundle args) throws Throwable {
@Override long account = args.getLong("account");
public void run() { return DB.getInstance(context).folder().getFolders(account);
List<EntityFolder> folders = new ArrayList<>(); }
if (_folders != null)
folders.addAll(_folders); @Override
setFolders(folders); protected void onLoaded(Bundle args, List<EntityFolder> folders) {
} if (folders == null)
}); folders = new ArrayList<>();
} setFolders(folders);
}); }
} }.load(FragmentAccount.this, args);
} }
}); }.load(this, args);
} }
private void selectAccount() { private void selectAccount() {

@ -36,7 +36,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;
public class FragmentAnswer extends FragmentEx { public class FragmentAnswer extends FragmentEx {
private ViewGroup view; private ViewGroup view;
@ -104,9 +103,18 @@ public class FragmentAnswer extends FragmentEx {
public void onActivityCreated(@Nullable final Bundle savedInstanceState) { public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
DB.getInstance(getContext()).answer().liveAnswer(id).observe(getViewLifecycleOwner(), new Observer<EntityAnswer>() { Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<EntityAnswer>() {
@Override @Override
public void onChanged(EntityAnswer answer) { protected EntityAnswer onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
return DB.getInstance(context).answer().getAnswer(id);
}
@Override
protected void onLoaded(Bundle args, EntityAnswer answer) {
etName.setText(answer == null ? null : answer.name); etName.setText(answer == null ? null : answer.name);
etText.setText(answer == null ? null : Html.fromHtml(answer.text)); etText.setText(answer == null ? null : Html.fromHtml(answer.text));
bottom_navigation.findViewById(R.id.action_delete).setVisibility(answer == null ? View.GONE : View.VISIBLE); bottom_navigation.findViewById(R.id.action_delete).setVisibility(answer == null ? View.GONE : View.VISIBLE);
@ -114,7 +122,7 @@ public class FragmentAnswer extends FragmentEx {
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE); grpReady.setVisibility(View.VISIBLE);
} }
}); }.load(this, args);
} }
private void onActionTrash() { private void onActionTrash() {

@ -44,7 +44,6 @@ import javax.mail.Session;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
public class FragmentFolder extends FragmentEx { public class FragmentFolder extends FragmentEx {
private ViewGroup view; private ViewGroup view;
@ -310,16 +309,18 @@ public class FragmentFolder extends FragmentEx {
public void onActivityCreated(@Nullable final Bundle savedInstanceState) { public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
// Observe Bundle args = new Bundle();
DB.getInstance(getContext()).folder().liveFolder(id).observe(getViewLifecycleOwner(), new Observer<EntityFolder>() { args.putLong("id", id);
private boolean once = false;
new SimpleTask<EntityFolder>() {
@Override @Override
public void onChanged(@Nullable final EntityFolder folder) { protected EntityFolder onLoad(Context context, Bundle args) throws Throwable {
if (once) long id = args.getLong("id");
return; return DB.getInstance(context).folder().getFolder(id);
once = true; }
@Override
protected void onLoaded(Bundle args, EntityFolder folder) {
if (savedInstanceState == null) { if (savedInstanceState == null) {
etRename.setText(folder == null ? null : folder.name); etRename.setText(folder == null ? null : folder.name);
etDisplay.setText(folder == null ? null : (folder.display == null ? folder.name : folder.display)); etDisplay.setText(folder == null ? null : (folder.display == null ? folder.name : folder.display));
@ -338,6 +339,6 @@ public class FragmentFolder extends FragmentEx {
btnSave.setEnabled(true); btnSave.setEnabled(true);
ibDelete.setVisibility(folder == null || !EntityFolder.USER.equals(folder.type) ? View.GONE : View.VISIBLE); ibDelete.setVisibility(folder == null || !EntityFolder.USER.equals(folder.type) ? View.GONE : View.VISIBLE);
} }
}); }.load(this, args);
} }
} }

@ -72,7 +72,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
public class FragmentIdentity extends FragmentEx { public class FragmentIdentity extends FragmentEx {
private ViewGroup view; private ViewGroup view;
@ -609,16 +608,18 @@ public class FragmentIdentity extends FragmentEx {
final DB db = DB.getInstance(getContext()); final DB db = DB.getInstance(getContext());
// Observe identity Bundle args = new Bundle();
db.identity().liveIdentity(id).observe(getViewLifecycleOwner(), new Observer<EntityIdentity>() { args.putLong("id", id);
private boolean once = false;
new SimpleTask<EntityIdentity>() {
@Override @Override
public void onChanged(@Nullable final EntityIdentity identity) { protected EntityIdentity onLoad(Context context, Bundle args) throws Throwable {
if (once) long id = args.getLong("id");
return; return DB.getInstance(context).identity().getIdentity(id);
once = true; }
@Override
protected void onLoaded(Bundle args, final EntityIdentity identity) {
if (savedInstanceState == null) { if (savedInstanceState == null) {
etName.setText(identity == null ? null : identity.name); etName.setText(identity == null ? null : identity.name);
etEmail.setText(identity == null ? null : identity.email); etEmail.setText(identity == null ? null : identity.email);
@ -674,16 +675,14 @@ public class FragmentIdentity extends FragmentEx {
ibDelete.setVisibility(identity == null ? View.GONE : View.VISIBLE); ibDelete.setVisibility(identity == null ? View.GONE : View.VISIBLE);
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
db.account().liveAccounts().removeObservers(getViewLifecycleOwner()); new SimpleTask<List<EntityAccount>>() {
db.account().liveAccounts().observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
private boolean once = false;
@Override @Override
public void onChanged(List<EntityAccount> accounts) { protected List<EntityAccount> onLoad(Context context, Bundle args) throws Throwable {
if (once) return DB.getInstance(context).account().getAccounts();
return; }
once = true;
@Override
protected void onLoaded(Bundle args, List<EntityAccount> accounts) {
if (accounts == null) if (accounts == null)
accounts = new ArrayList<>(); accounts = new ArrayList<>();
@ -742,9 +741,9 @@ public class FragmentIdentity extends FragmentEx {
spAccount.setSelection(account); spAccount.setSelection(account);
} }
} }
}); }.load(FragmentIdentity.this, args);
} }
}); }.load(this, args);
} }
private void setColor(int color) { private void setColor(int color) {

Loading…
Cancel
Save