Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use a Loader for the dashboard, get work off the UI thread

Also using a 'fragment' for the first time- the repo list fragment.
  • Loading branch information...
commit 1c9492072a013f165a811836718f687b743331dd 1 parent e7d25f4
@rtyley authored
View
2  agit/pom.xml
@@ -18,7 +18,7 @@
<dependency>
<groupId>com.madgag</groupId>
<artifactId>android-viewholder-listviews</artifactId>
- <version>0.4</version>
+ <version>0.5</version>
</dependency>
<dependency>
<groupId>com.madgag</groupId>
View
11 agit/res/layout/dashboard.xml
@@ -1,11 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView
- android:id="@android:id/list"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:cacheColorHint="#FFF"/>
-</LinearLayout>
View
11 agit/res/layout/dashboard_activity.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent" >
+ <fragment
+ android:id="@android:id/list"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ class="com.madgag.agit.RepoListFragment" />
+
+</FrameLayout>
View
79 agit/src/main/java/com/madgag/agit/AsyncLoader.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2011 Alexander Blom
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.madgag.agit;
+
+import android.content.Context;
+import android.support.v4.content.AsyncTaskLoader;
+
+/**
+ * Loader which extends AsyncTaskLoaders and handles caveats as pointed out in
+ * http://code.google.com/p/android/issues/detail?id=14944.
+ * <p/>
+ * Based on CursorLoader.java in the Fragment compatibility package
+ *
+ * @param <D>
+ * data type
+ * @author Alexander Blom (me@alexanderblom.se)
+ */
+public abstract class AsyncLoader<D> extends AsyncTaskLoader<D> {
+ private D data;
+
+ /**
+ * Create async loader
+ *
+ * @param context
+ */
+ public AsyncLoader(Context context) {
+ super(context);
+ }
+
+ @Override
+ public void deliverResult(D data) {
+ if (isReset())
+ // An async query came in while the loader is stopped
+ return;
+
+ this.data = data;
+
+ super.deliverResult(data);
+ }
+
+ @Override
+ protected void onStartLoading() {
+ if (data != null)
+ deliverResult(data);
+
+ if (takeContentChanged() || data == null)
+ forceLoad();
+ }
+
+ @Override
+ protected void onStopLoading() {
+ // Attempt to cancel the current load task if possible.
+ cancelLoad();
+ }
+
+ @Override
+ protected void onReset() {
+ super.onReset();
+
+ // Ensure the loader is stopped
+ onStopLoading();
+
+ data = null;
+ }
+}
View
99 agit/src/main/java/com/madgag/agit/DashboardActivity.java
@@ -1,99 +1,30 @@
package com.madgag.agit;
-import static android.R.drawable.ic_menu_info_details;
-import static android.graphics.PixelFormat.RGBA_8888;
-import static com.madgag.agit.GitIntents.REPO_STATE_CHANGED_BROADCAST;
-import static com.madgag.agit.GitIntents.actionWithSuffix;
-import static com.madgag.agit.R.layout.dashboard_repo_list_header;
-import static com.madgag.agit.R.layout.repo_list_item;
-import static com.madgag.agit.R.string.about_app_menu_option;
-import static com.madgag.agit.RepositoryViewerActivity.manageRepoIntent;
import static com.madgag.agit.sync.AccountAuthenticatorService.addAccount;
import static com.madgag.android.jgit.HarmonyFixInflater.checkHarmoniousRepose;
-import static com.madgag.android.listviews.ViewInflator.viewInflatorFor;
-import android.content.BroadcastReceiver;
-import android.content.Context;
import android.content.Intent;
-import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
-import android.view.LayoutInflater;
+
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
-import android.view.View;
-import android.widget.AdapterView;
-import android.widget.ListView;
-import android.widget.TextView;
-
-import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockActivity;
-import com.madgag.android.listviews.ViewHolder;
-import com.madgag.android.listviews.ViewHolderFactory;
-import com.madgag.android.listviews.ViewHoldingListAdapter;
-
-import roboguice.activity.RoboActivity;
-import roboguice.inject.InjectView;
+import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;
-public class DashboardActivity extends RoboSherlockActivity {
- private static final String TAG = "DA";
+public class DashboardActivity extends RoboSherlockFragmentActivity {
- @InjectView(android.R.id.list)
- ListView listView;
- ViewHoldingListAdapter<RepoSummary> listAdapter;
+ private static final String TAG = "DashboardActivity";
@Override
- public void onCreate(Bundle savedInstanceState) {
+ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- setContentView(R.layout.dashboard);
-
- setupRepoList();
- Log.i(TAG, "Inflater zero-byte inflation (HARMONY-6637/Android #11755 fix applied) : " +
- checkHarmoniousRepose());
+ Log.i(TAG, "Inflater zero-byte inflation (HARMONY-6637/Android #11755) fixed: " + checkHarmoniousRepose());
try {
addAccount(this);
} catch (Exception e) {
Log.w(TAG, "Unable to add account for syncing", e);
}
- }
-
- @Override
- public void onAttachedToWindow() {
- super.onAttachedToWindow();
- getWindow().setFormat(RGBA_8888);
- }
-
- BroadcastReceiver repoStateChangeReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- Log.d(TAG, "repoStateChangeReceiver got broadcast : " + intent);
- updateRepoList();
- }
- };
-
- private void setupRepoList() {
-
- listAdapter = new ViewHoldingListAdapter<RepoSummary>(RepoSummary.getAllReposOrderChronologically(),
- viewInflatorFor(this, repo_list_item), new ViewHolderFactory<RepoSummary>() {
- public ViewHolder<RepoSummary> createViewHolderFor(View view) {
- return new RepositoryViewHolder(view);
- }
- });
-
- listView.addHeaderView(repoListHeader(), null, false);
- listView.setAdapter(listAdapter);
- listView.setHeaderDividersEnabled(true);
-
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- startActivity(manageRepoIntent(((RepoSummary) listView.getAdapter().getItem(position)).getRepo()
- .getDirectory()));
- }
- });
- }
-
- private TextView repoListHeader() {
- TextView repoListHeader = (TextView) LayoutInflater.from(this).inflate(dashboard_repo_list_header, null);
- repoListHeader.setCompoundDrawables(null, null, null, listView.getDivider());
- return repoListHeader;
+ setContentView(R.layout.dashboard_activity);
}
@Override
@@ -115,20 +46,4 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}
-
-
- protected void onResume() {
- super.onResume();
- registerReceiver(repoStateChangeReceiver, new IntentFilter(actionWithSuffix(REPO_STATE_CHANGED_BROADCAST)));
- updateRepoList();
- }
-
- protected void onPause() {
- super.onPause();
- unregisterReceiver(repoStateChangeReceiver);
- }
-
- private void updateRepoList() {
- listAdapter.setList(RepoSummary.getAllReposOrderChronologically());
- }
}
View
59 agit/src/main/java/com/madgag/agit/ListLoadingFragment.java
@@ -0,0 +1,59 @@
+package com.madgag.agit;
+
+import android.os.Bundle;
+import android.support.v4.app.LoaderManager.LoaderCallbacks;
+import android.support.v4.content.Loader;
+import android.util.Log;
+
+import com.github.rtyley.android.sherlock.roboguice.fragment.RoboSherlockListFragment;
+import com.madgag.android.listviews.ViewHoldingListAdapter;
+
+import java.util.List;
+
+public abstract class ListLoadingFragment<E> extends RoboSherlockListFragment implements LoaderCallbacks<List<E>> {
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState) {
+ super.onActivityCreated(savedInstanceState);
+ setListShown(false);
+ getLoaderManager().initLoader(0, null, this);
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setHasOptionsMenu(true);
+ }
+
+ public void refresh() {
+ Log.d(getClass().getSimpleName(), "Refresh requested...");
+ if (getActivity() != null)
+ getLoaderManager().restartLoader(0, null, this);
+ }
+
+ public void onLoadFinished(Loader<List<E>> loader, List<E> items) {
+ setList(items);
+
+ if (isResumed())
+ setListShown(true);
+ else
+ setListShownNoAnimation(true);
+ }
+
+
+ private void setList(List<E> items) {
+ @SuppressWarnings("unchecked")
+ ViewHoldingListAdapter<E> listAdapter = (ViewHoldingListAdapter<E>) getListAdapter();
+ if (listAdapter == null) {
+ setListAdapter(adapterFor(items));
+ } else {
+ listAdapter.setList(items);
+ }
+ }
+
+ protected abstract ViewHoldingListAdapter<E> adapterFor(List<E> items);
+
+ @Override
+ public void onLoaderReset(Loader<List<E>> listLoader) {
+ }
+}
View
66 agit/src/main/java/com/madgag/agit/RepoListFragment.java
@@ -0,0 +1,66 @@
+package com.madgag.agit;
+
+import static com.madgag.agit.GitIntents.REPO_STATE_CHANGED_BROADCAST;
+import static com.madgag.agit.GitIntents.actionWithSuffix;
+import static com.madgag.agit.R.layout.repo_list_item;
+import static com.madgag.agit.RepositoryViewerActivity.manageRepoIntent;
+import static com.madgag.android.listviews.ReflectiveHolderFactory.reflectiveFactoryFor;
+import static com.madgag.android.listviews.ViewInflator.viewInflatorFor;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Bundle;
+import android.support.v4.content.Loader;
+import android.util.Log;
+import android.view.View;
+import android.widget.ListView;
+
+import com.madgag.android.listviews.ViewHoldingListAdapter;
+
+import java.util.List;
+
+public class RepoListFragment extends ListLoadingFragment<RepoSummary> {
+
+ private static final String TAG = "RepoListFragment";
+
+ BroadcastReceiver repoStateChangeReceiver = new BroadcastReceiver() {
+ public void onReceive(Context context, Intent intent) {
+ Log.d(TAG, "repoStateChangeReceiver got broadcast : " + intent);
+ refresh();
+ }
+ };
+
+ @Override
+ protected ViewHoldingListAdapter<RepoSummary> adapterFor(List<RepoSummary> items) {
+ return new ViewHoldingListAdapter<RepoSummary>(items, viewInflatorFor(getActivity(), repo_list_item),
+ reflectiveFactoryFor(RepositoryViewHolder.class));
+ }
+
+ @Override
+ public Loader<List<RepoSummary>> onCreateLoader(int id, Bundle args) {
+ return new AsyncLoader<List<RepoSummary>>(getActivity()) {
+ public List<RepoSummary> loadInBackground() {
+ return RepoSummary.getAllReposOrderChronologically();
+ }
+ };
+ }
+
+ @Override
+ public void onListItemClick(ListView list, View view, int position, long id) {
+ RepoSummary repo = (RepoSummary) list.getItemAtPosition(position);
+ startActivity(manageRepoIntent(repo.getRepo().getDirectory()));
+ }
+
+ public void onResume() {
+ super.onResume();
+ refresh();
+ getActivity().registerReceiver(repoStateChangeReceiver,
+ new IntentFilter(actionWithSuffix(REPO_STATE_CHANGED_BROADCAST)));
+ }
+
+ public void onPause() {
+ super.onPause();
+ getActivity().unregisterReceiver(repoStateChangeReceiver);
+ }
+}
View
8 agit/src/main/java/com/madgag/agit/SuggestRepoActivity.java
@@ -4,6 +4,7 @@
import static com.madgag.agit.CloneLauncherActivity.cloneLauncherIntentFor;
import static com.madgag.agit.SuggestedRepo.SUGGESTIONS;
import static com.madgag.android.ActionBarUtil.homewardsWith;
+import static com.madgag.android.listviews.ReflectiveHolderFactory.reflectiveFactoryFor;
import static com.madgag.android.listviews.ViewInflator.viewInflatorFor;
import android.content.Intent;
import android.os.Bundle;
@@ -13,6 +14,7 @@
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockListActivity;
+import com.madgag.android.listviews.ReflectiveHolderFactory;
import com.madgag.android.listviews.ViewHolder;
import com.madgag.android.listviews.ViewHolderFactory;
import com.madgag.android.listviews.ViewHoldingListAdapter;
@@ -34,11 +36,7 @@ public void onCreate(Bundle savedInstanceState) {
actionBar.setTitle("Some example repos...");
adapter = new ViewHoldingListAdapter<SuggestedRepo>(SUGGESTIONS, viewInflatorFor(this, simple_list_item_2),
- new ViewHolderFactory<SuggestedRepo>() {
- public ViewHolder<SuggestedRepo> createViewHolderFor(View view) {
- return new SuggestedRepoViewHolder(view);
- }
- });
+ reflectiveFactoryFor(SuggestedRepoViewHolder.class));
setListAdapter(adapter);
}
View
1  proguard.cfg
@@ -16,6 +16,7 @@
-keep class com.madgag.agit.**
-keep class com.madgag.agit.** { public static *; }
-keep class com.madgag.android.blockingprompt.**
+-keepclassmembers class * extends com.madgag.android.listviews.ViewHolder { public <init>(...); }
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
Please sign in to comment.
Something went wrong with that request. Please try again.