Sharing Lists

Once a user is syncing with the cloud host, they sync a list of users profiles for people they can share a list with.

View Profiles


This list is used to power the Share UI, by binding a view query to the list of names. If someone signs up while you are in this view, you'll see their name pop up.

- (void)configureView
{
    // Do any additional setup after loading the view.
    _dataSource.query = [Profile queryProfilesInDatabase: database].asLiveQuery;
    _dataSource.labelProperty = @"name";    // Document property to display in the cell label}
}
// Customizes the appearance of table view cells.
- (void)couchTableSource:(CBLUITableSource*)source
             willUseCell:(UITableViewCell*)cell
                  forRow:(CBLQueryRow*)row
{
    
    // Configure the cell contents.
    // (cell.textLabel.text is already set, thanks to setting up labelProperty above.)
    NSString* personId = row.document.documentID;
//    if the person's id is in the list of members, or is the owner we are happy.
    bool member = NO;
    if ([myDocId isEqualToString:personId]) {
        member = YES;
    } else {
        NSMutableSet *intersection = [NSMutableSet setWithArray:_list.members];
        [intersection intersectSet:[NSSet setWithObject:personId]];
        
        if ([intersection count] > 0) {
            member = YES;
        }
    }
    if (member) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}
                              
No code example is currently available.
final Document user = (Document) getItem(position);
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checked);
boolean checked = isMemberOfTheCurrentList(user);
checkBox.setChecked(checked);
checkBox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        try {
            if (checkBox.isChecked()) {
                List.addMemberToList(mCurrentList, user);
            } else {
                List.removeMemberFromList(mCurrentList, user);
            }
        } catch (CouchbaseLiteException e) {
            Log.e(Application.TAG, "Cannot update a member to a list", e);
        }
    }
});
                  
final Document user = (Document) getItem(position);
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checked);
boolean checked = isMemberOfTheCurrentList(user);
checkBox.setChecked(checked);
checkBox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        try {
            if (checkBox.isChecked()) {
                List.addMemberToList(mCurrentList, user);
            } else {
                List.removeMemberFromList(mCurrentList, user);
            }
        } catch (CouchbaseLiteException e) {
            Log.e(Application.TAG, "Cannot update a member to a list", e);
        }
    }
});
                  
No code example is currently available.

Add Members to a List


When a user selects a person from the list, they are added as a member to the list (or removed).

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CBLQueryRow *row = [self.dataSource rowAtIndex:indexPath.row];
    NSString *toggleMemberId = row.document.documentID;
    NSArray *ms = _list.members;
    if (!ms) ms = @[];
    NSLog(@"toggle %@ members %@", toggleMemberId, [ms componentsJoinedByString:@" "]);
    NSUInteger x = [ms indexOfObject:toggleMemberId];
    NSLog(@"index member %d",x);
    
    if (x == NSNotFound) {
//        add to array
        _list.members = [ms arrayByAddingObject:toggleMemberId];
    } else {
//        remove from array
        _list.members = [ms filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF != %@" argumentArray:@[toggleMemberId]]];
    }
    NSLog(@"!!!!! %@", [_list.members componentsJoinedByString:@" "]);
    
    // Save changes:
    NSError* error;
    if (![_list save: &error]) {
    }
    [self configureView];
}
                              
No code example is currently available.
public static void addMemberToList(Document list, Document user)
        throws CouchbaseLiteException {
    Map<String, Object> newProperties = new HashMap<String, Object>();
    newProperties.putAll(list.getProperties());
    java.util.List<String> members = (java.util.List<String>) newProperties.get("members");
    if (members == null) members = new ArrayList<String>();
    members.add(user.getId());
    newProperties.put("members", members);
    try {
        list.putProperties(newProperties);
    } catch (CouchbaseLiteException e) {
        com.couchbase.lite.util.Log.e(Application.TAG, "Cannot add member to the list", e);
    }
}
public static void removeMemberFromList(Document list, Document user)
        throws CouchbaseLiteException {
    Map<String, Object> newProperties = new HashMap<String, Object>();
    newProperties.putAll(list.getProperties());
    java.util.List<String> members = (java.util.List<String>) newProperties.get("members");
    if (members != null) members.remove(user.getId());
    newProperties.put("members", members);
    list.putProperties(newProperties);
}
                          
public static void addMemberToList(Document list, Document user)
        throws CouchbaseLiteException {
    Map<String, Object> newProperties = new HashMap<String, Object>();
    newProperties.putAll(list.getProperties());
    java.util.List<String> members = (java.util.List<String>) newProperties.get("members");
    if (members == null) members = new ArrayList<String>();
    members.add(user.getId());
    newProperties.put("members", members);
    try {
        list.putProperties(newProperties);
    } catch (CouchbaseLiteException e) {
        com.couchbase.lite.util.Log.e(Application.TAG, "Cannot add member to the list", e);
    }
}
public static void removeMemberFromList(Document list, Document user)
        throws CouchbaseLiteException {
    Map<String, Object> newProperties = new HashMap<String, Object>();
    newProperties.putAll(list.getProperties());
    java.util.List<String> members = (java.util.List<String>) newProperties.get("members");
    if (members != null) members.remove(user.getId());
    newProperties.put("members", members);
    list.putProperties(newProperties);
}
                          
No code example is currently available.
function toggleShare(doc, user, cb) {
    doc.members = doc.members || [];
    var i = doc.members.indexOf(user)
    if (i === -1) {
        doc.members.push(user)
    } else {
        doc.members.splice(i,1)
    }
    log("members", doc.members)
    config.db.post(doc, cb)
}