From 99fe3457fcf33bea3e4d54faaa86da23478571b3 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 23 Mar 2017 21:36:16 +0000 Subject: [PATCH] Refresh ViewCollectionActivity when edited/deleted Until now we were just closing it when opening a view that can cause a change. This is nicer. --- .../syncadapter/ui/AccountActivity.java | 5 +- .../ui/DeleteCollectionFragment.java | 6 +- .../ui/EditCollectionActivity.java | 9 ++ .../syncadapter/ui/ImportFragment.java | 4 + .../etesync/syncadapter/ui/Refreshable.java | 5 + .../ui/ViewCollectionActivity.java | 112 +++++++++--------- 6 files changed, 83 insertions(+), 58 deletions(-) create mode 100644 app/src/main/java/com/etesync/syncadapter/ui/Refreshable.java diff --git a/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.java b/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.java index ca6d390a..aeef8364 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/AccountActivity.java @@ -72,7 +72,7 @@ import lombok.Cleanup; import static android.content.ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE; -public class AccountActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener, PopupMenu.OnMenuItemClickListener, LoaderManager.LoaderCallbacks { +public class AccountActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener, PopupMenu.OnMenuItemClickListener, LoaderManager.LoaderCallbacks, Refreshable { public static final String EXTRA_ACCOUNT = "account"; private Account account; @@ -202,7 +202,8 @@ public class AccountActivity extends AppCompatActivity implements Toolbar.OnMenu return new AccountLoader(this, account); } - public void reload() { + @Override + public void refresh() { getLoaderManager().restartLoader(0, getIntent().getExtras(), this); } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/DeleteCollectionFragment.java b/app/src/main/java/com/etesync/syncadapter/ui/DeleteCollectionFragment.java index 4e76adc1..48ddf73b 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/DeleteCollectionFragment.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/DeleteCollectionFragment.java @@ -82,9 +82,9 @@ public class DeleteCollectionFragment extends DialogFragment implements LoaderMa .commitAllowingStateLoss(); else { Activity activity = getActivity(); - if (activity instanceof AccountActivity) - ((AccountActivity) activity).reload(); - else if (activity instanceof CreateCollectionActivity) + if (activity instanceof Refreshable) + ((Refreshable) activity).refresh(); + else if (activity instanceof EditCollectionActivity) activity.finish(); } } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/EditCollectionActivity.java b/app/src/main/java/com/etesync/syncadapter/ui/EditCollectionActivity.java index ded3d778..ac96e87e 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/EditCollectionActivity.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/EditCollectionActivity.java @@ -67,6 +67,15 @@ public class EditCollectionActivity extends CreateCollectionActivity { return true; } + @Override + protected void onDestroy() { + super.onDestroy(); + + if (getParent() instanceof Refreshable) { + ((Refreshable) getParent()).refresh(); + } + } + public void onDeleteCollection(MenuItem item) { EntityDataStore data = ((App) getApplication()).getData(); int journalCount = data.count(JournalEntity.class).where(JournalEntity.SERVICE.eq(info.serviceID)).get().value(); diff --git a/app/src/main/java/com/etesync/syncadapter/ui/ImportFragment.java b/app/src/main/java/com/etesync/syncadapter/ui/ImportFragment.java index 6ef32c09..72042f40 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/ImportFragment.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/ImportFragment.java @@ -205,6 +205,10 @@ public class ImportFragment extends DialogFragment { .commitAllowingStateLoss(); dismissAllowingStateLoss(); + + if (getActivity() instanceof Refreshable) { + ((Refreshable) getActivity()).refresh(); + } } private class ImportCalendarsLoader implements Runnable { diff --git a/app/src/main/java/com/etesync/syncadapter/ui/Refreshable.java b/app/src/main/java/com/etesync/syncadapter/ui/Refreshable.java new file mode 100644 index 00000000..8301aa74 --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/ui/Refreshable.java @@ -0,0 +1,5 @@ +package com.etesync.syncadapter.ui; + +interface Refreshable { + public void refresh(); +} diff --git a/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.java b/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.java index da43127a..8fac74b8 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.java @@ -36,7 +36,7 @@ import at.bitfire.vcard4android.ContactsStorageException; import io.requery.Persistable; import io.requery.sql.EntityDataStore; -public class ViewCollectionActivity extends AppCompatActivity { +public class ViewCollectionActivity extends AppCompatActivity implements Refreshable { public final static String EXTRA_ACCOUNT = "account", EXTRA_COLLECTION_INFO = "collectionInfo"; @@ -50,6 +50,61 @@ public class ViewCollectionActivity extends AppCompatActivity { return intent; } + @Override + public void refresh() { + EntityDataStore data = ((App) getApplicationContext()).getData(); + int entryCount = -1; + + final JournalEntity journalEntity = JournalEntity.fetch(data, info.url); + if (journalEntity == null) { + finish(); + return; + } + + info = journalEntity.getInfo(); + + entryCount = data.count(EntryEntity.class).where(EntryEntity.JOURNAL.eq(journalEntity)).get().value(); + + + final TextView stats = (TextView) findViewById(R.id.stats); + + final View colorSquare = findViewById(R.id.color); + if (info.type == CollectionInfo.Type.CALENDAR) { + if (info.color != null) { + colorSquare.setBackgroundColor(info.color); + } else { + colorSquare.setBackgroundColor(LocalCalendar.defaultColor); + } + + try { + LocalCalendar resource = (LocalCalendar) LocalCalendar.find(account, this.getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI), + LocalCalendar.Factory.INSTANCE, CalendarContract.Calendars.NAME + "=?", new String[]{info.url})[0]; + long count = resource.count(); + stats.setText(String.format(Locale.getDefault(), "Events: %d, Journal entries: %d", count, entryCount)); + } catch (CalendarStorageException e) { + e.printStackTrace(); + stats.setText("Stats loading error."); + } + } else { + colorSquare.setVisibility(View.GONE); + + try { + LocalAddressBook resource = new LocalAddressBook(account, this.getContentResolver().acquireContentProviderClient(ContactsContract.Contacts.CONTENT_URI)); + long count = resource.count(); + stats.setText(String.format(Locale.getDefault(), "Contacts: %d, Journal Entries: %d", count, entryCount)); + } catch (ContactsStorageException e) { + e.printStackTrace(); + stats.setText("Stats loading error."); + } + } + + final TextView title = (TextView) findViewById(R.id.display_name); + title.setText(info.displayName); + + final TextView desc = (TextView) findViewById(R.id.description); + desc.setText(info.description); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -67,56 +122,7 @@ public class ViewCollectionActivity extends AppCompatActivity { .commit(); } - - final TextView stats = (TextView) findViewById(R.id.stats); - - final View colorSquare = findViewById(R.id.color); - if (info.type == CollectionInfo.Type.CALENDAR) { - if (info.color != null) { - colorSquare.setBackgroundColor(info.color); - } else { - colorSquare.setBackgroundColor(LocalCalendar.defaultColor); - } - - try { - LocalCalendar resource = (LocalCalendar) LocalCalendar.find(account, this.getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI), - LocalCalendar.Factory.INSTANCE, CalendarContract.Calendars.NAME + "=?", new String[]{info.url})[0]; - long count = resource.count(); - EntityDataStore data = ((App) getApplication()).getData(); - int entryCount = -1; - final JournalEntity journalEntity = data.select(JournalEntity.class).where(JournalEntity.UID.eq(info.url)).limit(1).get().firstOrNull(); - if (journalEntity != null) { - entryCount = data.count(EntryEntity.class).where(EntryEntity.JOURNAL.eq(journalEntity)).get().value(); - } - stats.setText(String.format(Locale.getDefault(), "Events: %d, Journal entries: %d", count, entryCount)); - } catch (CalendarStorageException e) { - e.printStackTrace(); - stats.setText("Stats loading error."); - } - } else { - colorSquare.setVisibility(View.GONE); - - try { - LocalAddressBook resource = new LocalAddressBook(account, this.getContentResolver().acquireContentProviderClient(ContactsContract.Contacts.CONTENT_URI)); - long count = resource.count(); - EntityDataStore data = ((App) getApplication()).getData(); - int entryCount = -1; - final JournalEntity journalEntity = data.select(JournalEntity.class).where(JournalEntity.UID.eq(info.url)).limit(1).get().firstOrNull(); - if (journalEntity != null) { - entryCount = data.count(EntryEntity.class).where(EntryEntity.JOURNAL.eq(journalEntity)).get().value(); - }; - stats.setText(String.format(Locale.getDefault(), "Contacts: %d, Journal Entries: %d", count, entryCount)); - } catch (ContactsStorageException e) { - e.printStackTrace(); - stats.setText("Stats loading error."); - } - } - - final TextView title = (TextView) findViewById(R.id.display_name); - title.setText(info.displayName); - - final TextView desc = (TextView) findViewById(R.id.description); - desc.setText(info.description); + refresh(); } @Override @@ -143,6 +149,8 @@ public class ViewCollectionActivity extends AppCompatActivity { App app = (App) getApplicationContext(); if (app.getCertManager() != null) app.getCertManager().appInForeground = true; + + refresh(); } @Override @@ -156,8 +164,6 @@ public class ViewCollectionActivity extends AppCompatActivity { public void onEditCollection(MenuItem item) { startActivity(EditCollectionActivity.newIntent(this, account, info)); - // FIXME: Handle it more gracefully - finish(); } public void onImport(MenuItem item) {