Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 80d67275 authored by Marten Gajda's avatar Marten Gajda
Browse files

improve model loading, don't run an asynctask if there is no need to

parent c156bdcf
Loading
Loading
Loading
Loading
+11 −14
Original line number Diff line number Diff line
@@ -30,8 +30,8 @@ import org.dmfs.provider.tasks.TaskContract.Tasks;
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.Model;
import org.dmfs.tasks.model.OnContentChangeListener;
import org.dmfs.tasks.model.Sources;
import org.dmfs.tasks.model.TaskFieldAdapters;
import org.dmfs.tasks.utils.AsyncModelLoader;
import org.dmfs.tasks.utils.ContentValueMapper;
import org.dmfs.tasks.utils.OnModelLoadedListener;
import org.dmfs.tasks.utils.TasksListCursorSpinnerAdapter;
@@ -251,11 +251,6 @@ public class EditTaskFragment extends SupportFragment implements LoaderManager.L
			{
				if (savedInstanceState == null && mValues == null)
				{
					if (mAccountType != null)
					{
						new AsyncModelLoader(mAppContext, this).execute(mAccountType);
					}

					mValues = new ContentSet(mTaskUri);
					mValues.addOnChangeListener(this, null, false);

@@ -266,11 +261,11 @@ public class EditTaskFragment extends SupportFragment implements LoaderManager.L
					if (savedInstanceState != null)
					{
						mValues = savedInstanceState.getParcelable(KEY_VALUES);
						new AsyncModelLoader(mAppContext, this).execute(mValues.getAsString(Tasks.ACCOUNT_TYPE));
						Sources.loadModelAsync(mAppContext, mValues.getAsString(Tasks.ACCOUNT_TYPE), this);
					}
					else
					{
						new AsyncModelLoader(mAppContext, this).execute(mValues.getAsString(Tasks.ACCOUNT_TYPE));
						Sources.loadModelAsync(mAppContext, mValues.getAsString(Tasks.ACCOUNT_TYPE), this);
						// ensure we're using the latest values
						mValues.update(mAppContext, CONTENT_VALUE_MAPPER);
					}
@@ -302,13 +297,13 @@ public class EditTaskFragment extends SupportFragment implements LoaderManager.L

				if (mLastAccountType != null)
				{
					new AsyncModelLoader(mAppContext, this).execute(mLastAccountType);
					Sources.loadModelAsync(mAppContext, mLastAccountType, this);
				}
			}
			else
			{
				mValues = savedInstanceState.getParcelable(KEY_VALUES);
				new AsyncModelLoader(mAppContext, this).execute(mLastAccountType);
				Sources.loadModelAsync(mAppContext, mLastAccountType, this);
			}
			setListUri(TaskLists.getContentUri(mAuthority), LIST_LOADER_VISIBLE_LISTS_FILTER);
		}
@@ -472,11 +467,13 @@ public class EditTaskFragment extends SupportFragment implements LoaderManager.L
	public void onContentLoaded(ContentSet contentSet)
	{
		if (contentSet.containsKey(Tasks.ACCOUNT_TYPE))
		{
			if (mModel != null && mModel.getAccountType().equals(contentSet.getAsString(Tasks.ACCOUNT_TYPE)))
		{
			mListColor = contentSet.getAsInteger(Tasks.LIST_COLOR);
			updateColor((float) mRootView.getScrollY() / mTaskListBar.getMeasuredHeight());

			if (mAppForEdit)
			{
				Sources.loadModelAsync(mAppContext, contentSet.getAsString(Tasks.ACCOUNT_TYPE), EditTaskFragment.this);
			}

			/*
@@ -530,7 +527,7 @@ public class EditTaskFragment extends SupportFragment implements LoaderManager.L
		if (mModel == null || !mModel.getAccountType().equals(accountType))
		{
			// the model changed, load the new model
			new AsyncModelLoader(mAppContext, EditTaskFragment.this).execute(accountType);
			Sources.loadModelAsync(mAppContext, accountType, EditTaskFragment.this);
		}
		else
		{
+2 −2
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ import org.dmfs.tasks.groupings.ByList;
import org.dmfs.tasks.groupings.filters.AbstractFilter;
import org.dmfs.tasks.groupings.filters.ConstantFilter;
import org.dmfs.tasks.model.Model;
import org.dmfs.tasks.utils.AsyncModelLoader;
import org.dmfs.tasks.model.Sources;
import org.dmfs.tasks.utils.ExpandableGroupDescriptor;
import org.dmfs.tasks.utils.ExpandableGroupDescriptorAdapter;
import org.dmfs.tasks.utils.FlingDetector;
@@ -239,7 +239,7 @@ public class TaskListFragment extends SupportFragment implements LoaderManager.L
		mCallbacks = (Callbacks) activity;

		// load accounts early
		new AsyncModelLoader(activity, this).execute(TaskContract.LOCAL_ACCOUNT);
		Sources.loadModelAsync(activity, TaskContract.LOCAL_ACCOUNT, this);
	}


+4 −5
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ import org.dmfs.provider.tasks.TaskContract.Tasks;
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.Model;
import org.dmfs.tasks.model.OnContentChangeListener;
import org.dmfs.tasks.model.Sources;
import org.dmfs.tasks.model.TaskFieldAdapters;
import org.dmfs.tasks.utils.AsyncModelLoader;
import org.dmfs.tasks.utils.ContentValueMapper;
import org.dmfs.tasks.utils.OnModelLoadedListener;
import org.dmfs.tasks.widget.ListenableScrollView;
@@ -238,7 +238,7 @@ public class ViewTaskFragment extends SupportFragment implements OnModelLoadedLi
				if (mContentSet.getAsString(Tasks.ACCOUNT_TYPE) != null)
				{
					// the content set contains a valid task, so load the model
					new AsyncModelLoader(mAppContext, this).execute(mContentSet.getAsString(Tasks.ACCOUNT_TYPE));
					Sources.loadModelAsync(mAppContext, mContentSet.getAsString(Tasks.ACCOUNT_TYPE), this);
				}
			}
		}
@@ -522,11 +522,10 @@ public class ViewTaskFragment extends SupportFragment implements OnModelLoadedLi
			{
				updateColor((float) mRootView.getScrollY() / ((ActionBarActivity) getActivity()).getSupportActionBar().getHeight());
			}

			if (mModel == null || !TextUtils.equals(mModel.getAccountType(), contentSet.getAsString(Tasks.ACCOUNT_TYPE)))
			{

				// the ContentSet has been (re-)loaded, load the model of this task
				new AsyncModelLoader(mAppContext, this).execute(contentSet.getAsString(Tasks.ACCOUNT_TYPE));
				Sources.loadModelAsync(mAppContext, contentSet.getAsString(Tasks.ACCOUNT_TYPE), this);
			}
			else
			{
+29 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import java.util.Map;

import org.dmfs.provider.tasks.TaskContract;
import org.dmfs.tasks.R;
import org.dmfs.tasks.utils.AsyncModelLoader;
import org.dmfs.tasks.utils.OnModelLoadedListener;

import android.accounts.Account;
import android.accounts.AccountManager;
@@ -86,6 +88,33 @@ public final class Sources extends BroadcastReceiver implements OnAccountsUpdate
	}


	/**
	 * Load a model asynchronously. This might be executed as a synchronous operation if the models have been loaded already.
	 * 
	 * @param context
	 *            A {@link Context}.
	 * @param accountType
	 *            The account type of the model to load.
	 * @param listener
	 *            The listener to call when the model has been loaded.
	 * @return <code>true</code> if the models were loaded already and the operation was executed synchronously, <code>false</code> otherwise.
	 */
	public static boolean loadModelAsync(Context context, String accountType, OnModelLoadedListener listener)
	{
		if (sInstance == null)
		{
			new AsyncModelLoader(context, listener).execute(accountType);
			return false;
		}
		else
		{
			Sources sources = getInstance(context);
			listener.onModelLoaded(sources.getModel(accountType));
			return true;
		}
	}


	/**
	 * Initialize all model sources.
	 *