From 7f3c2460899998b99bf7bdd1f78b297e77173f3e Mon Sep 17 00:00:00 2001 From: Gautier Nilhcem Date: Mon, 6 May 2013 23:50:37 +0200 Subject: [PATCH] Add loading messages --- res/layout/list_hosts_layout.xml | 18 ++++++++-- res/values/strings.xml | 9 +++++ .../hostseditor/bus/event/LoadingEvent.java | 21 ++++++++++++ .../hostseditor/list/ListHostsActivity.java | 33 +++++++++++++++---- .../hostseditor/list/ListHostsFragment.java | 10 ++++-- .../hostseditor/task/AddEditHostAsync.java | 6 ++++ .../hostseditor/task/GenericTaskAsync.java | 7 ++-- .../hostseditor/task/ListHostsAsync.java | 3 +- .../hostseditor/task/RemoveHostsAsync.java | 6 ++++ .../hostseditor/task/ToggleHostsAsync.java | 6 ++++ 10 files changed, 103 insertions(+), 16 deletions(-) diff --git a/res/layout/list_hosts_layout.xml b/res/layout/list_hosts_layout.xml index aa75982..fecaf4b 100644 --- a/res/layout/list_hosts_layout.xml +++ b/res/layout/list_hosts_layout.xml @@ -1,5 +1,6 @@ - + + android:layout_height="wrap_content" + android:visibility="gone" /> + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index a9abe55..9a2f94a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -32,4 +32,13 @@ Delete Toggle + + Loading host entries + Adding entry + Editing entry + Removing entry + Removing entries + Toggling entry + Toggling entries + diff --git a/src/com/nilhcem/hostseditor/bus/event/LoadingEvent.java b/src/com/nilhcem/hostseditor/bus/event/LoadingEvent.java index 40cecb7..058aced 100644 --- a/src/com/nilhcem/hostseditor/bus/event/LoadingEvent.java +++ b/src/com/nilhcem/hostseditor/bus/event/LoadingEvent.java @@ -1,13 +1,34 @@ package com.nilhcem.hostseditor.bus.event; +import android.content.Context; + public class LoadingEvent { private boolean mIsLoading; + private int mMessageRes; + private String mMessage; public LoadingEvent(boolean isLoading) { + this(isLoading, ""); + } + + public LoadingEvent(boolean isLoading, int messageRes) { + mIsLoading = isLoading; + mMessageRes = messageRes; + } + + public LoadingEvent(boolean isLoading, String message) { mIsLoading = isLoading; + mMessage = message; } public boolean isLoading() { return mIsLoading; } + + public String getMessage(Context context) { + if (mMessage == null) { + return context.getString(mMessageRes); + } + return mMessage; + } } diff --git a/src/com/nilhcem/hostseditor/list/ListHostsActivity.java b/src/com/nilhcem/hostseditor/list/ListHostsActivity.java index 40bea59..ebfb62c 100644 --- a/src/com/nilhcem/hostseditor/list/ListHostsActivity.java +++ b/src/com/nilhcem/hostseditor/list/ListHostsActivity.java @@ -6,8 +6,10 @@ import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; +import android.util.Log; import android.view.View; import android.widget.ProgressBar; +import android.widget.TextView; import butterknife.InjectView; import butterknife.Views; @@ -23,11 +25,14 @@ import com.squareup.otto.Subscribe; public class ListHostsActivity extends BaseActivity { - private static final int REQUESTCODE_ADDHOST_ACTIVITY = 1; + private static final int REQUESTCODE_ADDEDIT_ACTIVITY = 1; + private static final String TAG = "ListHostsActivity"; private static final String INSTANCE_STATE_LOADING = "loading"; + private static final String INSTANCE_STATE_LOADING_MESSAGE = "loadingMessage"; @Inject HostsManager mHostsManager; @InjectView(R.id.listLoading) ProgressBar mProgressBar; + @InjectView(R.id.listLoadingMsg) TextView mLoadingMsg; private ListHostsFragment mFragment; @Override @@ -41,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) { mFragment.computeViewWidths(); if (savedInstanceState == null) { - onLoadingEvent(new LoadingEvent(true)); + onLoadingEvent(new LoadingEvent(true, R.string.loading_hosts)); } } @@ -72,12 +77,15 @@ public boolean onOptionsItemSelected(MenuItem item) { protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - if (requestCode == REQUESTCODE_ADDHOST_ACTIVITY) { + Log.d(TAG, "Activity result received"); + if (requestCode == REQUESTCODE_ADDEDIT_ACTIVITY) { if (resultCode == RESULT_OK) { - onLoadingEvent(new LoadingEvent(true)); Host modified = data.getParcelableExtra(AddEditHostActivity.EXTRA_HOST_MODIFIED); Host original = data.getParcelableExtra(AddEditHostActivity.EXTRA_HOST_ORIGINAL); - mFragment.addHost(new Host[] { modified, original }); + + boolean addMode = (original == null); + onLoadingEvent(new LoadingEvent(true, addMode ? R.string.loading_add : R.string.loading_edit)); + mFragment.addEditHost(addMode, new Host[] { modified, original }); } } } @@ -86,6 +94,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(INSTANCE_STATE_LOADING, mProgressBar.getVisibility() == View.VISIBLE); + outState.putString(INSTANCE_STATE_LOADING_MESSAGE, mLoadingMsg.getText().toString()); } @Override @@ -93,26 +102,36 @@ protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); boolean isLoading = savedInstanceState.getBoolean(INSTANCE_STATE_LOADING, true); if (isLoading) { - onLoadingEvent(new LoadingEvent(true)); + String loadingMsg = savedInstanceState.getString(INSTANCE_STATE_LOADING_MESSAGE); + if (loadingMsg == null) { + loadingMsg = ""; + } + onLoadingEvent(new LoadingEvent(isLoading, loadingMsg)); } } @Subscribe public void onStartAddEditActivityEvent(StartAddEditActivityEvent event) { + Log.d(TAG, "Ready to start AddEditActivity"); Intent intent = new Intent(this, AddEditHostActivity.class); intent.putExtra(AddEditHostActivity.EXTRA_HOST_ORIGINAL, event.getHost()); - startActivityForResult(intent, REQUESTCODE_ADDHOST_ACTIVITY); + startActivityForResult(intent, REQUESTCODE_ADDEDIT_ACTIVITY); } @Subscribe public void onLoadingEvent(LoadingEvent event) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + mLoadingMsg.setText(event.getMessage(this)); if (event.isLoading()) { + Log.d(TAG, "Start loading"); mProgressBar.setVisibility(View.VISIBLE); + mLoadingMsg.setVisibility(View.VISIBLE); ft.hide(mFragment); } else { + Log.d(TAG, "Stop loading"); mProgressBar.setVisibility(View.GONE); + mLoadingMsg.setVisibility(View.GONE); ft.show(mFragment); } ft.commit(); diff --git a/src/com/nilhcem/hostseditor/list/ListHostsFragment.java b/src/com/nilhcem/hostseditor/list/ListHostsFragment.java index 8aa874c..ca86dbd 100644 --- a/src/com/nilhcem/hostseditor/list/ListHostsFragment.java +++ b/src/com/nilhcem/hostseditor/list/ListHostsFragment.java @@ -122,8 +122,8 @@ public void onHostsRefreshed(RefreshHostsEvent hosts) { mBus.post(new LoadingEvent(false)); } - public void addHost(Host[] hosts) { - runGenericTask(AddEditHostAsync.class, hosts); + public void addEditHost(boolean addMode, Host[] hosts) { + runGenericTask(AddEditHostAsync.class, hosts, addMode); } public void refreshHosts(boolean forceRefresh) { @@ -220,8 +220,12 @@ private Host[] getSelectedItems() { }; private void runGenericTask(Class clazz, Host[] hosts) { + runGenericTask(clazz, hosts, hosts.length == 1); + } + + private void runGenericTask(Class clazz, Host[] hosts, boolean flagMsg) { GenericTaskAsync task = mApp.getObjectGraph().get(clazz); - task.setAppContext(mActivity.getApplicationContext()); + task.init(mActivity.getApplicationContext(), flagMsg); task.execute(hosts); } diff --git a/src/com/nilhcem/hostseditor/task/AddEditHostAsync.java b/src/com/nilhcem/hostseditor/task/AddEditHostAsync.java index 6b1b444..620b277 100644 --- a/src/com/nilhcem/hostseditor/task/AddEditHostAsync.java +++ b/src/com/nilhcem/hostseditor/task/AddEditHostAsync.java @@ -4,6 +4,7 @@ import android.util.Log; +import com.nilhcem.hostseditor.R; import com.nilhcem.hostseditor.core.Host; /** @@ -25,4 +26,9 @@ protected void process(Host... params) { hosts.get(hosts.indexOf(original)).merge(host); } } + + @Override + protected int getLoadingMsgRes() { + return mFlagLoadingMsg ? R.string.loading_add : R.string.loading_edit; + } } diff --git a/src/com/nilhcem/hostseditor/task/GenericTaskAsync.java b/src/com/nilhcem/hostseditor/task/GenericTaskAsync.java index 5225c40..2dc21a1 100644 --- a/src/com/nilhcem/hostseditor/task/GenericTaskAsync.java +++ b/src/com/nilhcem/hostseditor/task/GenericTaskAsync.java @@ -20,11 +20,12 @@ public abstract class GenericTaskAsync extends AsyncTask { HostsManager mHostsManager; private Context mAppContext; + protected boolean mFlagLoadingMsg; // which loading message (between 2) to display: (singular/plural) - (add/edit). @Override protected void onPreExecute() { super.onPreExecute(); - mBus.post(new LoadingEvent(true)); + mBus.post(new LoadingEvent(true, getLoadingMsgRes())); } @Override @@ -50,8 +51,9 @@ protected void onCancelled() { mBus.post(new TaskCompletedEvent(getClass().getSimpleName(), false)); } - public void setAppContext(Context appContext) { + public void init(Context appContext, boolean flagMsg) { mAppContext = appContext; + mFlagLoadingMsg = flagMsg; } /** @@ -59,4 +61,5 @@ public void setAppContext(Context appContext) { * @param params selected Hosts from the main ListView. */ protected abstract void process(Host... params); + protected abstract int getLoadingMsgRes(); } diff --git a/src/com/nilhcem/hostseditor/task/ListHostsAsync.java b/src/com/nilhcem/hostseditor/task/ListHostsAsync.java index ea3622f..f060565 100644 --- a/src/com/nilhcem/hostseditor/task/ListHostsAsync.java +++ b/src/com/nilhcem/hostseditor/task/ListHostsAsync.java @@ -7,6 +7,7 @@ import android.os.AsyncTask; +import com.nilhcem.hostseditor.R; import com.nilhcem.hostseditor.bus.event.LoadingEvent; import com.nilhcem.hostseditor.bus.event.RefreshHostsEvent; import com.nilhcem.hostseditor.core.Host; @@ -23,7 +24,7 @@ public class ListHostsAsync extends AsyncTask> { @Override protected void onPreExecute() { super.onPreExecute(); - mBus.post(new LoadingEvent(true)); + mBus.post(new LoadingEvent(true, R.string.loading_hosts)); } @Override diff --git a/src/com/nilhcem/hostseditor/task/RemoveHostsAsync.java b/src/com/nilhcem/hostseditor/task/RemoveHostsAsync.java index aa21f25..caf2e87 100644 --- a/src/com/nilhcem/hostseditor/task/RemoveHostsAsync.java +++ b/src/com/nilhcem/hostseditor/task/RemoveHostsAsync.java @@ -4,6 +4,7 @@ import android.util.Log; +import com.nilhcem.hostseditor.R; import com.nilhcem.hostseditor.core.Host; /** @@ -22,4 +23,9 @@ protected void process(Host... params) { hosts.remove(params[i]); } } + + @Override + protected int getLoadingMsgRes() { + return mFlagLoadingMsg ? R.string.loading_remove_single : R.string.loading_remove_multiple; + } } diff --git a/src/com/nilhcem/hostseditor/task/ToggleHostsAsync.java b/src/com/nilhcem/hostseditor/task/ToggleHostsAsync.java index ff110f8..bca3456 100644 --- a/src/com/nilhcem/hostseditor/task/ToggleHostsAsync.java +++ b/src/com/nilhcem/hostseditor/task/ToggleHostsAsync.java @@ -2,6 +2,7 @@ import android.util.Log; +import com.nilhcem.hostseditor.R; import com.nilhcem.hostseditor.core.Host; /** @@ -19,4 +20,9 @@ protected void process(Host... params) { params[i].toggleComment(); } } + + @Override + protected int getLoadingMsgRes() { + return mFlagLoadingMsg ? R.string.loading_toggle_single : R.string.loading_toggle_multiple; + } }