mirror of
https://github.com/etesync/android
synced 2025-01-23 06:01:01 +00:00
Journal - Load entries on background
This commit is contained in:
parent
d5ba48e59c
commit
e4fc23eb70
@ -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<Persistable> 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<Void, Void, Long> {
|
||||
private int entryCount;
|
||||
|
||||
@Override
|
||||
protected Long doInBackground(Void... aVoids) {
|
||||
EntityDataStore<Persistable> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Persistable> 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<Void, Void, List<EntryEntity>> {
|
||||
|
||||
@Override
|
||||
protected List<EntryEntity> 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<EntryEntity> result) {
|
||||
EntriesListAdapter listAdapter = new EntriesListAdapter(getContext());
|
||||
setListAdapter(listAdapter);
|
||||
|
||||
listAdapter.addAll(result);
|
||||
|
||||
emptyTextView.setText(getString(R.string.journal_entries_list_empty));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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" />
|
||||
</LinearLayout>
|
||||
|
@ -1,9 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
@ -22,16 +22,16 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:hint="@string/create_calendar_display_name_hint"
|
||||
android:layout_weight="1"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium" />
|
||||
android:hint="@string/create_calendar_display_name_hint"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"/>
|
||||
|
||||
<View
|
||||
android:id="@+id/color"
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="32dp"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:background="@color/orangeA700" />
|
||||
android:background="@color/orangeA700"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
@ -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"/>
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
@ -47,14 +47,26 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="@dimen/activity_margin"
|
||||
android:text="@string/change_journal_title"
|
||||
android:textAppearance="?android:attr/textAppearanceMedium" />
|
||||
android:textAppearance="?android:attr/textAppearanceMedium"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/stats"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="@dimen/activity_margin"
|
||||
android:text="Stats:" />
|
||||
<LinearLayout android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:padding="@dimen/activity_margin">
|
||||
<TextView
|
||||
android:id="@+id/stats"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Stats:"/>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progressBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_gravity="right"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/list_entries_container"
|
||||
|
@ -208,6 +208,7 @@
|
||||
|
||||
<!-- JournalViewer -->
|
||||
<string name="journal_entries_list_empty">No entries found for this journal</string>
|
||||
<string name="journal_entries_loading">Loading journal entries...</string>
|
||||
|
||||
<!-- ExceptionInfoFragment -->
|
||||
<string name="exception">An error has occurred.</string>
|
||||
|
Loading…
Reference in New Issue
Block a user