Todo-ios

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, and couple of fields within this class:

Field Types
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.

BuiltUILoginController *loginController = [[BuiltUILoginController alloc] initWithNibName:nil bundle:nil];
//set builtApplication object to logincontroller
loginController.builtApplication = builtApplication;
//select the login fields that will be displayed to the user
loginController.fields = BuiltLoginFieldGoogle | BuiltLoginFieldUsernameAndPassword | BuiltLoginFieldLogin;
//set google app setting delegate to set the app client id and secret of your google app
loginController.googleAppSettingDelegate = self;
//set the login delegate to be notified when user logs in
loginController.delegate = self;

Implement the delegate methods for Google login.

- (NSString*)googleAppClientID {
    //create a project on Google APIs console https://code.google.com/apis/console/ and then enter client id
    return @"google_client_id";
}
- (NSString*)googleAppClientSecret {
    //create a project on Google APIs console https://code.google.com/apis/console/ and then enter client secret
    return @"google_client_secret";
}

After a user is successfully logged in the following delegate method is called.

-(void)loginSuccessWithUser:(BuiltUser *)user{
  //login done
}

Creating Tasks

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

BuiltClass *taskClass = [builtApplication classWithUID:@"todo_task"];
BuiltObject *newTask = [taskClass object];
newTask[@"task_name"] = @"Buy Milk";

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 BuiltApplication returns the currently logged in user. We set read, write and delete access for the logged in user.

BuiltACL *acl = [builtApplication acl];
[acl setReadAccess:YES forUserId:builtApplication.currentUser.uid];
[acl setWriteAccess:YES forUserId:builtApplication.currentUser.uid];
[acl setDeleteAccess:YES forUserId:builtApplication.currentUser.uid];

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

[acl setPublicReadAccess:NO];
[acl setPublicWriteAccess:NO];
[acl setPublicDeleteAccess:NO];

We then set this ACL on the new task.

[newTask setACL:acl];

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

[newTask saveInBackgroundWithCompletion:^(ResponseType responseType, NSError *error) {
    if (error == nil) {
        //object saved successfully
    }else {
        //there was some error in saving the object
    }
}];

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

Listing the tasks

With the help of BuiltUITableViewController (present in SDK), you can easily list tasks in your app. You just need to pass the class object during instance creation. The BuiltUITableViewController already has ‘pull to refresh’ and ‘load more’ functionality integrated into it.

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

In the BuiltUITableViewDataSource delegate method, we can assign the task_name to the cell's textLabel property:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath builtObject:(BuiltObject *)builtObject{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel = builtObject[@"task_name"];
    return cell;
}

Updating and Deleting Tasks

The BuiltUITableViewController has a builtObjectAtIndexPath: method that returns the object in a particular row of the table. Once we have this task object, we can update its task_status field.

BuiltObject *taskObject = [self builtObjectAtIndexPath:indexPath];
//update a task with task_status as YES to mark as done
taskObject[@"task_status"] = @(YES)
[taskObject saveInBackgroundWithCompletion:^(ResponseType responseType, NSError *error) {
    if (error == nil) {
        //object saved successfully
    }else {
        //there was some error in saving the object
    }
}];

Deleting a task

BuiltObject *taskObject = [self builtObjectAtIndexPath:indexPath];
[taskObject destroyInBackgroundWithCompletion:^(ResponseType responseType, NSError *error) {
   if (error == nil) {
        //object deleted successfully
    }else {
        //there was some error while deleting object
    }
}];