Permalink
Browse files

Merge branch 'develop'

  • Loading branch information...
2 parents 4af0568 + 127d3a6 commit c9acce11578d6f511476269f586e2b1c785a89fc Harry Brundage committed Nov 30, 2010
@@ -38,7 +38,7 @@ class AccountsController extends Citrus.Controller
# Calledback from the NewAccountSelectWindow
# Returns true or false based on the instantiation of the account,
# not the authorization.
- addNewAccountOfType: (type) ->
+ addNewAccountOfType: (type, callback) ->
if _.isFunction(type)
account = new type()
else
@@ -48,9 +48,11 @@ class AccountsController extends Citrus.Controller
this.watchAccount(account)
# Callback to trigger initial sync
success = () =>
- @selectWindow.win.close({animated: false})
+ @selectWindow.win.close({animated: false}) if @selectWindow?
account.synch()
account.removeEventListener "authorization:success", success
+ if callback?
+ callback(account)
account.addEventListener "authorization:success", success
account.authorize() # This adds a new window to the heirarchy
@@ -46,7 +46,7 @@
animated: true
});
};
- AccountsController.prototype.addNewAccountOfType = function(type) {
+ AccountsController.prototype.addNewAccountOfType = function(type, callback) {
var account, success;
if (_.isFunction(type)) {
account = new type();
@@ -56,11 +56,16 @@
if ((account != null) && account.valid) {
this.watchAccount(account);
success = __bind(function() {
- this.selectWindow.win.close({
- animated: false
- });
+ if (this.selectWindow != null) {
+ this.selectWindow.win.close({
+ animated: false
+ });
+ }
account.synch();
- return account.removeEventListener("authorization:success", success);
+ account.removeEventListener("authorization:success", success);
+ if (callback != null) {
+ return callback(account);
+ }
}, this);
account.addEventListener("authorization:success", success);
account.authorize();
@@ -44,10 +44,32 @@ class SplashController extends Citrus.Controller
takeActionFromRow: (row, e) =>
action = row.action
- if action.requiresAccount()
- this.takeAccountBasedActionFromRow(row, e)
+ # Run action if possible, if not offer to make it possible
+ if this.isActionTakeable(action)
+ if action.requiresAccount()
+ this.takeAccountBasedActionFromRow(row, e)
+ else
+ this.takeAccountlessActionFromRow(row, e)
else
- this.takeAccountlessActionFromRow(row, e)
+ d("Cant run action!")
+ if action.requiresAccount() && this.possibleAccountsForAction(action).length == 0
+ # Can't run action because no account has been added.
+ type = action.accountType.replace('Account', '')
+ alertDialog = Titanium.UI.createAlertDialog
+ title: 'Whoops!'
+ message: "You can't run this yet because you haven't added a #{type} account yet. Would you like to add one now?"
+ buttonNames: ["Add #{type}", 'Cancel']
+
+ alertDialog.addEventListener 'click', (e) =>
+ d e
+ if e.index? && e.index == 0
+ d "trying to add new account #{action.accountType}"
+ root.AccountsController.addNewAccountOfType(action.accountType, (account) =>
+ account.addEventListener "state:ready", () =>
+ this.takeAccountBasedActionFromRow(row, e)
+ )
+ alertDialog.show()
+
takeAccountlessActionFromRow: (row, e) ->
action = row.action
@@ -122,10 +144,9 @@ class SplashController extends Citrus.Controller
row.displaySuccess()
_actionFailure: (row, xhr, status, error) ->
- Titanium.API.error("Action failed!")
- Titanium.API.error(status)
- d(error)
- d(xhr.responseText)
+ er "Action Failed! xhr:", xhr, "status:", status, "error", error
+ if error? && error.alertText? && _.isString(error.alertText)
+ alert error.alertText
row.displayError()
_prepareActions: (actions) ->
@@ -58,12 +58,36 @@
}, this));
};
SplashController.prototype.takeActionFromRow = function(row, e) {
- var action;
+ var action, alertDialog, type;
action = row.action;
- if (action.requiresAccount()) {
- return this.takeAccountBasedActionFromRow(row, e);
+ if (this.isActionTakeable(action)) {
+ if (action.requiresAccount()) {
+ return this.takeAccountBasedActionFromRow(row, e);
+ } else {
+ return this.takeAccountlessActionFromRow(row, e);
+ }
} else {
- return this.takeAccountlessActionFromRow(row, e);
+ d("Cant run action!");
+ if (action.requiresAccount() && this.possibleAccountsForAction(action).length === 0) {
+ type = action.accountType.replace('Account', '');
+ alertDialog = Titanium.UI.createAlertDialog({
+ title: 'Whoops!',
+ message: "You can't run this yet because you haven't added a " + type + " account yet. Would you like to add one now?",
+ buttonNames: ["Add " + type, 'Cancel']
+ });
+ alertDialog.addEventListener('click', __bind(function(e) {
+ d(e);
+ if ((e.index != null) && e.index === 0) {
+ d("trying to add new account " + action.accountType);
+ return root.AccountsController.addNewAccountOfType(action.accountType, __bind(function(account) {
+ return account.addEventListener("state:ready", __bind(function() {
+ return this.takeAccountBasedActionFromRow(row, e);
+ }, this));
+ }, this));
+ }
+ }, this));
+ return alertDialog.show();
+ }
}
};
SplashController.prototype.takeAccountlessActionFromRow = function(row, e) {
@@ -141,10 +165,10 @@
return row.displaySuccess();
};
SplashController.prototype._actionFailure = function(row, xhr, status, error) {
- Titanium.API.error("Action failed!");
- Titanium.API.error(status);
- d(error);
- d(xhr.responseText);
+ er("Action Failed! xhr:", xhr, "status:", status, "error", error);
+ if ((error != null) && (error.alertText != null) && _.isString(error.alertText)) {
+ alert(error.alertText);
+ }
return row.displayError();
};
SplashController.prototype._prepareActions = function(actions) {
@@ -5,6 +5,21 @@ class FollowAction extends Citrus.TwitterAction
buttonText: "Follow"
action: (account, success, failure) ->
- account.api.addFriend(@followeeId, success, failure)
+ account.api.addFriend(@followeeId, success, (xhr, status, error) ->
+ if xhr?
+ d xhr.responseText
+ if xhr.responseText.match(/already on your list/)
+ alert(account.screenName + " is already following this user!")
+ return success({})
+ try
+ msg = JSON.parse xhr.responseText
+ catch e
+ return failure(error)
+
+ if msg? && msg.error?
+ failure(null, null, {alertText: msg.error})
+ else
+ failure(error)
+ )
Citrus.Actions.Twitter.FollowAction = FollowAction
@@ -17,7 +17,28 @@
FollowAction.prototype.type = "TwitterFollowAction";
FollowAction.prototype.buttonText = "Follow";
FollowAction.prototype.action = function(account, success, failure) {
- return account.api.addFriend(this.followeeId, success, failure);
+ return account.api.addFriend(this.followeeId, success, function(xhr, status, error) {
+ var msg;
+ if (xhr != null) {
+ d(xhr.responseText);
+ if (xhr.responseText.match(/already on your list/)) {
+ alert(account.screenName + " is already following this user!");
+ return success({});
+ }
+ }
+ try {
+ msg = JSON.parse(xhr.responseText);
+ } catch (e) {
+ return failure(error);
+ }
+ if ((msg != null) && (msg.error != null)) {
+ return failure(null, null, {
+ alertText: msg.error
+ });
+ } else {
+ return failure(error);
+ }
+ });
};
return FollowAction;
}();
@@ -26,31 +26,30 @@ class ActionTableViewRow extends Citrus.Object
row.object = this
return row
- displayButton: (style, title) ->
+ displayButton: (style, title, opts) ->
style ?= Titanium.UI.iPhone.SystemButton.BORDERED
title ?= if _.isFunction(@action.buttonText) then @action.buttonText() else @action.buttonText
key = (String(style)+"style" || "nostyle")
@buttons ?= {} # Keep an array to hold the different styled buttons since changing styles is apparently devastating to the Ti runtime
shittyTI = style == Titanium.UI.iPhone.SystemButton.SPINNER
- unless @buttons[key]?
- # Set up opts. Get around Titanium bugs by not setting the enabled or title attributes if
- # the style is the spinner type (which is proxied by a different object in titanium which
- # barfs when it gets these option)
- opts = {
- right:5
- color: "#000"
- width: this.buttonWidth()
- height: 25
- }
- unless shittyTI
- opts.style = style if style?
- else
- opts.style = style
- opts.enabled = true
- opts.height = "auto"
- opts.width = "auto"
+
+ # Set up opts. Get around Titanium bugs by not setting the enabled or title attributes if
+ # the style is the spinner type (which is proxied by a different object in titanium which
+ # barfs when it gets these option)
+ opts = _.extend({
+ right: 5
+ color: "#000"
+ width: this.buttonWidth()
+ height: 25
+ }, (opts || {}))
+
+ unless shittyTI
+ opts.style = style if style?
+ else
+ opts.systemButton = style
+ unless @buttons[key]?
button = Ti.UI.createButton opts
unless shittyTI
@@ -63,10 +62,12 @@ class ActionTableViewRow extends Citrus.Object
@buttons[key] = button
@row.add(button)
+ for k, v of opts
+ unless _.include(["style", "systemButton"], k)
+ @buttons[key][k] = v
for k, b of @buttons
b.hide()
@buttons[key].title = title
- @buttons[key].enabled = @takeable
@buttons[key].show()
true
@@ -97,14 +98,14 @@ class ActionTableViewRow extends Citrus.Object
d("Trying to display progress")
@state = Citrus.ActionTableViewRow.InProgress
@takeable = false
- this.displayButton(Titanium.UI.iPhone.SystemButton.ACTION, " ")
+ this.displayButton(Titanium.UI.iPhone.SystemButton.SPINNER, "Running ...")
# Updates the row to show the user it's done running its action and it worked!
displaySuccess: ->
d("Trying to display success")
@state = Citrus.ActionTableViewRow.Success
@takeable = false
- this.displayButton(Titanium.UI.iPhone.SystemButton.PLAIN, "Done!")
+ this.displayButton(Titanium.UI.iPhone.SystemButton.PLAIN, "Done!", {enabled: false, color: "#ccc"})
d("Success displayed")
# Updates the row to show that the action failed.
@@ -113,7 +114,7 @@ class ActionTableViewRow extends Citrus.Object
retry ?= true
@state = Citrus.ActionTableViewRow.Error
@takeable = retry
- this.displayButton(null, if retry then "Retry?" else "Error!")
+ this.displayButton(null,(if retry then "Retry?" else "Error!"), {color: 'red'})
d("Error displayed")
icon: ->
@@ -38,30 +38,27 @@
row.object = this;
return row;
};
- ActionTableViewRow.prototype.displayButton = function(style, title) {
- var b, button, k, key, opts, shittyTI, _ref, _ref2;
+ ActionTableViewRow.prototype.displayButton = function(style, title, opts) {
+ var b, button, k, key, shittyTI, v, _ref, _ref2;
style != null ? style : style = Titanium.UI.iPhone.SystemButton.BORDERED;
title != null ? title : title = _.isFunction(this.action.buttonText) ? this.action.buttonText() : this.action.buttonText;
key = String(style) + "style" || "nostyle";
(_ref = this.buttons) != null ? _ref : this.buttons = {};
shittyTI = style === Titanium.UI.iPhone.SystemButton.SPINNER;
- if (this.buttons[key] == null) {
- opts = {
- right: 5,
- color: "#000",
- width: this.buttonWidth(),
- height: 25
- };
- if (!shittyTI) {
- if (style != null) {
- opts.style = style;
- }
- } else {
+ opts = _.extend({
+ right: 5,
+ color: "#000",
+ width: this.buttonWidth(),
+ height: 25
+ }, opts || {});
+ if (!shittyTI) {
+ if (style != null) {
opts.style = style;
- opts.enabled = true;
- opts.height = "auto";
- opts.width = "auto";
}
+ } else {
+ opts.systemButton = style;
+ }
+ if (this.buttons[key] == null) {
button = Ti.UI.createButton(opts);
if (!shittyTI) {
button.addEventListener("click", __bind(function(e) {
@@ -75,14 +72,20 @@
this.buttons[key] = button;
this.row.add(button);
}
+ for (k in opts) {
+ if (!__hasProp.call(opts, k)) continue;
+ v = opts[k];
+ if (!_.include(["style", "systemButton"], k)) {
+ this.buttons[key][k] = v;
+ }
+ }
_ref2 = this.buttons;
for (k in _ref2) {
if (!__hasProp.call(_ref2, k)) continue;
b = _ref2[k];
b.hide();
}
this.buttons[key].title = title;
- this.buttons[key].enabled = this.takeable;
this.buttons[key].show();
return true;
};
@@ -117,21 +120,26 @@
d("Trying to display progress");
this.state = Citrus.ActionTableViewRow.InProgress;
this.takeable = false;
- return this.displayButton(Titanium.UI.iPhone.SystemButton.ACTION, " ");
+ return this.displayButton(Titanium.UI.iPhone.SystemButton.SPINNER, "Running ...");
};
ActionTableViewRow.prototype.displaySuccess = function() {
d("Trying to display success");
this.state = Citrus.ActionTableViewRow.Success;
this.takeable = false;
- this.displayButton(Titanium.UI.iPhone.SystemButton.PLAIN, "Done!");
+ this.displayButton(Titanium.UI.iPhone.SystemButton.PLAIN, "Done!", {
+ enabled: false,
+ color: "#ccc"
+ });
return d("Success displayed");
};
ActionTableViewRow.prototype.displayError = function(retry) {
d("Trying to display error");
retry != null ? retry : retry = true;
this.state = Citrus.ActionTableViewRow.Error;
this.takeable = retry;
- this.displayButton(null, retry ? "Retry?" : "Error!");
+ this.displayButton(null, (retry ? "Retry?" : "Error!"), {
+ color: 'red'
+ });
return d("Error displayed");
};
ActionTableViewRow.prototype.icon = function() {
Oops, something went wrong. Retry.

0 comments on commit c9acce1

Please sign in to comment.