Todo-android

A simple 'todo' app to get started with.

Overview

To Do is a simple task manager app that helps you keep a track of things to be done, and helps you get them done in an organized manner. Creating a simple to-do app will help you get started with Built.io Backend.

Let us have a look at the architecture of this app.

Architecture

The To-Do app has a single class called ‘Task’ on Built.io Backend application, and couple of fields within this class:

Field Type
task_name text
task_status boolean

The data type of text_name field is String. Here, the user enters the title of the task (e.g., Meeting at 4:00 pm).

The data type of task_status is Boolean. This determines whether the task is complete or pending.

Login

A user can login into the app using his/her Google Account. You can achieve this easily by extending the BuiltUILoginController class, which is found in the SDK provided by Built.io Backend. It contains a ready-made login screen for the app.

With the help of the following code, you can present a ‘Login with Google’ screen:

//For Login:

//'bltad1e7e5sampleabdd5a' is application api key
BuiltApplication builtApplication = Built.application(context, "bltad1e7e5sampleabdd5a");

// add this method to login to specific application
setApplication(builtApplication);


//Over ride methods which get add after BuiltUILoginController class extends. 
 @Override
 public void loginSuccess(BuiltUser user) {
     //After successful login, this method gets called.
 }

 @Override
 public void loginError(BuiltError error) {
     //If error occurs, this method gets called.
 }

Creating Tasks

To create a new task, we will create an object in the Tasks class, and set some value for task_name field.

//'bltad1e7e5sampleabdd5a' is application api key
BuiltApplication builtApplication = Built.application(context, "bltad1e7e5sampleabdd5a");

BuiltObject taskObject = builtApplication.classWithUid("todo_task").object();
taskObject.set("task_name", "Buy Milk");

//user can add this but default boolean value will be false.
taskObject.set("task_status", false);

When a user creates a task, no one else can view, edit, or delete this task. Only the creator of the task has the privilege to modify it. For this reason, we need to set ACL on the task we are creating. Let us create an object of BuiltACL class.

To do this, we first provide all the permissions to the user creating a task. The currentUser method on BuiltUser returns the currently logged in user. We set read, write and delete access for the logged in user.

BuiltACL aclObject = builtApplication.acl();
aclObject.setUserDeleteAccess(current_BuiltUser_Uid, true);
aclObject.setUserReadAccess(current_BuiltUser_Uid, true);
aclObject.setUserWriteAccess(current_BuiltUser_Uid, true);

Since we do not want any other user to view, edit, or delete it, we set public access to false.

aclObject.setPublicDeleteAccess(false);
aclObject.setPublicReadAccess(false);
aclObject.setPublicWriteAccess(false);

We then set this ACL on the new task.

taskObject.setACL(aclObject);

And finally, we save this task to our Built.io Backend application.

taskObject.saveInBackground(new BuiltResultCallBack() {
     @Override
       public void onCompletion(BuiltConstant.ResponseType responseType, BuiltError builtError){
      if(builtError == null){
              //Success block.
      }else{
              //Error block.
      }       
       }
});

The success block is called when the task is saved to Built.io Backend application.

Listing the tasks

With the help of BuiltUIListViewController (present in SDK) you can easily list tasks in your app. You just need to pass the context, class uid and BuiltApplication’s instance to the constructor during instance creation.

The loadData() method in the BuiltUIListViewController class loads the list of builtObject of the class whose UID is passed while installation.

//Intialize BuiltUIListViewController instance.
//'bltad1e7e5sampleabdd5a' is application api key
BuiltApplication builtApplication = Built.application(context, "bltad1e7e5sampleabdd5a");

// add this method to fetch objects specific application and class.
BuiltUIListViewController listView = new BuiltUIListViewController(context, builtApplication, "todo_task");

//Make a call for load project list.
listView.loadData(new BuiltListViewResultCallBack() {

  @Override
  public View getView(int position, View convertView,ViewGroup parent, BuiltObject object){
  //After successful call this block gets called.
  //return view which is set for row in list view.
if(convertView == null){
       LayoutInflater inflater = LayoutInflater.from(Context);
       convertView = inflater.inflate(R.layout.todo_task, parent, false);
       taskNameTextView = (CheckedTextView)convertView.findViewById(R.id.taskTextView);
       deleteTaskImageButton = (ImageButton)convertView.findViewById(R.id.deleteTaskImageButton);
   }else{
       viewholder.taskNameTextView.setText("");
   }
   taskNameTextView.setText(builtObject.getString("task_name"));

  return convertView;
  }

  
  @Override
  public void onError(BuiltError error) {
  //Error block
  }

  @Override
  public void onAlways() {
  //Always called after onSuccess and onError.
  }

  @Override
  public int getViewTypeCount() {
    return 1;
  }

  @Override
  public int getItemViewType(int position) {
  return position;
  }

});

//Set login layout from Built.io Backend
//For using BuiltUIListViewController user need to set layout programmatically.
setContentView(listView.getLayout());

And we have the list populated with tasks.

Updating and Deleting Tasks

The BuiltUIListViewController has a getView() callback method which contains the builtObject and its row position in the 'listview'. Once we have this task object, we can update its task_status field.

// 'bltad1e7e5sampleabdd5a' is application api key
BuiltApplication builtApplication = Built.application(context, "bltad1e7e5sampleabdd5a");
BuiltObject taskObject = builtApplication.classWithUid("todo_task").object("selected_task_builtObject_Uid");
//update a task with task_status as true or false.
taskObject.set("task_status", task_builtObject_current_status);
taskObject.saveInBackground(new BuiltResultCallBack() {
     @Override
       public void onCompletion(BuiltConstant.ResponseType responseType, BuiltError builtError){
      
if(builtError == null){
          //Success block.
      }else{
          //Error block.
      }       
       }

});

Deleting a task

// 'bltad1e7e5sampleabdd5a' is application api key
BuiltApplication builtApplication = Built.application(context, "bltad1e7e5sampleabdd5a");

BuiltObject taskObject = builtApplication.classWithUid("todo_task").object(taskModel.getTaskUid());

taskObject.destroyInBackground(new BuiltResultCallBack() {
@Override
       public void onCompletion(BuiltConstant.ResponseType responseType, BuiltError builtError){
      
if(builtError == null){
          //Success block.
      }else{
          //Error block.
      }       
       }

});