Skip to content

Instantly share code, notes, and snippets.

@hideki
Created June 2, 2015 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hideki/a7fd004cbaa42ffe533d to your computer and use it in GitHub Desktop.
Save hideki/a7fd004cbaa42ffe533d to your computer and use it in GitHub Desktop.
CBL 101 Java
Page:
Link to the actual code of GrocerySync-Android:
Java File and Line:
Java Code snippet:
------------------------------
- Page 18 : Initialization
- https://github.com/couchbaselabs/GrocerySync-Android/blob/master/GrocerySync-Android/src/main/java/com/couchbase/grocerysync/MainActivity.java#L343
- MainActivity.java#L343
// Initialize Manager
try {
manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
} catch (IOException e) {
Log.e(TAG, "Couldn't create Manager instance", e);
return;
}
// Initialize Couchbase Lite and find/create my databaes
try {
database = manager.getDatabase(DATABASE_NAME);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Couldn't open database: " + DATABASE_NAME, e);
return;
}
--------------------------------------
- Page 23 : Adding New Items
- https://github.com/couchbaselabs/GrocerySync-Android/blob/master/GrocerySync-Android/src/main/java/com/couchbase/grocerysync/MainActivity.java#L343
- MainActivity.java#L343
// Create the new document's properties:
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("text", text);
properties.put("check", Boolean.FALSE);
properties.put("created_at", currentTimeString);
// Save the document:
Document doc = database.createDocument();
try {
doc.putProperties(properties);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Couldn't save new item", e);
}
-----------------------
- Page 25/28: Creating Database View
- https://github.com/couchbaselabs/GrocerySync-Android/blob/master/GrocerySync-Android/src/main/java/com/couchbase/grocerysync/MainActivity.java#L122
- MainActivity.java#L122
// Define a view with a map function that indexes to-­‐do items by creation date:
View viewItemsByDate = database.getView("byDate");
viewItemsByDate.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
Object createdAt = document.get("created_at");
if (createdAt != null) {
emitter.emit(createdAt.toString(), document);
}
}
}, "1.1");
-----------------------
- Page 29/33 : Driving the Table from a View Query
- https://github.com/couchbaselabs/GrocerySync-Android/blob/master/GrocerySync-Android/src/main/java/com/couchbase/grocerysync/MainActivity.java#L164
- MainActivity.java#L164
NOTE: This code snippet is hard to be identical with Objective-C.
// Create a query sorted by descending date, i.e. newest items first:
LiveQuery liveQuery = view.createQuery().toLiveQuery();
liveQuery.setDescending(true);
// Set ChangeListener for LiveQuey
liveQuery.addChangeListener(new LiveQuery.ChangeListener() {
public void changed(final LiveQuery.ChangeEvent event) {
// Update UI
runOnUiThread(new Runnable() {
public void run() {
grocerySyncArrayAdapter.clear();
for (Iterator<QueryRow> it = event.getRows(); it.hasNext();) {
grocerySyncArrayAdapter.add(it.next());
}
grocerySyncArrayAdapter.notifyDataSetChanged();
}
});
}
});
// Start LiveQuery
liveQuery.start();
-----------------------
- Page 34 : Displaying TableCells
- https://github.com/couchbaselabs/GrocerySync-Android/blob/a07edc012a9728e3a027982bb1b0c9795023c2cb/GrocerySync-Android/src/main/java/com/couchbase/grocerysync/GrocerySyncArrayAdapter.java#L33
- GrocerySyncArrayAdapter.java#L33
public View getView(int position, View itemView, ViewGroup parent) {
...
...
TextView label = ((ViewHolder)itemView.getTag()).label;
QueryRow row = getItem(position);
SavedRevision currentRevision = row.getDocument().getCurrentRevision();
// Check box
Object check = (Object) currentRevision.getProperty("check");
boolean isGroceryItemChecked = false;
if (check != null && check instanceof Boolean)
isGroceryItemChecked = ((Boolean)check).booleanValue();
// Text
String groceryItemText = (String) currentRevision.getProperty("text");
label.setText(groceryItemText);
// Image
ImageView icon = ((ViewHolder)itemView.getTag()).icon;
if(isGroceryItemChecked)
icon.setImageResource(R.drawable.list_area___checkbox___checked);
else
icon.setImageResource(R.drawable.list_area___checkbox___unchecked);
-----------------------
- Page 35 : Responding To Taps
- https://github.com/couchbaselabs/GrocerySync-Android/blob/a07edc012a9728e3a027982bb1b0c9795023c2cb/GrocerySync-Android/src/main/java/com/couchbase/grocerysync/MainActivity.java#L243
- MainActivity.java#L243
// CALLED BY THE ListView (setOnItemClickListener at line 193)
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Ask AdapterView for the corresponding query row, and get its document:
QueryRow row = (QueryRow) adapterView.getItemAtPosition(position);
Document document = row.getDocument();
// Toggle the document's 'checked' property:
Map<String, Object> newProperties = new HashMap<String, Object>(document.getProperties());
boolean checked = ((Boolean) newProperties.get("check")).booleanValue();
newProperties.put("check", !checked);
// Save changes:
try {
document.putProperties(newProperties);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Failed to update item", Toast.LENGTH_LONG).show();
}
}
-----------------------
- Page 40 : Creating Replications
- https://github.com/couchbaselabs/GrocerySync-Android/blob/a07edc012a9728e3a027982bb1b0c9795023c2cb/GrocerySync-Android/src/main/java/com/couchbase/grocerysync/MainActivity.java#L133
- MainActivity.java#L133
URL syncUrl = new URL(SYNC_URL);
Replication pullReplication = database.createPullReplication(syncUrl);
Replication pushReplication = database.createPushReplication(syncUrl);
pullReplication.setContinuous(true);
pushReplication.setContinuous(true);
// Observe replication progress changes, in both directions:
pullReplication.addChangeListener(this);
pushReplication.addChangeListener(this);
pullReplication.start();
pushReplication.start();
-----------------------
- Page 41 : Monitoring Replication
- https://github.com/couchbaselabs/GrocerySync-Android/blob/a07edc012a9728e3a027982bb1b0c9795023c2cb/GrocerySync-Android/src/main/java/com/couchbase/grocerysync/MainActivity.java#L371
- MainActivity.java#L371
public void changed(Replication.ChangeEvent event) {
Replication replication = event.getSource();
if (event.getSource().getStatus() != Replication.ReplicationStatus.REPLICATION_IDLE) {
// Sync is active
int completed = replication.getCompletedChangesCount();
int total = replication.getChangesCount();
String msg = String.format("Replicator completed %d / %d", completed, total);
Log.d(TAG, msg);
} else {
// Sync is idle
Log.d(TAG, "SYNC idle");
}
// Check for any change in error status
if (event.getError() != null) {
Log.e(TAG, "Error syncing", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment