Created
September 3, 2013 00:57
-
-
Save jpotts18/6418586 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.ProgressDialog; | |
import android.os.Bundle; | |
import com.mydietitian.android.MDApplication; | |
import com.mydietitian.android.R; | |
import com.mydietitian.android.utils.DialogHelper; | |
import com.mydietitian.android.utils.DialogType; | |
import com.mydietitian.android.utils.Network; | |
import com.mydietitian.android.utils.SessionManager; | |
import retrofit.client.Response; | |
/** | |
* User: jpotts | |
* Date: 8/28/13 | |
* Time: 9:03 PM | |
*/ | |
public abstract class AbstractActivity extends Activity { | |
protected static final String TAG = AbstractActivity.class.getSimpleName(); | |
private ProgressDialog mProgressDialog; | |
private boolean destroyed; | |
public MDApplication mApp; | |
public boolean mNetworkIsAvailable = false; | |
public boolean mUserSessionIsVerified = false; | |
public String mAuthHeader; | |
public String mApiVersion; | |
// *************************************** | |
// Activity methods | |
// *************************************** | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mApp = (MDApplication) getApplication(); | |
verifyConnectivity(); | |
verifySession(); | |
if(mUserSessionIsVerified){ | |
mAuthHeader = SessionManager.getInstance(this).getAuthorizationHeader(); | |
mApiVersion = getResources().getString(R.string.api_version); | |
} | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
destroyed = true; | |
} | |
// *************************************** | |
// Public methods | |
// *************************************** | |
private void verifyConnectivity(){ | |
mNetworkIsAvailable = Network.isOnline(this); | |
} | |
private void verifySession(){ | |
mUserSessionIsVerified = SessionManager.getInstance(this).verifySession(); | |
} | |
public void launchNetworkingDialog(){ | |
DialogHelper.makeDialog(this, DialogType.NETWORK, null); | |
} | |
public void launchErrorDialog(){ | |
DialogHelper.makeDialog(this, DialogType.ERROR, null); | |
} | |
public void launchErrorDialog(String message){ | |
DialogHelper.makeDialog(this, DialogType.ERROR, message, null); | |
} | |
public void launchMaintenanceDialog(){ | |
DialogHelper.makeDialog(this, DialogType.MAINTENANCE, null); | |
} | |
public void launchInfoDialog(){ | |
DialogHelper.makeDialog(this, DialogType.INFO, null); | |
} | |
public void launchInfoDialog(String message){ | |
} | |
public void showLoadingProgressDialog() { | |
this.showProgressDialog("Loading. Please wait..."); | |
} | |
public void showProgressDialog(CharSequence message) { | |
if (mProgressDialog == null) { | |
mProgressDialog = new ProgressDialog(this); | |
mProgressDialog.setIndeterminate(true); | |
} | |
mProgressDialog.setMessage(message); | |
mProgressDialog.show(); | |
} | |
public void dismissProgressDialog() { | |
if (mProgressDialog != null && !destroyed) { | |
mProgressDialog.dismiss(); | |
} | |
} | |
// TODO How should I handle the errors | |
public void processErrors(Response response, Object body){ | |
switch (response.getStatus()){ | |
case 401: | |
mUserSessionIsVerified = false; | |
break; | |
case 403: | |
launchErrorDialog("Sorry you don't have access to do that"); | |
break; | |
case 422: | |
launchErrorDialog(); | |
break; | |
case 500: | |
launchMaintenanceDialog(); | |
default: | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment