Advertisement
mmayoub

Tasks manager using backendless database, 31.10.2017

Oct 31st, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 42.93 KB | None | 0 0
  1. Task.java
  2. ---------
  3. package com.example.mohamadpc.backendless;
  4.  
  5. import com.backendless.Backendless;
  6. import com.backendless.async.callback.AsyncCallback;
  7. import com.backendless.persistence.DataQueryBuilder;
  8.  
  9. import java.util.List;
  10.  
  11. public class Task {
  12.     private java.util.Date updated;
  13.     private Integer Priorty;
  14.     private String ownerId;
  15.     private java.util.Date closed;
  16.     private java.util.Date target;
  17.     private String name;
  18.     private java.util.Date created;
  19.     private String objectId;
  20.     private boolean done;
  21.  
  22.     public void setOwnerId(String ownerId) {
  23.         this.ownerId = ownerId;
  24.     }
  25.  
  26.     public boolean getDone() {
  27.         return done;
  28.     }
  29.  
  30.     public void setDone(boolean done) {
  31.         this.done = done;
  32.     }
  33.  
  34.     public java.util.Date getUpdated() {
  35.         return updated;
  36.     }
  37.  
  38.     public Integer getPriorty() {
  39.         return Priorty;
  40.     }
  41.  
  42.     public void setPriorty(Integer Priorty) {
  43.         this.Priorty = Priorty;
  44.     }
  45.  
  46.     public String getOwnerId() {
  47.         return ownerId;
  48.     }
  49.  
  50.     public java.util.Date getClosed() {
  51.         return closed;
  52.     }
  53.  
  54.     public void setClosed(java.util.Date closed) {
  55.         this.closed = closed;
  56.     }
  57.  
  58.     public java.util.Date getTarget() {
  59.         return target;
  60.     }
  61.  
  62.     public void setTarget(java.util.Date target) {
  63.         this.target = target;
  64.     }
  65.  
  66.     public String getName() {
  67.         return name;
  68.     }
  69.  
  70.     public void setName(String name) {
  71.         this.name = name;
  72.     }
  73.  
  74.     public java.util.Date getCreated() {
  75.         return created;
  76.     }
  77.  
  78.     public String getObjectId() {
  79.         return objectId;
  80.     }
  81.  
  82.     public Task save() {
  83.         return Backendless.Data.of(Task.class).save(this);
  84.     }
  85.  
  86.     public void saveAsync(AsyncCallback<Task> callback) {
  87.         Backendless.Data.of(Task.class).save(this, callback);
  88.     }
  89.  
  90.     public Long remove() {
  91.         return Backendless.Data.of(Task.class).remove(this);
  92.     }
  93.  
  94.     public void removeAsync(AsyncCallback<Long> callback) {
  95.         Backendless.Data.of(Task.class).remove(this, callback);
  96.     }
  97.  
  98.     public static Task findById(String id) {
  99.         return Backendless.Data.of(Task.class).findById(id);
  100.     }
  101.  
  102.     public static void findByIdAsync(String id, AsyncCallback<Task> callback) {
  103.         Backendless.Data.of(Task.class).findById(id, callback);
  104.     }
  105.  
  106.     public static Task findFirst() {
  107.         return Backendless.Data.of(Task.class).findFirst();
  108.     }
  109.  
  110.     public static void findFirstAsync(AsyncCallback<Task> callback) {
  111.         Backendless.Data.of(Task.class).findFirst(callback);
  112.     }
  113.  
  114.     public static Task findLast() {
  115.         return Backendless.Data.of(Task.class).findLast();
  116.     }
  117.  
  118.     public static void findLastAsync(AsyncCallback<Task> callback) {
  119.         Backendless.Data.of(Task.class).findLast(callback);
  120.     }
  121.  
  122.     public static List<Task> find(DataQueryBuilder queryBuilder) {
  123.         return Backendless.Data.of(Task.class).find(queryBuilder);
  124.     }
  125.  
  126.     public static void findAsync(DataQueryBuilder queryBuilder, AsyncCallback<List<Task>> callback) {
  127.         Backendless.Data.of(Task.class).find(queryBuilder, callback);
  128.     }
  129. }
  130.  
  131. RegisterActivity.java
  132. ----------------------
  133. package com.example.mohamadpc.backendless;
  134.  
  135. import android.app.Activity;
  136. import android.app.ProgressDialog;
  137. import android.content.Context;
  138. import android.os.Bundle;
  139. import android.view.View;
  140. import android.widget.EditText;
  141. import android.widget.Toast;
  142.  
  143. import com.backendless.Backendless;
  144. import com.backendless.BackendlessUser;
  145. import com.backendless.async.callback.AsyncCallback;
  146. import com.backendless.exceptions.BackendlessFault;
  147.  
  148. public class RegisterActivity extends Activity implements View.OnClickListener {
  149.     private Context context;
  150.     private EditText etUserEmail, etUserName, etPassword, etReTypePassword;
  151.  
  152.     @Override
  153.     protected void onCreate(Bundle savedInstanceState) {
  154.         super.onCreate(savedInstanceState);
  155.         setContentView(R.layout.activity_register);
  156.  
  157.         setPointer();
  158.     }
  159.  
  160.     private void setPointer() {
  161.         this.context = this;
  162.  
  163.         etUserEmail = (EditText) findViewById(R.id.etRegisterUserEmail);
  164.         etUserName = (EditText) findViewById(R.id.etRegisterUserName);
  165.         etPassword = (EditText) findViewById(R.id.etRegisterPassword);
  166.         etReTypePassword = (EditText) findViewById(R.id.etRegisterReTypePassword);
  167.  
  168.         findViewById(R.id.btnRegisterRgister).setOnClickListener(this);
  169.         findViewById(R.id.btnRegisterCancel).setOnClickListener(this);
  170.     }
  171.  
  172.     @Override
  173.     public void onClick(View view) {
  174.         switch (view.getId()) {
  175.             case R.id.btnRegisterCancel:
  176.                 cancel();
  177.                 break;
  178.             case R.id.btnRegisterRgister:
  179.                 String userEmail = etUserEmail.getText().toString();
  180.                 String userName = etUserName.getText().toString();
  181.                 String password = etPassword.getText().toString();
  182.                 String reTypePassword = etReTypePassword.getText().toString();
  183.  
  184.                 if (isValidInput(userEmail, userName, password, reTypePassword))
  185.                     register(userEmail, userName, password);
  186.                 break;
  187.             default:
  188.                 Toast.makeText(this, "Unknown click event on " + view.getId(), Toast.LENGTH_SHORT).show();
  189.         }
  190.     }
  191.  
  192.     private boolean isValidInput(String userEmail, String userName, String password, String reTypePassword) {
  193.         // no empty fields
  194.         if (userEmail.isEmpty() || userName.isEmpty() ||
  195.                 password.isEmpty() ||
  196.                 reTypePassword.isEmpty()) {
  197.             Toast.makeText(this, "Please fill all fields", Toast.LENGTH_LONG).show();
  198.             return false;
  199.         }
  200.  
  201.         // password match
  202.         if (!password.equals(reTypePassword)) {
  203.             Toast.makeText(this, "password fields do not match", Toast.LENGTH_LONG).show();
  204.             return false;
  205.         }
  206.  
  207.         // minimum password length
  208.         if (password.length() < 3) {
  209.             Toast.makeText(this, "password is too short", Toast.LENGTH_LONG).show();
  210.             return false;
  211.         }
  212.         // valid email address
  213.         if (!android.util.Patterns.EMAIL_ADDRESS.matcher(userEmail).matches()) {
  214.             Toast.makeText(this, "invalid email address", Toast.LENGTH_LONG).show();
  215.             return false;
  216.         }
  217.  
  218.         return true;
  219.  
  220.     }
  221.  
  222.  
  223.     private void cancel() {
  224.         this.finish();
  225.     }
  226.  
  227.     private void register(String userEmail, String userName, String password) {
  228.         if (userEmail.isEmpty() || userName.isEmpty() || password.isEmpty()) {
  229.             Toast.makeText(context, "missing data for registrationS", Toast.LENGTH_LONG).show();
  230.             return;
  231.         }
  232.         final ProgressDialog pd = new ProgressDialog(context);
  233.         pd.setMessage("Registering . . .");
  234.         pd.show();
  235.  
  236.         BackendlessUser user = new BackendlessUser();
  237.         user.setEmail(userEmail);
  238.         user.setProperty("name", userName);
  239.         user.setPassword(password);
  240.  
  241.         AsyncCallback<BackendlessUser> registerAsyncCallBack = new AsyncCallback<BackendlessUser>() {
  242.             @Override
  243.             public void handleResponse(BackendlessUser response) {
  244.                 pd.dismiss();
  245.                 finish();
  246.             }
  247.  
  248.             @Override
  249.             public void handleFault(BackendlessFault fault) {
  250.                 pd.cancel();
  251.                 Toast.makeText(context, "register faild", Toast.LENGTH_LONG).show();
  252.                 finish();
  253.             }
  254.         };
  255.  
  256.         Backendless.UserService.register(user, registerAsyncCallBack);
  257.     }
  258. }
  259.  
  260. LoginActivity.java
  261. -------------------
  262. package com.example.mohamadpc.backendless;
  263.  
  264. import android.app.Activity;
  265. import android.app.ProgressDialog;
  266. import android.content.Context;
  267. import android.content.Intent;
  268. import android.os.Bundle;
  269. import android.view.View;
  270. import android.widget.EditText;
  271. import android.widget.Toast;
  272.  
  273. import com.backendless.Backendless;
  274. import com.backendless.BackendlessUser;
  275. import com.backendless.async.callback.AsyncCallback;
  276. import com.backendless.exceptions.BackendlessFault;
  277.  
  278. public class LoginActivity extends Activity implements View.OnClickListener {
  279.     private Context context;
  280.     private EditText etUserName, etPassword;
  281.  
  282.     @Override
  283.     protected void onCreate(Bundle savedInstanceState) {
  284.         super.onCreate(savedInstanceState);
  285.         setContentView(R.layout.activity_login);
  286.  
  287.         setPointer();
  288.     }
  289.  
  290.     private void setPointer() {
  291.         this.context = this;
  292.         etUserName = (EditText) findViewById(R.id.etLoginUserName);
  293.         etPassword = (EditText) findViewById(R.id.etLoginUserPassword);
  294.  
  295.         // register button to on click listener
  296.         findViewById(R.id.btnLoginLogin).setOnClickListener(this);
  297.         findViewById(R.id.btnLoginRegister).setOnClickListener(this);
  298.  
  299.         // initialize connection to BackEndLess database
  300.         String applicationId = context.getResources().getString(R.string.application_Id);
  301.         String apiKey = context.getResources().getString(R.string.api_key);
  302.         Backendless.initApp(getApplicationContext(), applicationId, apiKey);
  303.     }
  304.  
  305.     @Override
  306.     public void onClick(View view) {
  307.         switch (view.getId()) {
  308.             case R.id.btnLoginLogin:
  309.                 login();
  310.                 break;
  311.  
  312.             case R.id.btnLoginRegister:
  313.                 register();
  314.                 break;
  315.             default:
  316.                 Toast.makeText(this, "Unhandled Click on " + view.getId(), Toast.LENGTH_SHORT).show();
  317.         }
  318.     }
  319.  
  320.     private void register() {
  321.         startActivity(new Intent(context, RegisterActivity.class));
  322.     }
  323.  
  324.     private void login() {
  325.         final String userName, password;
  326.         userName = etUserName.getText().toString();
  327.         password = etPassword.getText().toString();
  328.  
  329.         if (userName.isEmpty() || password.isEmpty()) {
  330.             Toast.makeText(context, "Please fill in all your details!", Toast.LENGTH_LONG).show();
  331.             return;
  332.         }
  333.         final ProgressDialog pd = new ProgressDialog(context);
  334.         pd.setMessage("Login in progress . . .");
  335.         pd.show();
  336.         AsyncCallback<BackendlessUser> loginAsyncCallBack = new AsyncCallback<BackendlessUser>() {
  337.             @Override
  338.             public void handleResponse(BackendlessUser response) {
  339.                 Intent intent = new Intent(context, TasksActivity.class);
  340.                 intent.putExtra("currentUserName", response.getProperty("name") + "");
  341.                 intent.putExtra("currentUserId", response.getUserId());
  342.  
  343.                 pd.dismiss();
  344.                 finish();
  345.                 startActivity(intent);
  346.             }
  347.  
  348.             @Override
  349.             public void handleFault(BackendlessFault fault) {
  350.                 pd.cancel();
  351.                 etUserName.setText("");
  352.                 etPassword.setText("");
  353.                 Toast.makeText(context, "Login failed", Toast.LENGTH_LONG).show();
  354.  
  355.             }
  356.         };
  357.  
  358.         Backendless.UserService.login(userName, password, loginAsyncCallBack, true);
  359.     }
  360. }
  361.  
  362. TasksActivity.java
  363. -------------------
  364. package com.example.mohamadpc.backendless;
  365.  
  366. import android.app.Activity;
  367. import android.app.AlertDialog;
  368. import android.app.ProgressDialog;
  369. import android.content.Context;
  370. import android.content.DialogInterface;
  371. import android.content.Intent;
  372. import android.graphics.Color;
  373. import android.os.Bundle;
  374. import android.view.LayoutInflater;
  375. import android.view.View;
  376. import android.widget.AdapterView;
  377. import android.widget.EditText;
  378. import android.widget.ListView;
  379. import android.widget.Toast;
  380.  
  381. import com.backendless.Backendless;
  382. import com.backendless.async.callback.AsyncCallback;
  383. import com.backendless.exceptions.BackendlessFault;
  384. import com.backendless.persistence.DataQueryBuilder;
  385. import com.nightonke.boommenu.BoomButtons.OnBMClickListener;
  386. import com.nightonke.boommenu.BoomButtons.TextOutsideCircleButton;
  387. import com.nightonke.boommenu.BoomMenuButton;
  388. import com.nightonke.boommenu.ButtonEnum;
  389.  
  390. import java.util.List;
  391.  
  392. public class TasksActivity extends Activity {
  393.     private String userName, userId;
  394.     private Context context;
  395.     private List<Task> myTasks;
  396.     private ListView myTaskList;
  397.     private BoomMenuButton bmb;
  398.  
  399.     @Override
  400.     protected void onCreate(Bundle savedInstanceState) {
  401.         super.onCreate(savedInstanceState);
  402.         setContentView(R.layout.activity_tasks);
  403.  
  404.         setPointer();
  405.         getIntentData();
  406.         getUserTasks();
  407.     }
  408.  
  409.     private void setPointer() {
  410.         this.context = this;
  411.  
  412.         myTaskList = (ListView) findViewById(R.id.lstTasks);
  413.         bmb = (BoomMenuButton) findViewById(R.id.btnboomMenu);
  414.         buildBoomMenu();
  415.  
  416.         myTaskList.setAdapter(new TasksAdapter(myTasks, context));
  417.         myTaskList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  418.             @Override
  419.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  420.                 myTaskList.setItemChecked(position, true);
  421.             }
  422.         });
  423.         myTaskList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  424.             @Override
  425.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  426.                 myTaskList.setItemChecked(position, true);
  427.             }
  428.  
  429.             @Override
  430.             public void onNothingSelected(AdapterView<?> parent) {
  431.  
  432.             }
  433.         });
  434.     }
  435.  
  436.     private void getUserTasks() {
  437.         final ProgressDialog pd = new ProgressDialog(context);
  438.         pd.show();
  439.  
  440.         DataQueryBuilder queryBuilder = DataQueryBuilder.create();
  441.         String whereClause = "ownerId='" + Backendless.UserService.loggedInUser() + "'";
  442.         queryBuilder.setWhereClause(whereClause);
  443.         //if we dont tell backendless the size of return records, it will be by defualt 10 result
  444.         queryBuilder.setPageSize(100);
  445.         //get our data from the database......
  446.         Backendless.Data.of(Task.class).find(queryBuilder, new AsyncCallback<List<Task>>() {
  447.             @Override
  448.             public void handleResponse(List<Task> response) {
  449.                 myTasks = response;
  450.                 myTaskList.setAdapter(new TasksAdapter(myTasks, context));
  451.                 pd.dismiss();
  452.                 Toast.makeText(context, response.size() + " tasks was loaded from server", Toast.LENGTH_LONG).show();
  453.             }
  454.  
  455.             @Override
  456.             public void handleFault(BackendlessFault fault) {
  457.                 myTasks = null;
  458.                 Toast.makeText(context, "Error loading tasks from server !!!", Toast.LENGTH_LONG).show();
  459.                 pd.dismiss();
  460.                 finish();
  461.             }
  462.         });
  463.     }
  464.  
  465.     private void buildBoomMenu() {
  466.         bmb.setButtonEnum(ButtonEnum.TextOutsideCircle);
  467.         for (int i = 0; i < bmb.getPiecePlaceEnum().pieceNumber(); i += 1) {
  468.             TextOutsideCircleButton.Builder builder = new TextOutsideCircleButton.Builder();
  469.             builder.textSize(16);
  470.             switch (i) {
  471.                 case 0:
  472.                     builder.normalColor(Color.RED);
  473.                     builder.normalImageRes(R.drawable.add_task);
  474.                     builder.normalText("Add");
  475.                     builder.listener(new OnBMClickListener() {
  476.                         @Override
  477.                         public void onBoomButtonClick(int index) {
  478.                             final AlertDialog.Builder addNewTask = new AlertDialog.Builder(context);
  479.                             final View myDialogView = LayoutInflater.from(context).inflate(R.layout.dialog_new_task_layout, null);
  480.  
  481.                             addNewTask.setView(myDialogView);
  482.                             addNewTask.setPositiveButton("Add", new DialogInterface.OnClickListener() {
  483.                                 @Override
  484.                                 public void onClick(final DialogInterface dialogInterface, int i) {
  485.                                     String newTaskName = ((EditText) myDialogView.findViewById(R.id.etNewTaskName)).getText().toString();
  486.                                     if (newTaskName.isEmpty()) {
  487.                                         return;
  488.                                     }
  489.                                     Task newTask = new Task();
  490.                                     newTask.setName(newTaskName);
  491.                                     newTask.setDone(false);
  492.                                     dialogInterface.dismiss();
  493.                                     final ProgressDialog pd = new ProgressDialog(context);
  494.                                     pd.setMessage("adding . . .");
  495.                                     pd.show();
  496.                                     Backendless.Persistence.of(Task.class).save(newTask, new AsyncCallback<Task>() {
  497.                                         @Override
  498.                                         public void handleResponse(Task response) {
  499.                                             myTasks.add(response);
  500.                                             ((TasksAdapter) myTaskList.getAdapter()).notifyDataSetChanged();
  501.                                             pd.dismiss();
  502.                                         }
  503.  
  504.                                         @Override
  505.                                         public void handleFault(BackendlessFault fault) {
  506.                                             pd.dismiss();
  507.                                             Toast.makeText(context, "Error adding new task !!", Toast.LENGTH_LONG).show();
  508.  
  509.                                         }
  510.                                     });
  511.  
  512.                                 }
  513.                             });
  514.                             addNewTask.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  515.                                 @Override
  516.                                 public void onClick(DialogInterface dialogInterface, int i) {
  517.                                     dialogInterface.dismiss();
  518.                                 }
  519.                             });
  520.                             addNewTask.show();
  521.                         }
  522.                     });
  523.                     break;
  524.                 case 1:
  525.                     builder.normalColor(Color.CYAN);
  526.                     builder.normalImageRes(R.drawable.delete_task);
  527.                     builder.normalText("Delete");
  528.                     builder.listener(new OnBMClickListener() {
  529.                         @Override
  530.                         public void onBoomButtonClick(int index) {
  531.                             if (myTaskList.getCheckedItemPosition() > -1) {
  532.                                 final Task selectedTask = myTasks.get(myTaskList.getCheckedItemPosition());
  533.                                 final ProgressDialog pd = new ProgressDialog(context);
  534.                                 pd.setMessage("deleting . . .");
  535.                                 pd.show();
  536.                                 Backendless.Persistence.of(Task.class).remove(selectedTask, new AsyncCallback<Long>() {
  537.                                     @Override
  538.                                     public void handleResponse(Long response) {
  539.                                         myTasks.remove(selectedTask);
  540.                                         ((TasksAdapter) myTaskList.getAdapter()).notifyDataSetChanged();
  541.                                         pd.dismiss();
  542.                                     }
  543.  
  544.                                     @Override
  545.                                     public void handleFault(BackendlessFault fault) {
  546.                                         pd.dismiss();
  547.                                         Toast.makeText(context, "Error deleteing task !!", Toast.LENGTH_LONG).show();
  548.                                     }
  549.                                 });
  550.  
  551.                             } else {
  552.                                 Toast.makeText(context, "Select a task first !", Toast.LENGTH_SHORT).show();
  553.                             }
  554.                         }
  555.                     });
  556.                     break;
  557.  
  558.                 case 2:
  559.                     builder.normalColor(Color.GREEN);
  560.                     builder.normalImageRes(R.drawable.move_task);
  561.                     builder.normalText("Move");
  562.                     builder.listener(new OnBMClickListener() {
  563.                         @Override
  564.                         public void onBoomButtonClick(int index) {
  565.                             startActivityForResult(new Intent(context, SelectUserActivity.class), 1010101);
  566.                         }
  567.                     });
  568.  
  569.                     break;
  570.                 case 3:
  571.                     builder.normalColor(Color.BLACK);
  572.                     builder.normalImageRes(R.drawable.logout);
  573.                     builder.normalText("Logout");
  574.                     builder.listener(new OnBMClickListener() {
  575.                         @Override
  576.                         public void onBoomButtonClick(int index) {
  577.                             Backendless.UserService.logout(new AsyncCallback<Void>() {
  578.                                 @Override
  579.                                 public void handleResponse(Void response) {
  580.                                     TasksActivity.this.finish();
  581.                                     startActivity(new Intent(context, LoginActivity.class));
  582.                                 }
  583.  
  584.                                 @Override
  585.                                 public void handleFault(BackendlessFault fault) {
  586.                                     Toast.makeText(context, "Error logout ...", Toast.LENGTH_SHORT).show();
  587.                                 }
  588.                             });
  589.  
  590.                         }
  591.                     });
  592.                     break;
  593.                 default:
  594.                     builder.normalText("");
  595.                     break;
  596.             }
  597.  
  598.             bmb.addBuilder(builder);
  599.         }
  600.  
  601.     }
  602.  
  603.     private void getIntentData() {
  604.         Intent intent = this.getIntent();
  605.         if (!intent.hasExtra("currentUserName") || !intent.hasExtra("currentUserId")) {
  606.             // user should login or register first
  607.             startActivity(new Intent(context, LoginActivity.class));
  608.             this.finish();
  609.             return;
  610.         }
  611.  
  612.         userName = intent.getStringExtra("currentUserName");
  613.         userId = intent.getStringExtra("currentUserId");
  614.     }
  615.  
  616.     public void addToList(Task response) {
  617.         myTasks.add(response);
  618.     }
  619.  
  620.     @Override
  621.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  622.         super.onActivityResult(requestCode, resultCode, data);
  623.         if (requestCode == 1010101 && resultCode == RESULT_OK) {
  624.             String userId = data.getStringExtra("result");
  625.  
  626.             if (myTaskList.getCheckedItemPosition() > -1) {
  627.                 Task task = (Task) myTasks.get(myTaskList.getCheckedItemPosition());
  628.                 task.setOwnerId(userId);
  629.                 final ProgressDialog pd = new ProgressDialog(context);
  630.                 pd.setTitle("moving task ...");
  631.                 pd.show();
  632.                 Backendless.Persistence.of(Task.class).save(task, new AsyncCallback<Task>() {
  633.                     @Override
  634.                     public void handleResponse(Task response) {
  635.                         myTasks.remove(myTaskList.getCheckedItemPosition());
  636.                         ((TasksAdapter) myTaskList.getAdapter()).notifyDataSetChanged();
  637.                         pd.dismiss();
  638.                     }
  639.  
  640.                     @Override
  641.                     public void handleFault(BackendlessFault fault) {
  642.                         Toast.makeText(TasksActivity.this.context, "Error moving task", Toast.LENGTH_LONG).show();
  643.                         pd.dismiss();
  644.                     }
  645.                 });
  646.             }
  647.         }
  648.     }
  649. }
  650.  
  651. SelectUserActivity.java
  652. ------------------------
  653. package com.example.mohamadpc.backendless;
  654.  
  655. import android.app.Activity;
  656. import android.app.ProgressDialog;
  657. import android.content.Context;
  658. import android.content.Intent;
  659. import android.os.Bundle;
  660. import android.view.View;
  661. import android.widget.AdapterView;
  662. import android.widget.ListView;
  663.  
  664. import com.backendless.Backendless;
  665. import com.backendless.BackendlessUser;
  666. import com.backendless.async.callback.AsyncCallback;
  667. import com.backendless.exceptions.BackendlessFault;
  668.  
  669. import java.util.List;
  670.  
  671. public class SelectUserActivity extends Activity implements View.OnClickListener {
  672.     private Context context;
  673.     private ListView usersList;
  674.     private List<BackendlessUser> users;
  675.  
  676.     @Override
  677.     protected void onCreate(Bundle savedInstanceState) {
  678.         super.onCreate(savedInstanceState);
  679.         setContentView(R.layout.activity_select_user);
  680.  
  681.         setPointer();
  682.         loadUsersList();
  683.     }
  684.  
  685.     private void setPointer() {
  686.         this.context = this;
  687.         usersList = (ListView) findViewById(R.id.usersList);
  688.  
  689.         findViewById(R.id.btnCancelSelect).setOnClickListener(this);
  690.         findViewById(R.id.btnSelectUser).setOnClickListener(this);
  691.  
  692.         usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  693.             @Override
  694.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  695.                 usersList.setItemChecked(position, true);
  696.             }
  697.         });
  698.  
  699.         usersList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  700.             @Override
  701.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  702.                 usersList.setItemChecked(position, true);
  703.             }
  704.  
  705.             @Override
  706.             public void onNothingSelected(AdapterView<?> parent) {
  707.             }
  708.         });
  709.  
  710.  
  711.     }
  712.  
  713.     private void loadUsersList() {
  714.         final ProgressDialog pd = new ProgressDialog(context);
  715.         pd.setMessage("Loading users list ...");
  716.         pd.show();
  717.         Backendless.Data.of(BackendlessUser.class).find(new AsyncCallback<List<BackendlessUser>>() {
  718.             @Override
  719.             public void handleResponse(List<BackendlessUser> response) {
  720.                 users = response;
  721.                 usersList.setAdapter(new UsersAdapter(users, context));
  722.                 pd.dismiss();
  723.             }
  724.  
  725.             @Override
  726.             public void handleFault(BackendlessFault fault) {
  727.                 pd.dismiss();
  728.             }
  729.         });
  730.     }
  731.  
  732.  
  733.     @Override
  734.     public void onClick(View v) {
  735.         Intent returnIntent = new Intent();
  736.         int resultCode = Activity.RESULT_CANCELED;
  737.         switch (v.getId()) {
  738.             case R.id.btnCancelSelect:
  739.                 returnIntent.putExtra("result", "");
  740.                 break;
  741.             case R.id.btnSelectUser:
  742.                 returnIntent.putExtra("result", users.get(usersList.getCheckedItemPosition()).getObjectId());
  743.                 resultCode = Activity.RESULT_OK;
  744.  
  745.                 break;
  746.         }
  747.         setResult(resultCode, returnIntent);
  748.         finish();
  749.     }
  750. }
  751.  
  752. TasksAdapter.java
  753. ------------------
  754. package com.example.mohamadpc.backendless;
  755.  
  756. import android.app.ProgressDialog;
  757. import android.content.Context;
  758. import android.graphics.Color;
  759. import android.view.LayoutInflater;
  760. import android.view.View;
  761. import android.view.ViewGroup;
  762. import android.widget.BaseAdapter;
  763. import android.widget.CompoundButton;
  764. import android.widget.ListView;
  765. import android.widget.Switch;
  766. import android.widget.TextView;
  767.  
  768. import com.backendless.Backendless;
  769. import com.backendless.async.callback.AsyncCallback;
  770. import com.backendless.exceptions.BackendlessFault;
  771.  
  772. import java.util.List;
  773.  
  774. /**
  775.  * Created by teacher on 10/26/2017.
  776.  */
  777.  
  778. public class TasksAdapter extends BaseAdapter {
  779.     private List<Task> myTasks;
  780.     private Context context;
  781.  
  782.     public TasksAdapter(List<Task> myTasks, Context context) {
  783.         this.myTasks = myTasks;
  784.         this.context = context;
  785.     }
  786.  
  787.     @Override
  788.     public int getCount() {
  789.         if (myTasks == null) return 0;
  790.  
  791.         return myTasks.size();
  792.     }
  793.  
  794.     @Override
  795.     public Task getItem(int i) {
  796.         return myTasks.get(i);
  797.     }
  798.  
  799.     @Override
  800.     public long getItemId(int i) {
  801.         return i;
  802.     }
  803.  
  804.     @Override
  805.     public View getView(final int i, View view, final ViewGroup viewGroup) {
  806.         View myView = LayoutInflater.from(context).inflate(R.layout.task_item, null);
  807.         Task task = myTasks.get(i);
  808.  
  809.         final Switch taskDone = (Switch) myView.findViewById(R.id.taskDone);
  810.         final TextView tvTaskName = (TextView) myView.findViewById(R.id.taskName);
  811.  
  812.         tvTaskName.setText(myTasks.get(i).getName());
  813.         taskDone.setChecked(myTasks.get(i).getDone());
  814.         tvTaskName.setTextColor(getTaskColor(myTasks.get(i).getDone()));
  815.  
  816.         // mark selected row
  817.         if (i == ((ListView) viewGroup).getCheckedItemPosition()) {
  818.             myView.setBackgroundColor(Color.CYAN);
  819.         }
  820.  
  821.         taskDone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  822.             @Override
  823.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  824.                 myTasks.get(i).setDone(isChecked);
  825.                 final ProgressDialog pd = new ProgressDialog(context);
  826.                 pd.setMessage("Updating task status ...");
  827.                 pd.show();
  828.                 Backendless.Persistence.of(Task.class).save(myTasks.get(i), new AsyncCallback<Task>() {
  829.                     @Override
  830.                     public void handleResponse(Task response) {
  831.                         pd.dismiss();
  832.                         TasksAdapter.this.notifyDataSetChanged();
  833.                     }
  834.  
  835.                     @Override
  836.                     public void handleFault(BackendlessFault fault) {
  837.                         pd.dismiss();
  838.                         TasksAdapter.this.notifyDataSetChanged();
  839.                     }
  840.                 });
  841.             }
  842.         });
  843.  
  844.         return myView;
  845.     }
  846.  
  847.     private int getTaskColor(Boolean switchStatus) {
  848.         return switchStatus ? Color.GREEN : Color.RED;
  849.     }
  850. }
  851.  
  852. UsersAdapter.java
  853. ------------------
  854. package com.example.mohamadpc.backendless;
  855.  
  856. import android.content.Context;
  857. import android.graphics.Color;
  858. import android.view.View;
  859. import android.view.ViewGroup;
  860. import android.widget.BaseAdapter;
  861. import android.widget.ListView;
  862. import android.widget.TextView;
  863.  
  864. import com.backendless.BackendlessUser;
  865.  
  866. import java.util.List;
  867.  
  868. /**
  869.  * Created by teacher on 10/26/2017.
  870.  */
  871.  
  872. public class UsersAdapter extends BaseAdapter {
  873.     Context context;
  874.     List<BackendlessUser> myUsers;
  875.  
  876.  
  877.     public UsersAdapter(List<BackendlessUser> myUsers, Context context) {
  878.         this.context = context;
  879.         this.myUsers = myUsers;
  880.  
  881.     }
  882.  
  883.     @Override
  884.     public int getCount() {
  885.         if (myUsers == null) return 0;
  886.  
  887.         return myUsers.size();
  888.     }
  889.  
  890.     @Override
  891.     public BackendlessUser getItem(int i) {
  892.         return myUsers.get(i);
  893.     }
  894.  
  895.     @Override
  896.     public long getItemId(int i) {
  897.         return i;
  898.     }
  899.  
  900.     @Override
  901.     public View getView(final int i, View view, final ViewGroup viewGroup) {
  902.         TextView tv = new TextView(context);
  903.         tv.setTextSize(22);
  904.         tv.setText(myUsers.get(i).getProperty("name") + "");
  905.  
  906.         if (i == ((ListView) viewGroup).getCheckedItemPosition()) {
  907.             tv.setBackgroundColor(Color.CYAN);
  908.         }
  909.         return tv;
  910.     }
  911. }
  912.  
  913. activity_register.xml
  914. ---------------------
  915. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  916.     android:layout_width="match_parent"
  917.     android:layout_height="match_parent"
  918.     android:orientation="vertical">
  919.  
  920.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  921.         android:layout_width="match_parent"
  922.         android:layout_height="match_parent"
  923.         android:layout_weight="1"
  924.         android:orientation="vertical">
  925.  
  926.  
  927.         <ImageView
  928.             android:id="@+id/imageView"
  929.             android:layout_width="match_parent"
  930.             android:layout_height="match_parent"
  931.             android:layout_weight="1"
  932.             android:src="@drawable/register" />
  933.  
  934.  
  935.     </LinearLayout>
  936.  
  937.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  938.         android:layout_width="match_parent"
  939.         android:layout_height="match_parent"
  940.         android:layout_weight="0.80"
  941.         android:orientation="vertical">
  942.  
  943.         <EditText
  944.             android:id="@+id/etRegisterUserEmail"
  945.             android:layout_width="match_parent"
  946.             android:layout_height="wrap_content"
  947.             android:layout_weight="1"
  948.             android:hint="Enter user email"
  949.             android:inputType="textEmailAddress" />
  950.  
  951.         <EditText
  952.             android:id="@+id/etRegisterUserName"
  953.             android:layout_width="match_parent"
  954.             android:layout_height="wrap_content"
  955.             android:layout_weight="1"
  956.             android:hint="Enter user name" />
  957.  
  958.         <EditText
  959.             android:id="@+id/etRegisterPassword"
  960.             android:layout_width="match_parent"
  961.             android:layout_height="wrap_content"
  962.             android:layout_weight="1"
  963.             android:hint="Enter user password"
  964.             android:inputType="textPassword" />
  965.  
  966.         <EditText
  967.             android:id="@+id/etRegisterReTypePassword"
  968.             android:layout_width="match_parent"
  969.             android:layout_height="wrap_content"
  970.             android:layout_weight="1"
  971.             android:hint="ReType user password"
  972.             android:inputType="textPassword" />
  973.     </LinearLayout>
  974.  
  975.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  976.         android:layout_width="match_parent"
  977.         android:layout_height="match_parent"
  978.         android:layout_weight="1"
  979.         android:orientation="horizontal">
  980.  
  981.  
  982.         <Button
  983.             android:id="@+id/btnRegisterRgister"
  984.             android:layout_width="match_parent"
  985.             android:layout_height="wrap_content"
  986.             android:layout_margin="20dp"
  987.             android:layout_weight="1"
  988.             android:text="Register"
  989.             android:textSize="22sp" />
  990.  
  991.         <Button
  992.             android:id="@+id/btnRegisterCancel"
  993.             android:layout_width="match_parent"
  994.             android:layout_height="wrap_content"
  995.             android:layout_margin="20dp"
  996.             android:layout_weight="1"
  997.             android:text="Cancel"
  998.             android:textSize="22sp" />
  999.     </LinearLayout>
  1000.  
  1001.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1002.         android:layout_width="match_parent"
  1003.         android:layout_height="match_parent"
  1004.         android:layout_weight="1"
  1005.         android:orientation="vertical">
  1006.  
  1007.         <TextView
  1008.             android:layout_width="match_parent"
  1009.             android:layout_height="wrap_content"
  1010.             android:layout_margin="20dp"
  1011.             android:text="Bla bla bla"
  1012.             android:textSize="22sp" />
  1013.  
  1014.     </LinearLayout>
  1015. </LinearLayout>
  1016.  
  1017. activity_login.xml
  1018. ------------------
  1019. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1020.     xmlns:tools="http://schemas.android.com/tools"
  1021.     android:layout_width="match_parent"
  1022.     android:layout_height="match_parent"
  1023.     android:orientation="vertical">
  1024.  
  1025.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1026.         android:layout_width="match_parent"
  1027.         android:layout_height="match_parent"
  1028.         android:layout_weight="1"
  1029.         android:orientation="vertical">
  1030.  
  1031.         <ImageView
  1032.             android:layout_width="match_parent"
  1033.             android:layout_height="match_parent"
  1034.             android:layout_margin="16dp"
  1035.             android:src="@drawable/app_logo_1" />
  1036.     </LinearLayout>
  1037.  
  1038.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1039.         android:layout_width="match_parent"
  1040.         android:layout_height="match_parent"
  1041.         android:layout_weight="1"
  1042.         android:orientation="vertical">
  1043.  
  1044.         <EditText
  1045.             android:id="@+id/etLoginUserName"
  1046.             android:layout_width="match_parent"
  1047.             android:layout_height="wrap_content"
  1048.             android:layout_marginLeft="16dp"
  1049.             android:layout_marginRight="16dp"
  1050.             android:layout_marginTop="20dp"
  1051.             android:hint="@string/hint_user_name"
  1052.             android:inputType="textEmailAddress" />
  1053.  
  1054.  
  1055.         <EditText
  1056.             android:id="@+id/etLoginUserPassword"
  1057.             android:layout_width="match_parent"
  1058.             android:layout_height="wrap_content"
  1059.             android:layout_marginLeft="16dp"
  1060.             android:layout_marginRight="16dp"
  1061.             android:layout_marginTop="20dp"
  1062.             android:hint="@string/hint_user_password"
  1063.             android:inputType="textPassword" />
  1064.     </LinearLayout>
  1065.  
  1066.     <LinearLayout
  1067.         android:layout_width="match_parent"
  1068.         android:layout_height="match_parent"
  1069.         android:layout_weight="1"
  1070.         android:orientation="vertical">
  1071.  
  1072.         <Button
  1073.             android:id="@+id/btnLoginLogin"
  1074.             android:layout_width="match_parent"
  1075.             android:layout_height="wrap_content"
  1076.             android:layout_marginLeft="16dp"
  1077.             android:layout_marginRight="16dp"
  1078.             android:layout_weight="1"
  1079.             android:text="@string/cmd_login"
  1080.             android:textSize="22sp" />
  1081.  
  1082.         <TextView
  1083.             android:layout_width="match_parent"
  1084.             android:layout_height="wrap_content"
  1085.             android:layout_marginLeft="16dp"
  1086.             android:layout_marginRight="16dp"
  1087.             android:gravity="center"
  1088.             android:text="or"
  1089.             android:textSize="17sp" />
  1090.  
  1091.         <Button
  1092.             android:id="@+id/btnLoginRegister"
  1093.             android:layout_width="match_parent"
  1094.             android:layout_height="wrap_content"
  1095.             android:layout_marginLeft="16dp"
  1096.             android:layout_marginRight="16dp"
  1097.             android:text="@string/hint_register"
  1098.             android:textSize="12sp" />
  1099.  
  1100.     </LinearLayout>
  1101.  
  1102.     <LinearLayout
  1103.         android:layout_width="match_parent"
  1104.         android:layout_height="match_parent"
  1105.         android:layout_weight="1"
  1106.         android:orientation="vertical">
  1107.  
  1108.         <TextView
  1109.             android:layout_width="match_parent"
  1110.             android:layout_height="match_parent"
  1111.             android:layout_marginBottom="16dp"
  1112.             android:layout_marginLeft="16dp"
  1113.             android:layout_marginRight="16dp"
  1114.             android:gravity="center|bottom"
  1115.             android:text="@string/app_developer"
  1116.             android:textSize="22sp" />
  1117.     </LinearLayout>
  1118. </LinearLayout>
  1119.  
  1120. activity_tasks.xml
  1121. -------------------
  1122. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1123.     xmlns:app="http://schemas.android.com/apk/res-auto"
  1124.     android:layout_width="match_parent"
  1125.     android:layout_height="match_parent">
  1126.  
  1127.     <ListView
  1128.         android:id="@+id/lstTasks"
  1129.         android:layout_width="match_parent"
  1130.         android:layout_height="match_parent"
  1131.         android:choiceMode="singleChoice" />
  1132.  
  1133.     <com.nightonke.boommenu.BoomMenuButton
  1134.         android:id="@+id/btnboomMenu"
  1135.         android:layout_width="wrap_content"
  1136.         android:layout_height="wrap_content"
  1137.         android:layout_gravity="bottom|end"
  1138.         app:bmb_buttonEnum="textOutsideCircle"
  1139.         app:bmb_buttonPlaceEnum="buttonPlace_sc_4_1"
  1140.         app:bmb_piecePlaceEnum="piecePlace_dot_4_1"
  1141.         app:layout_anchor="@id/lstTasks"
  1142.         app:layout_anchorGravity="bottom|end" />
  1143.  
  1144. </android.support.design.widget.CoordinatorLayout>
  1145.  
  1146. activity_select_user.xml
  1147. ------------------------
  1148. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1149.     android:layout_width="match_parent"
  1150.     android:layout_height="match_parent"
  1151.     android:orientation="vertical">
  1152.     xmlns:android="http://schemas.android.com/apk/res/android" />
  1153.  
  1154.     <ListView
  1155.         android:id="@+id/usersList"
  1156.         android:layout_width="match_parent"
  1157.         android:layout_height="match_parent"
  1158.         android:layout_weight="0.1"
  1159.         android:choiceMode="singleChoice" />
  1160.  
  1161.     <LinearLayout
  1162.         android:layout_width="match_parent"
  1163.         android:layout_height="match_parent"
  1164.         android:layout_weight="1"
  1165.         android:orientation="horizontal">
  1166.  
  1167.         <Button
  1168.             android:id="@+id/btnSelectUser"
  1169.             android:layout_width="match_parent"
  1170.             android:layout_height="wrap_content"
  1171.             android:layout_weight="1"
  1172.             android:text="Select" />
  1173.  
  1174.         <Button
  1175.             android:id="@+id/btnCancelSelect"
  1176.             android:layout_width="match_parent"
  1177.             android:layout_height="wrap_content"
  1178.             android:layout_weight="1"
  1179.             android:text="Cancel" />
  1180.     </LinearLayout>
  1181. </LinearLayout>
  1182.  
  1183. task_item.xml
  1184. -------------
  1185. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1186.     xmlns:app="http://schemas.android.com/apk/res-auto"
  1187.     android:layout_width="match_parent"
  1188.     android:layout_height="wrap_content"
  1189.     android:orientation="horizontal">
  1190.  
  1191.     <Switch
  1192.         android:id="@+id/taskDone"
  1193.         android:layout_width="wrap_content"
  1194.         android:layout_height="wrap_content"
  1195.         android:layout_marginBottom="10dp"
  1196.         android:layout_marginLeft="16dp"
  1197.         android:layout_marginTop="10dp"
  1198.         android:focusable="false"
  1199.         android:focusableInTouchMode="false" />
  1200.  
  1201.     <TextView
  1202.         android:id="@+id/taskName"
  1203.         android:layout_width="match_parent"
  1204.         android:layout_height="wrap_content"
  1205.         android:layout_gravity="center_vertical"
  1206.         android:layout_marginRight="16dp"
  1207.         android:text="Sample task data.."
  1208.         android:textColor="#ff0000"
  1209.         android:textSize="18sp" />
  1210. </LinearLayout>
  1211.  
  1212. dialog_new_task_layout.xml
  1213. --------------------------
  1214. <?xml version="1.0" encoding="utf-8"?>
  1215. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  1216.     android:layout_width="match_parent"
  1217.     android:layout_height="wrap_content"
  1218.     android:background="#000000"
  1219.     android:orientation="vertical">
  1220.  
  1221.     <TextView
  1222.         android:layout_width="wrap_content"
  1223.         android:layout_height="wrap_content"
  1224.         android:layout_gravity="center"
  1225.         android:layout_marginBottom="20dp"
  1226.         android:text="Add new task !!!"
  1227.         android:textColor="#ff0000"
  1228.         android:textSize="22sp" />
  1229.  
  1230.     <EditText
  1231.         android:id="@+id/etNewTaskName"
  1232.         android:layout_width="match_parent"
  1233.         android:layout_height="wrap_content"
  1234.         android:layout_margin="16dp"
  1235.         android:background="#ffffff"
  1236.         android:hint="Enter task name here"
  1237.         android:text=""
  1238.         android:textColor="#000000"
  1239.         android:textSize="22sp" />
  1240. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement