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 bd7621b5..15165182 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.java @@ -11,10 +11,12 @@ package com.etesync.syncadapter.ui; import android.accounts.Account; import android.content.Context; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.provider.CalendarContract; import android.provider.ContactsContract; import android.support.v7.app.AppCompatActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -32,12 +34,15 @@ import com.etesync.syncadapter.ui.journalviewer.ListEntriesFragment; import java.io.FileNotFoundException; import java.util.Locale; +import java.util.Objects; import at.bitfire.ical4android.CalendarStorageException; import at.bitfire.vcard4android.ContactsStorageException; import io.requery.Persistable; import io.requery.sql.EntityDataStore; +import static com.etesync.syncadapter.R.id.stats; + public class ViewCollectionActivity extends AppCompatActivity implements Refreshable { public final static String EXTRA_ACCOUNT = "account", EXTRA_COLLECTION_INFO = "collectionInfo"; @@ -55,7 +60,6 @@ public class ViewCollectionActivity extends AppCompatActivity implements Refresh @Override public void refresh() { EntityDataStore data = ((App) getApplicationContext()).getData(); - int entryCount = -1; final JournalEntity journalEntity = JournalEntity.fetch(data, info.url); if ((journalEntity == null) || journalEntity.isDeleted()) { @@ -65,11 +69,6 @@ public class ViewCollectionActivity extends AppCompatActivity implements Refresh 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) { @@ -77,28 +76,12 @@ public class ViewCollectionActivity extends AppCompatActivity implements Refresh } else { colorSquare.setBackgroundColor(LocalCalendar.defaultColor); } - - try { - LocalCalendar resource = LocalCalendar.findByName(account, getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI), LocalCalendar.Factory.INSTANCE, info.url); - long count = resource.count(); - stats.setText(String.format(Locale.getDefault(), "Events: %d, Journal entries: %d", count, entryCount)); - } catch (FileNotFoundException|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."); - } } + new LoadCountTask().execute(); + final TextView title = (TextView) findViewById(R.id.display_name); title.setText(info.displayName); @@ -170,4 +153,55 @@ public class ViewCollectionActivity extends AppCompatActivity implements Refresh public void onImport(MenuItem item) { startActivity(ImportActivity.newIntent(ViewCollectionActivity.this, account, info)); } + + private class LoadCountTask extends AsyncTask { + private int entryCount; + + @Override + protected Long doInBackground(Void... aVoids) { + EntityDataStore data = ((App) getApplicationContext()).getData(); + + final JournalEntity journalEntity = JournalEntity.fetch(data, info.url); + + entryCount = data.count(EntryEntity.class).where(EntryEntity.JOURNAL.eq(journalEntity)).get().value(); + long count; + + if (info.type == CollectionInfo.Type.CALENDAR) { + try { + LocalCalendar resource = LocalCalendar.findByName(account, getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI), LocalCalendar.Factory.INSTANCE, info.url); + count = resource.count(); + } catch (FileNotFoundException | CalendarStorageException e) { + e.printStackTrace(); + return null; + } + } else { + try { + LocalAddressBook resource = new LocalAddressBook(account, getContentResolver().acquireContentProviderClient(ContactsContract.Contacts.CONTENT_URI)); + count = resource.count(); + } catch (ContactsStorageException e) { + e.printStackTrace(); + return null; + } + } + return count; + } + + @Override + protected void onPostExecute(Long result) { + final TextView stats = (TextView) findViewById(R.id.stats); + findViewById(R.id.progressBar).setVisibility(View.GONE); + + if (result == null) { + stats.setText("Stats loading error."); + } else { + if (info.type == CollectionInfo.Type.CALENDAR) { + stats.setText(String.format(Locale.getDefault(), "Events: %d, Journal entries: %d", + result, entryCount)); + } else { + stats.setText(String.format(Locale.getDefault(), "Contacts: %d, Journal Entries: %d", + result, entryCount)); + } + } + } + } } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/journalviewer/ListEntriesFragment.java b/app/src/main/java/com/etesync/syncadapter/ui/journalviewer/ListEntriesFragment.java index 21b53bbe..975e6242 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/journalviewer/ListEntriesFragment.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/journalviewer/ListEntriesFragment.java @@ -9,6 +9,7 @@ package com.etesync.syncadapter.ui.journalviewer; import android.content.Context; +import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.ListFragment; @@ -27,6 +28,7 @@ import com.etesync.syncadapter.model.EntryEntity; import com.etesync.syncadapter.model.JournalEntity; import com.etesync.syncadapter.model.JournalModel; +import java.util.List; import java.util.Locale; import io.requery.Persistable; @@ -36,8 +38,11 @@ public class ListEntriesFragment extends ListFragment implements AdapterView.OnI protected static final String EXTRA_COLLECTION_INFO = "collectionInfo"; private EntityDataStore data; + private CollectionInfo info; private JournalEntity journalEntity; + private TextView emptyTextView; + public static ListEntriesFragment newInstance(CollectionInfo info) { ListEntriesFragment frag = new ListEntriesFragment(); Bundle args = new Bundle(1); @@ -50,24 +55,25 @@ public class ListEntriesFragment extends ListFragment implements AdapterView.OnI public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); data = ((App) getContext().getApplicationContext()).getData(); - CollectionInfo info = (CollectionInfo) getArguments().getSerializable(EXTRA_COLLECTION_INFO); - journalEntity = JournalModel.Journal.fetch(data, info.url); + info = (CollectionInfo) getArguments().getSerializable(EXTRA_COLLECTION_INFO); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - getActivity().setTitle(journalEntity.getInfo().displayName); - return inflater.inflate(R.layout.journal_viewer_list, container, false); + getActivity().setTitle(info.displayName); + View view = inflater.inflate(R.layout.journal_viewer_list, container, false); + + //This is instead of setEmptyText() function because of Google bug + //See: https://code.google.com/p/android/issues/detail?id=21742 + emptyTextView = (TextView) view.findViewById(android.R.id.empty); + return view; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - EntriesListAdapter listAdapter = new EntriesListAdapter(getContext()); - setListAdapter(listAdapter); - - listAdapter.addAll(data.select(EntryEntity.class).where(EntryEntity.JOURNAL.eq(journalEntity)).orderBy(EntryEntity.ID.desc()).get().toList()); + new JournalFetch().execute(); getListView().setOnItemClickListener(this); } @@ -126,4 +132,23 @@ public class ListEntriesFragment extends ListFragment implements AdapterView.OnI return v; } } + + private class JournalFetch extends AsyncTask> { + + @Override + protected List doInBackground(Void... voids) { + journalEntity = JournalModel.Journal.fetch(data, info.url); + return data.select(EntryEntity.class).where(EntryEntity.JOURNAL.eq(journalEntity)).orderBy(EntryEntity.ID.desc()).get().toList(); + } + + @Override + protected void onPostExecute(List result) { + EntriesListAdapter listAdapter = new EntriesListAdapter(getContext()); + setListAdapter(listAdapter); + + listAdapter.addAll(result); + + emptyTextView.setText(getString(R.string.journal_entries_list_empty)); + } + } } diff --git a/app/src/main/res/layout/journal_viewer_list.xml b/app/src/main/res/layout/journal_viewer_list.xml index e21e5e44..e676e623 100644 --- a/app/src/main/res/layout/journal_viewer_list.xml +++ b/app/src/main/res/layout/journal_viewer_list.xml @@ -15,6 +15,6 @@ android:layout_height="match_parent" android:layout_margin="16dp" android:gravity="center" - android:text="@string/journal_entries_list_empty" + android:text="@string/journal_entries_loading" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> diff --git a/app/src/main/res/layout/view_collection_activity.xml b/app/src/main/res/layout/view_collection_activity.xml index 34106bfe..329badc0 100644 --- a/app/src/main/res/layout/view_collection_activity.xml +++ b/app/src/main/res/layout/view_collection_activity.xml @@ -1,9 +1,9 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + android:hint="@string/create_calendar_display_name_hint" + android:textAppearance="?android:attr/textAppearanceMedium"/> + android:background="@color/orangeA700"/> @@ -39,7 +39,7 @@ android:id="@+id/description" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginBottom="16dp" /> + android:layout_marginBottom="16dp"/> + android:textAppearance="?android:attr/textAppearanceMedium"/> - + + + + + No entries found for this journal + Loading journal entries... An error has occurred.