Skip to content

Commit

Permalink
Add loading messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilhcem committed May 6, 2013
1 parent 5f3f4cc commit 7f3c246
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 16 deletions.
18 changes: 15 additions & 3 deletions res/layout/list_hosts_layout.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.nilhcem.hostseditor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
Expand All @@ -12,12 +13,23 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- Loading -->
<!-- Loading (progress bar) -->
<ProgressBar
android:id="@+id/listLoading"
style="?android:attr/progressBarStyleLarge"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:visibility="gone" />

<!-- Loading (message) -->
<com.nilhcem.hostseditor.widget.TypefacedTextView
android:id="@+id/listLoadingMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="12dp"
android:textSize="18sp"
android:visibility="gone"
app:typeface="fonts/Roboto-Regular.ttf" />

</LinearLayout>
9 changes: 9 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@
<string name="list_menu_delete">Delete</string>
<string name="list_menu_toggle">Toggle</string>

<!-- Loading messages -->
<string name="loading_hosts">Loading host entries</string>
<string name="loading_add">Adding entry</string>
<string name="loading_edit">Editing entry</string>
<string name="loading_remove_single">Removing entry</string>
<string name="loading_remove_multiple">Removing entries</string>
<string name="loading_toggle_single">Toggling entry</string>
<string name="loading_toggle_multiple">Toggling entries</string>

</resources>
21 changes: 21 additions & 0 deletions src/com/nilhcem/hostseditor/bus/event/LoadingEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
33 changes: 26 additions & 7 deletions src/com/nilhcem/hostseditor/list/ListHostsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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 });
}
}
}
Expand All @@ -86,33 +94,44 @@ 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
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();
Expand Down
10 changes: 7 additions & 3 deletions src/com/nilhcem/hostseditor/list/ListHostsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -220,8 +220,12 @@ private Host[] getSelectedItems() {
};

private void runGenericTask(Class<? extends GenericTaskAsync> clazz, Host[] hosts) {
runGenericTask(clazz, hosts, hosts.length == 1);
}

private void runGenericTask(Class<? extends GenericTaskAsync> clazz, Host[] hosts, boolean flagMsg) {
GenericTaskAsync task = mApp.getObjectGraph().get(clazz);
task.setAppContext(mActivity.getApplicationContext());
task.init(mActivity.getApplicationContext(), flagMsg);
task.execute(hosts);
}

Expand Down
6 changes: 6 additions & 0 deletions src/com/nilhcem/hostseditor/task/AddEditHostAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import android.util.Log;

import com.nilhcem.hostseditor.R;
import com.nilhcem.hostseditor.core.Host;

/**
Expand All @@ -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;
}
}
7 changes: 5 additions & 2 deletions src/com/nilhcem/hostseditor/task/GenericTaskAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public abstract class GenericTaskAsync extends AsyncTask<Host, Void, Void> {
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
Expand All @@ -50,13 +51,15 @@ 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;
}

/**
* This method should edit the main {@code List<Host>}.
* @param params selected Hosts from the main ListView.
*/
protected abstract void process(Host... params);
protected abstract int getLoadingMsgRes();
}
3 changes: 2 additions & 1 deletion src/com/nilhcem/hostseditor/task/ListHostsAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,7 +24,7 @@ public class ListHostsAsync extends AsyncTask<Boolean, Void, List<Host>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mBus.post(new LoadingEvent(true));
mBus.post(new LoadingEvent(true, R.string.loading_hosts));
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions src/com/nilhcem/hostseditor/task/RemoveHostsAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import android.util.Log;

import com.nilhcem.hostseditor.R;
import com.nilhcem.hostseditor.core.Host;

/**
Expand All @@ -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;
}
}
6 changes: 6 additions & 0 deletions src/com/nilhcem/hostseditor/task/ToggleHostsAsync.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.util.Log;

import com.nilhcem.hostseditor.R;
import com.nilhcem.hostseditor.core.Host;

/**
Expand All @@ -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;
}
}

0 comments on commit 7f3c246

Please sign in to comment.