1
0
mirror of https://github.com/etesync/android synced 2024-12-23 15:18:14 +00:00

Refresh ViewCollectionActivity when edited/deleted

Until now we were just closing it when opening a view that can cause a
change. This is nicer.
This commit is contained in:
Tom Hacohen 2017-03-23 21:36:16 +00:00
parent 0cd57851ad
commit 99fe3457fc
6 changed files with 83 additions and 58 deletions

View File

@ -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<AccountActivity.AccountInfo> {
public class AccountActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener, PopupMenu.OnMenuItemClickListener, LoaderManager.LoaderCallbacks<AccountActivity.AccountInfo>, 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);
}

View File

@ -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();
}
}

View File

@ -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<Persistable> data = ((App) getApplication()).getData();
int journalCount = data.count(JournalEntity.class).where(JournalEntity.SERVICE.eq(info.serviceID)).get().value();

View File

@ -205,6 +205,10 @@ public class ImportFragment extends DialogFragment {
.commitAllowingStateLoss();
dismissAllowingStateLoss();
if (getActivity() instanceof Refreshable) {
((Refreshable) getActivity()).refresh();
}
}
private class ImportCalendarsLoader implements Runnable {

View File

@ -0,0 +1,5 @@
package com.etesync.syncadapter.ui;
interface Refreshable {
public void refresh();
}

View File

@ -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<Persistable> 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<Persistable> 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<Persistable> 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) {