Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.egslava.ma.ui.login;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.Uri;
- import android.os.Build;
- import android.text.Html;
- import android.view.View;
- import android.widget.TextView;
- import com.androidquery.AQuery;
- import com.egslava.ma.R;
- import com.egslava.ma.api.Comment;
- import com.egslava.ma.api.Like;
- import com.egslava.ma.api.Photo;
- import com.parse.ParseException;
- import com.parse.ParseObject;
- import com.parse.ParseUser;
- import org.androidannotations.annotations.AfterInject;
- import org.androidannotations.annotations.EBean;
- import org.androidannotations.annotations.RootContext;
- import org.androidannotations.annotations.SystemService;
- import org.androidannotations.annotations.UiThread;
- import org.androidannotations.annotations.res.StringRes;
- import org.apache.commons.lang3.StringUtils;
- import java.io.File;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import java.util.Locale;
- import br.com.sapereaude.maskedEditText.MaskedEditText;
- import static org.androidannotations.annotations.UiThread.Propagation.REUSE;
- /**
- * Created by egslava on 16/01/15.
- */
- @EBean
- public class ApiHelper {
- public AQuery aq;
- @RootContext Context context;
- @SystemService ConnectivityManager connectivityManager;
- @StringRes String loading, pleaseWait;
- private ProgressDialog dialog;
- @AfterInject void init(){ aq = new AQuery(context); }
- public boolean cacheMiss( ParseException e) {
- return e != null && e.getCode() == ParseException.CACHE_MISS;
- }
- public boolean showFail(ParseException e) {
- if (e == null) return false;
- showDialog(e.getLocalizedMessage());
- return true;
- }
- public void showDialog( String message ) {
- new AlertDialog.Builder( context )
- .setTitle(android.R.string.dialog_alert_title)
- .setMessage(message)
- .setPositiveButton(android.R.string.ok, null)
- .show();
- }
- public void confirm( String message, DialogInterface.OnClickListener onOk) {
- new AlertDialog.Builder( context )
- .setTitle(android.R.string.dialog_alert_title)
- .setMessage(message)
- .setPositiveButton(android.R.string.ok, onOk)
- .setNegativeButton(android.R.string.cancel, null)
- .show();
- }
- public void resetErrors(TextView... fields){
- for(TextView field: fields){
- field.setError(null);
- }
- }
- /** Return true if user is logged in */
- public boolean checkLogin(final Activity activityToFinish){
- if (ParseUser.getCurrentUser() == null){
- new AlertDialog.Builder(context)
- .setTitle(android.R.string.dialog_alert_title)
- .setMessage(R.string.you_arent_logged_in)
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- LoginActivity_.intent(context).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
- activityToFinish.finish();
- }
- }).setNegativeButton(android.R.string.cancel, null)
- .show();
- return false;
- }else{
- return true;
- }
- };
- /**
- * @param fields for a checking
- * @return true if it's all right
- */
- public boolean validateRequired(TextView... fields){
- boolean result = true;
- for (TextView field : fields){
- String value = field.getText().toString();
- if (value.isEmpty()){
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
- field.setError(Html.fromHtml("<font color='white'>" + context.getString(R.string.the_field_is_required) + "</font>"));
- else
- field.setError(Html.fromHtml("<font color='black'>" + context.getString(R.string.the_field_is_required) + "</font>"));
- result = false;
- }
- }
- return result;
- }
- public static void map(TextView textView, String value, boolean changeVisibility){
- if (StringUtils.isBlank(value)){
- if (changeVisibility)
- textView.setVisibility(View.GONE);
- }else{
- if (changeVisibility)
- textView.setVisibility(View.VISIBLE);
- textView.setText(value);
- }
- };
- public static DateFormat format = SimpleDateFormat.getDateInstance();
- public void map (TextView textView, Date value){
- if (value == null)return;
- if (textView == null)return;
- String formattedDate = format.format(value);
- map(textView, formattedDate, false);
- }
- @StringRes String notSet;
- public void unmapDate (ParseObject object, String dateField, TextView value){
- if (value == null)return;
- if (dateField == null)return;
- if (object == null)return;
- if (value.getText().toString().equals(notSet))return;
- Date date = null;
- try {
- date = format.parse(value.getText().toString());
- } catch (java.text.ParseException e) {
- throw new RuntimeException(e);
- }
- if (date == null) return;
- object.put(dateField, date);
- }
- public static void map (TextView textView, String value){
- map(textView, value, true);
- }
- begn
- public boolean validateMask(MaskedEditText maskedEditText){
- if (maskedEditText.getMask().length() != maskedEditText.getText().toString().replaceAll(" ", "").length()){
- maskedEditText.setError( context.getString(R.string.value_is_not_correct));
- return false;
- }
- return true;
- }
- public boolean validatePassword(TextView password, TextView validation){
- if (!password.getText().toString().equals(validation.getText().toString())){
- validation.setError( context.getString(R.string.password_and_confirmation_should_be_the_same) );
- return false;
- }else{
- return true;
- }
- }
- public boolean beginQueries( ){
- return beginQueries( loading );
- }
- // @UiThread(propagation = REUSE) can not be used because of boolean result
- public boolean beginQueries( String message ){
- if (!checkConnection()) return false;
- if (dialog == null){
- dialog = ProgressDialog.show(context, pleaseWait, message);
- } else {
- dialog.setMessage(message);
- }
- return true;
- }
- public boolean checkConnection() {
- if (!isNetworkConnected()){
- showDialog( context.getString(R.string.app_requires_internet ));
- return false;
- }
- return true;
- }
- @UiThread(propagation = REUSE)
- public void endQueries(){
- if (dialog != null) dialog.cancel();
- dialog = null;
- }
- public boolean isNetworkConnected() {
- NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
- return ni != null;
- }
- public Uri tempUri(){
- File avatar_cropped = new File(context.getCacheDir(), "avatar_cropped");
- Uri avatarUri = Uri.fromFile(avatar_cropped);
- return avatarUri;
- }
- @StringRes String sendEmail, callPhone;
- public void sendEmail(String recipient, String subject, String text){
- Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", recipient, null));
- i.putExtra(Intent.EXTRA_SUBJECT, subject );
- i.putExtra(Intent.EXTRA_TEXT , text);
- context.startActivity(i);
- }
- public static <T extends ParseObject> List<T> uncorrupted(List<T> source, Class<T> clazz) {
- ArrayList<T> result = new ArrayList<>();
- if (source == null) return result;
- for (Object object: source) if (object != null && object.getClass().equals(clazz)) {
- result.add( (T)object );
- }
- return result;
- }
- public static List<Photo> ok_photos(ParseObject obj, String key) {
- ArrayList<Photo> result = new ArrayList<>();
- List<Photo> photos = obj.getList(key);
- if (photos == null) return result;
- for (Object object: photos)
- if (object != null && object instanceof Photo && ((Photo) object).getPhoto() != null) {
- result.add( (Photo)object );
- }
- return result;
- }
- public static List<Like> ok_likes(ParseObject obj, String key) {
- ArrayList<Like> result = new ArrayList<>();
- List<Like> likes = obj.getList(key);
- if (likes == null) return result;
- for (Object object: likes)
- if (object != null && object instanceof Like ) {
- Like like = (Like) object;
- if (like.getUser() == null)continue;
- result.add((Like) object);
- }
- return result;
- }
- public static List<Comment> ok_comments(ParseObject obj, String key) {
- ArrayList<Comment> result = new ArrayList<>();
- List<Comment> comments = obj.getList(key);
- if (comments == null) return result;
- for (Object object: comments)
- if (object != null && object instanceof Comment) {
- Comment comment = (Comment) object;
- if ( comment.getComment() == null || comment.getUser() == null ) continue;
- result.add( comment );
- }
- return result;
- }
- public void callPhone(String number){
- Intent callIntent = new Intent(Intent.ACTION_DIAL);
- callIntent.setData(Uri.parse("tel:"+number));
- context.startActivity(callIntent);
- }
- public static final SimpleDateFormat YEARS = new SimpleDateFormat("yyyy");
- public static final SimpleDateFormat TIME = new SimpleDateFormat("hh:mm a");
- public static final SimpleDateFormat DATE = new SimpleDateFormat("dd MMMM");
- public static final int YESTERDAY = R.string.yesterday;
- public static final int TODAY = R.string.today;
- public String getFormattedDate(Date date){ return getFormattedDate(context, date);}
- public static String getFormattedDate(Context context, Date date){
- StringBuilder result = new StringBuilder();
- Calendar cal = Calendar.getInstance();
- // Date date = getCreatedAt();
- if (date == null) date = new Date(); // new message
- cal.setTime(date);
- Calendar today = Calendar.getInstance();
- Calendar yesterday = Calendar.getInstance();
- yesterday.add(Calendar.DAY_OF_YEAR, -1);
- if ( cal.get(Calendar.DATE) == today.get(Calendar.DATE))
- result.append( context.getString( R.string.today ) );
- else if (cal.get(Calendar.DATE) == yesterday.get( Calendar.DATE ))
- result.append( context.getString( R.string.yesterday ) );
- else {
- result.append( DATE.format(date) );
- if (cal.get(Calendar.YEAR) != today.get(Calendar.YEAR)){
- result.append(" ");
- result.append( YEARS.format(date) );
- }
- }
- result.append(" ");
- result.append(TIME.format( date ));
- return result.toString();
- }
- public String local;
- public static String local(){
- String local = String.format("texts_%s", Locale.getDefault().getLanguage());
- return local;
- }
- public static String local(ParseObject obj, String key){
- if(!obj.has(local()) ||
- obj.getParseObject(local()) == null ||
- !obj.getParseObject(local()).has(key))
- return obj.getString(key);
- return obj.getParseObject(local()).getString(key);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment