Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active December 12, 2015 06:08
Show Gist options
  • Save ChrisRisner/4726642 to your computer and use it in GitHub Desktop.
Save ChrisRisner/4726642 to your computer and use it in GitHub Desktop.
Unofficial Android SDK Todo list
@Override
public void onClick(View v) {
if (txtItem.getText().length() > 0) {
TodoItem newItem = new TodoItem();
newItem.SetComplete(false);
newItem.SetText(txtItem.getText().toString());
todoTable.insertAsync(newItem, new MobileServiceCallback<TodoItem>() {
@Override
public void completedSuccessfully(TodoItem item) {
todoItems.add(item);
listAdapter.notifyDataSetChanged();
txtItem.setText("");
}
@Override
public void errorOccurred(MobileException exception) {
Toast.makeText(activity, "An error occurred inserting", Toast.LENGTH_SHORT).show();
}
});
}
}
msClient = new MobileService(this);
todoTable = msClient.getTable(TodoItem.class);
function insert(item, user, request) {
if (item.text.length <5) {
request.respond(statusCodes.BAD_REQUEST, "Your todo must be at least 5 characters");
} else {
request.execute();
sendGcmPush({ __builtInType: "toast", text: item.text + ' from push' });
}
}
//Sends a GCM push to all registered subscribers from the pushChannel table,
//with the specified payload body. Note that the payload is limited to about
//4000 bytes after it's serialized to JSON.
function sendGcmPush(payload) {
var reqModule = require('request');
var channelTable = tables.getTable('pushChannel');
channelTable.read({
success: function(channels) {
channels.forEach(function(channel) {
reqModule({
url: 'https://android.googleapis.com/gcm/send',
method: 'POST',
headers: {
//TODO: this should be your push API key
'Authorization': 'key=AIzaSyDYvmWfB1IjOkTK1Hq9M_Ezba_lj1ue-B4'
},
json: {
//You could pipe up to 1,000 registration ids here
registration_ids: [channel.regId],
data: payload
}
}, function (err, resp, body) {
if (err || resp.statusCode !== 200) {
console.error('Error sending GCM push: ', err);
console.error('Error sending GCM status: ', resp.statusCode);
} else {
console.log('Sent GCM push notification successfully to ' + channel.regId);
//TODO: handle dead channels
}
});
});
}
});
}
private void LoadTodoItems() {
todoTable.where().equal("complete", false).selectAsync(new MobileServiceCallbackWithResults<TodoItem>() {
@Override
public void errorOccurred(MobileException exception) {
Toast.makeText(activity, "An error occurred loading: " + exception.getMessage(), Toast.LENGTH_SHORT).show();
exception.printStackTrace();
}
@Override
public void completedSuccessfully(List<TodoItem> results) {
todoItems = (ArrayList<TodoItem>)results;
listAdapter = new TodoArrayAdapter(activity, todoItems);
lvItems.setAdapter(listAdapter);
}
});
}
if (msClient.isLoggedIn())
LoadTodoItems();
else {
msClient.login(MobileServiceAuthenticationProvider.TWITTER, new MobileServiceLoginCallback() {
public void errorOccurred(MobileException exception) {
//Invoked if an error occurred in the authentication process
Toast.makeText(activity, "An error occurred logging in", Toast.LENGTH_SHORT).show();
}
public void completedSuccessfully(MobileUser user) {
//Invoked if the login completed successfully -- user.getUserId() provides the user id
//which you can also access in server-side scripts
LoadTodoItems();
}
public void cancelled() {
//Invoked if the user cancelled the login process
Toast.makeText(activity, "User cancelled login", Toast.LENGTH_SHORT).show();
}
});
}
<permission
android:name="com.cmr.wams_test.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.cmr.wams_test.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver
android:name="net.sashag.wams.android.WAMSGCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.cmr.wams_test" />
</intent-filter>
</receiver>
<service android:name="net.sashag.wams.android.WAMSGCMIntentService" />
public void markItemComplete(CharSequence text) {
for (TodoItem item : todoItems) {
if (item.GetText().equals(text)) {
item.SetComplete(true);
todoTable.updateAsync(item, new MobileServiceCallback<TodoItem>() {
@Override
public void errorOccurred(MobileException exception) {
Toast.makeText(activity, "An error occurred updating", Toast.LENGTH_SHORT).show();
}
@Override
public void completedSuccessfully(TodoItem item) {
todoItems.remove(item);
listAdapter.notifyDataSetChanged();
}
});
break;
}
}
}
private MobileService msClient;
private MobileTable<TodoItem> todoTable;
function insert(item, user, request) {
var channelTable = tables.getTable('pushChannel');
channelTable
.where({ regId: item.regId })
.read({ success: insertChannelIfNotFound });
function insertChannelIfNotFound(existingChannels) {
if (existingChannels.length > 0) {
request.respond(200, existingChannels[0]);
} else {
request.execute();
}
}
}
<string name="mobileServiceUrl">https://unofficial-android.azure-mobile.net</string>
<string name="mobileServiceApiKey">KCWTLxNZroDacGTAqbnuHyaaKbLusQ33</string>
import net.sashag.wams.android.DataMember;
import net.sashag.wams.android.DataTable;
import net.sashag.wams.android.Key;
@DataTable("TodoItem")
public class TodoItem {
@DataMember("text") private String Text;
@DataMember("complete") private boolean Complete;
@Key public int id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment