egslava

Untitled

Mar 22nd, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.18 KB | None | 0 0
  1. package com.egslava.ma.ui.login;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.ProgressDialog;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.net.ConnectivityManager;
  10. import android.net.NetworkInfo;
  11. import android.net.Uri;
  12. import android.os.Build;
  13. import android.text.Html;
  14. import android.view.View;
  15. import android.widget.TextView;
  16.  
  17. import com.androidquery.AQuery;
  18. import com.egslava.ma.R;
  19. import com.egslava.ma.api.Comment;
  20. import com.egslava.ma.api.Like;
  21. import com.egslava.ma.api.Photo;
  22. import com.parse.ParseException;
  23. import com.parse.ParseObject;
  24. import com.parse.ParseUser;
  25.  
  26. import org.androidannotations.annotations.AfterInject;
  27. import org.androidannotations.annotations.EBean;
  28. import org.androidannotations.annotations.RootContext;
  29. import org.androidannotations.annotations.SystemService;
  30. import org.androidannotations.annotations.UiThread;
  31. import org.androidannotations.annotations.res.StringRes;
  32. import org.apache.commons.lang3.StringUtils;
  33.  
  34. import java.io.File;
  35. import java.text.DateFormat;
  36. import java.text.SimpleDateFormat;
  37. import java.util.ArrayList;
  38. import java.util.Calendar;
  39. import java.util.Date;
  40. import java.util.List;
  41. import java.util.Locale;
  42.  
  43. import br.com.sapereaude.maskedEditText.MaskedEditText;
  44.  
  45. import static org.androidannotations.annotations.UiThread.Propagation.REUSE;
  46.  
  47. /**
  48.  * Created by egslava on 16/01/15.
  49.  */
  50. @EBean
  51. public class ApiHelper {
  52.  
  53.     public AQuery aq;
  54.  
  55.     @RootContext Context context;
  56.     @SystemService ConnectivityManager connectivityManager;
  57.     @StringRes String loading, pleaseWait;
  58.  
  59.     private ProgressDialog dialog;
  60.  
  61.     @AfterInject void init(){ aq = new AQuery(context); }
  62.  
  63.     public boolean cacheMiss( ParseException e) {
  64.         return e != null && e.getCode() == ParseException.CACHE_MISS;
  65.     }
  66.     public boolean showFail(ParseException e) {
  67.         if (e == null) return false;
  68.  
  69.         showDialog(e.getLocalizedMessage());
  70.         return true;
  71.     }
  72.  
  73.     public void showDialog( String message ) {
  74.         new AlertDialog.Builder( context )
  75.                 .setTitle(android.R.string.dialog_alert_title)
  76.                 .setMessage(message)
  77.                 .setPositiveButton(android.R.string.ok, null)
  78.                 .show();
  79.     }
  80.  
  81.     public void confirm( String message, DialogInterface.OnClickListener onOk) {
  82.         new AlertDialog.Builder( context )
  83.                 .setTitle(android.R.string.dialog_alert_title)
  84.                 .setMessage(message)
  85.                 .setPositiveButton(android.R.string.ok, onOk)
  86.                 .setNegativeButton(android.R.string.cancel, null)
  87.                 .show();
  88.     }
  89.  
  90.     public void resetErrors(TextView... fields){
  91.         for(TextView field: fields){
  92.             field.setError(null);
  93.         }
  94.     }
  95.  
  96.     /** Return true if user is logged in */
  97.     public boolean checkLogin(final Activity activityToFinish){
  98.  
  99.         if (ParseUser.getCurrentUser() == null){
  100.             new AlertDialog.Builder(context)
  101.                     .setTitle(android.R.string.dialog_alert_title)
  102.                     .setMessage(R.string.you_arent_logged_in)
  103.                     .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  104.                         public void onClick(DialogInterface dialog, int which) {
  105.                             LoginActivity_.intent(context).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
  106.                             activityToFinish.finish();
  107.                         }
  108.                     }).setNegativeButton(android.R.string.cancel, null)
  109.                     .show();
  110.             return false;
  111.         }else{
  112.             return true;
  113.         }
  114.  
  115.     };
  116.  
  117.     /**
  118.      * @param fields for a checking
  119.      * @return true if it's all right
  120.      */
  121.     public boolean validateRequired(TextView... fields){
  122.         boolean result = true;
  123.         for (TextView field : fields){
  124.             String value = field.getText().toString();
  125.             if (value.isEmpty()){
  126.                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  127.                     field.setError(Html.fromHtml("<font color='white'>" + context.getString(R.string.the_field_is_required) + "</font>"));
  128.                 else
  129.                     field.setError(Html.fromHtml("<font color='black'>" + context.getString(R.string.the_field_is_required) + "</font>"));
  130.  
  131.                 result = false;
  132.             }
  133.         }
  134.  
  135.         return result;
  136.     }
  137.  
  138.     public static void map(TextView textView, String value, boolean changeVisibility){
  139.         if (StringUtils.isBlank(value)){
  140.  
  141.             if (changeVisibility)
  142.                 textView.setVisibility(View.GONE);
  143.         }else{
  144.             if (changeVisibility)
  145.                 textView.setVisibility(View.VISIBLE);
  146.             textView.setText(value);
  147.         }
  148.     };
  149.  
  150.     public static DateFormat format = SimpleDateFormat.getDateInstance();
  151.     public void map (TextView textView, Date value){
  152.         if (value       == null)return;
  153.         if (textView    == null)return;
  154.  
  155.         String formattedDate = format.format(value);
  156.  
  157.         map(textView, formattedDate, false);
  158.     }
  159.  
  160.     @StringRes String notSet;
  161.  
  162.     public void unmapDate (ParseObject object, String dateField, TextView value){
  163.         if (value       == null)return;
  164.         if (dateField    == null)return;
  165.         if (object    == null)return;
  166.  
  167.         if (value.getText().toString().equals(notSet))return;
  168.  
  169.         Date date = null;
  170.         try {
  171.              date = format.parse(value.getText().toString());
  172.         } catch (java.text.ParseException e) {
  173.             throw new RuntimeException(e);
  174.         }
  175.  
  176.         if (date == null) return;
  177.  
  178.         object.put(dateField, date);
  179.     }
  180.  
  181.     public static void map (TextView textView, String value){
  182.         map(textView, value, true);
  183.     }
  184. begn
  185.     public boolean validateMask(MaskedEditText maskedEditText){
  186.         if (maskedEditText.getMask().length() != maskedEditText.getText().toString().replaceAll(" ", "").length()){
  187.             maskedEditText.setError( context.getString(R.string.value_is_not_correct));
  188.             return false;
  189.         }
  190.         return true;
  191.     }
  192.  
  193.     public boolean validatePassword(TextView password, TextView validation){
  194.         if (!password.getText().toString().equals(validation.getText().toString())){
  195.             validation.setError( context.getString(R.string.password_and_confirmation_should_be_the_same) );
  196.             return false;
  197.         }else{
  198.             return true;
  199.         }
  200.     }
  201.  
  202.     public boolean beginQueries( ){
  203.         return beginQueries( loading );
  204.     }
  205.  
  206.     //    @UiThread(propagation = REUSE)  can not be used because of boolean result
  207.     public boolean beginQueries( String message ){
  208.         if (!checkConnection()) return false;
  209.  
  210.         if (dialog == null){
  211.             dialog = ProgressDialog.show(context, pleaseWait, message);
  212.         } else {
  213.             dialog.setMessage(message);
  214.         }
  215.  
  216.         return true;
  217.     }
  218.  
  219.     public boolean checkConnection() {
  220.         if (!isNetworkConnected()){
  221.             showDialog( context.getString(R.string.app_requires_internet ));
  222.             return false;
  223.         }
  224.         return true;
  225.     }
  226.  
  227.     @UiThread(propagation = REUSE)
  228.     public void endQueries(){
  229.         if (dialog != null) dialog.cancel();
  230.         dialog = null;
  231.     }
  232.  
  233.     public boolean isNetworkConnected() {
  234.         NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
  235.         return ni != null;
  236.     }
  237.  
  238.     public Uri tempUri(){
  239.         File avatar_cropped = new File(context.getCacheDir(), "avatar_cropped");
  240.         Uri avatarUri = Uri.fromFile(avatar_cropped);
  241.         return avatarUri;
  242.     }
  243.  
  244.     @StringRes  String sendEmail, callPhone;
  245.     public void sendEmail(String recipient, String subject, String text){
  246.         Intent i = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", recipient, null));
  247.         i.putExtra(Intent.EXTRA_SUBJECT, subject );
  248.         i.putExtra(Intent.EXTRA_TEXT   , text);
  249.         context.startActivity(i);
  250.     }
  251.  
  252.     public static <T extends ParseObject> List<T> uncorrupted(List<T> source, Class<T> clazz) {
  253.         ArrayList<T> result = new ArrayList<>();
  254.         if (source == null) return result;
  255.         for (Object object: source) if (object != null && object.getClass().equals(clazz)) {
  256.             result.add( (T)object );
  257.         }
  258.         return result;
  259.     }
  260.  
  261.     public static List<Photo> ok_photos(ParseObject obj, String key) {
  262.         ArrayList<Photo> result = new ArrayList<>();
  263.         List<Photo> photos = obj.getList(key);
  264.         if (photos == null) return result;
  265.         for (Object object: photos)
  266.             if (object != null && object instanceof Photo && ((Photo) object).getPhoto() != null) {
  267.             result.add( (Photo)object );
  268.         }
  269.         return result;
  270.     }
  271.  
  272.     public static List<Like> ok_likes(ParseObject obj, String key) {
  273.         ArrayList<Like> result = new ArrayList<>();
  274.         List<Like> likes = obj.getList(key);
  275.         if (likes == null) return result;
  276.         for (Object object: likes)
  277.             if (object != null && object instanceof Like ) {
  278.                 Like like = (Like) object;
  279.  
  280.                 if (like.getUser() == null)continue;
  281.                 result.add((Like) object);
  282.             }
  283.         return result;
  284.     }
  285.  
  286.     public static List<Comment> ok_comments(ParseObject obj, String key) {
  287.         ArrayList<Comment> result = new ArrayList<>();
  288.         List<Comment> comments = obj.getList(key);
  289.         if (comments == null) return result;
  290.         for (Object object: comments)
  291.         if (object != null && object instanceof Comment) {
  292.             Comment comment = (Comment) object;
  293.             if ( comment.getComment() == null || comment.getUser() == null ) continue;
  294.             result.add( comment );
  295.         }
  296.         return result;
  297.     }
  298.  
  299.     public void callPhone(String number){
  300.         Intent callIntent = new Intent(Intent.ACTION_DIAL);
  301.         callIntent.setData(Uri.parse("tel:"+number));
  302.         context.startActivity(callIntent);
  303.     }
  304.  
  305.     public static final SimpleDateFormat YEARS    = new SimpleDateFormat("yyyy");
  306.     public static final SimpleDateFormat TIME     = new SimpleDateFormat("hh:mm a");
  307.     public static final SimpleDateFormat DATE     = new SimpleDateFormat("dd MMMM");
  308.     public static final int YESTERDAY   = R.string.yesterday;
  309.     public static final int TODAY       = R.string.today;
  310.  
  311.     public String getFormattedDate(Date date){ return getFormattedDate(context, date);}
  312.     public static String getFormattedDate(Context context, Date date){
  313.         StringBuilder   result = new StringBuilder();
  314.         Calendar cal = Calendar.getInstance();
  315. //        Date date = getCreatedAt();
  316.         if (date == null) date = new Date();    // new message
  317.         cal.setTime(date);
  318.  
  319.         Calendar today      = Calendar.getInstance();
  320.         Calendar yesterday  = Calendar.getInstance();
  321.         yesterday.add(Calendar.DAY_OF_YEAR, -1);
  322.  
  323.         if ( cal.get(Calendar.DATE) == today.get(Calendar.DATE))
  324.             result.append( context.getString( R.string.today ) );
  325.         else if (cal.get(Calendar.DATE) == yesterday.get( Calendar.DATE ))
  326.             result.append( context.getString( R.string.yesterday ) );
  327.         else {
  328.             result.append( DATE.format(date) );
  329.             if (cal.get(Calendar.YEAR) != today.get(Calendar.YEAR)){
  330.                 result.append(" ");
  331.                 result.append( YEARS.format(date) );
  332.             }
  333.         }
  334.  
  335.         result.append(" ");
  336.         result.append(TIME.format( date ));
  337.  
  338.         return result.toString();
  339.     }
  340.  
  341.     public String local;
  342.     public static String local(){
  343.         String local = String.format("texts_%s", Locale.getDefault().getLanguage());
  344.         return local;
  345.     }
  346.  
  347.     public static String local(ParseObject obj, String key){
  348.         if(!obj.has(local()) ||
  349.                 obj.getParseObject(local()) == null ||
  350.                 !obj.getParseObject(local()).has(key))
  351.             return obj.getString(key);
  352.         return obj.getParseObject(local()).getString(key);
  353.     }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment