Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Remove runOnUiThread wrappers - now not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkus Karchebnyy committed Nov 21, 2016
1 parent 410f7e7 commit 1f0ba93
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public class ChannelActivity extends Activity implements ChatClientListener

private static final Handler handler = new Handler();
private AlertDialog incomingChannelInvite;
private StatusListener joinListener;
private StatusListener declineInvitationListener;

@Override
protected void onCreate(Bundle savedInstanceState)
Expand Down Expand Up @@ -116,43 +114,10 @@ public boolean onOptionsItemSelected(MenuItem item)
basicClient.shutdown();
finish();
break;
case R.id.action_unregistercm: {
String gcmToken = basicClient.getGCMToken();
basicClient.getChatClient().unregisterGCMToken(
gcmToken, new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
logger.w("GCM unregistration not successful");
runOnUiThread(new Runnable() {
public void run()
{
Toast
.makeText(ChannelActivity.this,
"GCM unregistration not successful",
Toast.LENGTH_SHORT)
.show();
}
});
}

@Override
public void onSuccess()
{
logger.d("GCM unregistration successful");
runOnUiThread(new Runnable() {
public void run()
{
Toast
.makeText(ChannelActivity.this,
"GCM unregistration successful",
Toast.LENGTH_SHORT)
.show();
}
});
}
});
}
case R.id.action_unregistercm:
basicClient.getChatClient().unregisterGCMToken(basicClient.getGCMToken(),
new ToastStatusListener("GCM unregistration successful",
"GCM unregistration not successful"));
break;
}
return super.onOptionsItemSelected(item);
Expand Down Expand Up @@ -265,17 +230,11 @@ public void onClick(DialogInterface dialog, int id)
logger.d("Searching for " + channelName);
final Channel channel = channelsObject.getChannelByUniqueName(channelName);

runOnUiThread(new Runnable() {
@Override
public void run()
{
if (channel != null) {
TwilioApplication.get().showToast(channel.getSid() + ":" + channel.getFriendlyName());
} else {
TwilioApplication.get().showToast("Channel not found.");
}
}
});
if (channel != null) {
TwilioApplication.get().showToast(channel.getSid() + ":" + channel.getFriendlyName());
} else {
TwilioApplication.get().showToast("Channel not found.");
}
}
});
createChannelDialog = builder.create();
Expand Down Expand Up @@ -315,29 +274,16 @@ public void onClick(DialogInterface dialog, int which)
{
if (which == JOIN) {
dialog.cancel();
joinListener = new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().showError(
"failed to join channel", errorInfo);
}

channel.join(
new ToastStatusListener("Successfully joined channel",
"Failed to join channel") {
@Override
public void onSuccess()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.notifyDataSetChanged();
}
});
logger.d("Successfully joined channel");
super.onSuccess();
adapter.notifyDataSetChanged();
}

};
channel.join(joinListener);
});
}
}
});
Expand Down Expand Up @@ -381,25 +327,14 @@ public void run()
@Override
public void onClick(DialogInterface dialog, int which)
{
channel.join(new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().showError(
"Failed to join channel", errorInfo);
}

channel.join(new ToastStatusListener(
"Successfully joined channel",
"Failed to join channel") {
@Override
public void onSuccess()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.notifyDataSetChanged();
}
});
logger.d("Successfully joined channel");
super.onSuccess();
adapter.notifyDataSetChanged();
}
});
incomingChannelInvite = null;
Expand All @@ -408,34 +343,19 @@ public void run()
.setNegativeButton(
R.string.decline,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which)
{
declineInvitationListener = new StatusListener() {

@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().showError(
"Failed to decline channel invite", errorInfo);
}

channel.declineInvitation(new ToastStatusListener(
"Successfully declined channel invite",
"Failed to decline channel invite") {
@Override
public void onSuccess()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.notifyDataSetChanged();
}
});
logger.d("Successfully declined channel invite");
super.onSuccess();
adapter.notifyDataSetChanged();
}

};
channel.declineInvitation(declineInvitationListener);
});
incomingChannelInvite = null;
}
})
Expand Down Expand Up @@ -463,43 +383,25 @@ public int compare(Channel lhs, Channel rhs)
public void onChannelAdd(final Channel channel)
{
logger.d("Received onChannelAdd callback for channel |" + channel.getFriendlyName() + "|");
runOnUiThread(new Runnable() {
@Override
public void run()
{
channels.add(channel);
adapter.notifyDataSetChanged();
}
});
channels.add(channel);
adapter.notifyDataSetChanged();
}

@Override
public void onChannelChange(Channel channel)
{
logger.d("Received onChannelChange callback for channel |" + channel.getFriendlyName()
+ "|");
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.notifyDataSetChanged();
}
});
adapter.notifyDataSetChanged();
}

@Override
public void onChannelDelete(final Channel channel)
{
logger.d("Received onChannelDelete callback for channel |" + channel.getFriendlyName()
+ "|");
runOnUiThread(new Runnable() {
@Override
public void run()
{
channels.remove(channel);
adapter.notifyDataSetChanged();
}
});
channels.remove(channel);
adapter.notifyDataSetChanged();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,26 +548,14 @@ public void onClick(DialogInterface dialog, int id)
((EditText)editTextDialog.findViewById(R.id.update_message))
.getText()
.toString();
message.updateMessageBody(updatedMsg, new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().showError(errorInfo);
TwilioApplication.get().logErrorInfo("Error updating message",
errorInfo);
}

message.updateMessageBody(updatedMsg, new ToastStatusListener(
"Success updating message",
"Error updating message") {
@Override
public void onSuccess()
{
logger.d("Success updating message");
runOnUiThread(new Runnable() {
@Override
public void run()
{
loadAndShowMessages();// @todo only need to update one message body
}
});
super.onSuccess();
loadAndShowMessages();// @todo only need to update one message body
}
});
}
Expand Down Expand Up @@ -608,26 +596,14 @@ public void onClick(DialogInterface dialog, int id)
}
}

message.setAttributes(jsonObj, new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().showError(errorInfo);
TwilioApplication.get().logErrorInfo(
"Error updating message attributes", errorInfo);
}

message.setAttributes(jsonObj, new ToastStatusListener(
"Success updating message attributes",
"Error updating message attributes") {
@Override
public void onSuccess()
{
logger.d("Success updating message attributes");
runOnUiThread(new Runnable() {
@Override
public void run()
{
loadAndShowMessages();// @todo only need to update one message
}
});
super.onSuccess();
loadAndShowMessages();// @todo only need to update one message
}
});
}
Expand Down Expand Up @@ -747,28 +723,15 @@ public void onClick(DialogInterface dialog, int which)
if (which == REMOVE) {
dialog.cancel();
messagesObject.removeMessage(
message.getMessage(), new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().showError(errorInfo);
TwilioApplication.get().logErrorInfo(
"Error removing message", errorInfo);
}

message.getMessage(), new ToastStatusListener(
"Successfully removed message. It should be GONE!!",
"Error removing message") {
@Override
public void onSuccess()
{
logger.d(
"Successfully removed message. It should be GONE!!");
runOnUiThread(new Runnable() {
@Override
public void run()
{
messageItemList.remove(message);
adapter.notifyDataSetChanged();
}
});
super.onSuccess();
messageItemList.remove(message);
adapter.notifyDataSetChanged();
}
});
} else if (which == EDIT) {
Expand Down Expand Up @@ -805,13 +768,8 @@ public void onSuccess(List<Message> messagesArray) {
items[i] = new MessageItem(messages.get(i), members, identity);
}
messageItemList = new ArrayList<>(Arrays.asList(items));
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.setItems(messageItemList);
adapter.notifyDataSetChanged();
}
});

adapter.setItems(messageItemList);
}
});
}
Expand All @@ -824,26 +782,15 @@ private void sendMessage()
if (!input.equals("")) {
final Messages messagesObject = this.channel.getMessages();

messagesObject.sendMessage(input, new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().showError(errorInfo);
TwilioApplication.get().logErrorInfo("Error sending message", errorInfo);
}

messagesObject.sendMessage(input, new ToastStatusListener(
"Successfully sent message",
"Error sending message") {
@Override
public void onSuccess()
{
logger.d("Successfully sent message.");
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.notifyDataSetChanged();
inputText.setText("");
}
});
super.onSuccess();
adapter.notifyDataSetChanged();
inputText.setText("");
}
});
}
Expand Down
Loading

0 comments on commit 1f0ba93

Please sign in to comment.