|
| 1 | +package net.servicestack.android; |
| 2 | + |
| 3 | +import net.servicestack.client.AsyncError; |
| 4 | +import net.servicestack.client.AsyncResult; |
| 5 | +import net.servicestack.client.AsyncResultVoid; |
| 6 | +import net.servicestack.client.AsyncSuccess; |
| 7 | +import net.servicestack.client.AsyncSuccessVoid; |
| 8 | +import net.servicestack.client.sse.GetEventSubscribers; |
| 9 | +import net.servicestack.client.sse.ServerEventUser; |
| 10 | +import net.servicestack.client.sse.ServerEventsClient; |
| 11 | +import net.servicestack.client.sse.UpdateEventSubscriber; |
| 12 | +import net.servicestack.client.sse.UpdateEventSubscriberResponse; |
| 13 | +import net.servicestack.func.Func; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by mythz on 2/15/2017. |
| 21 | + */ |
| 22 | + |
| 23 | +public class AndroidServerEventsClient extends ServerEventsClient { |
| 24 | + public AndroidServerEventsClient(String baseUri, String... channels) { |
| 25 | + super(baseUri, channels); |
| 26 | + this.serviceClient = new AndroidServiceClient(this.baseUri); |
| 27 | + } |
| 28 | + |
| 29 | + public AndroidServerEventsClient(String baseUrl, String channel) { |
| 30 | + this(baseUrl, new String[]{ channel }); |
| 31 | + } |
| 32 | + |
| 33 | + public AndroidServerEventsClient(String baseUrl) { |
| 34 | + this(baseUrl, new String[]{}); |
| 35 | + } |
| 36 | + |
| 37 | + public AndroidServiceClient getAndroidClient(){ |
| 38 | + return (AndroidServiceClient) this.serviceClient; |
| 39 | + } |
| 40 | + |
| 41 | + public void getChannelSubscribersAsync(final AsyncResult<List<ServerEventUser>> asyncResult){ |
| 42 | + this.getAndroidClient().getAsync( |
| 43 | + new GetEventSubscribers().setChannels(Func.toList(this.getChannels())), |
| 44 | + new AsyncResult<ArrayList<HashMap<String,String>>>(){ |
| 45 | + @Override |
| 46 | + public void success(ArrayList<HashMap<String,String>> response) { |
| 47 | + asyncResult.success(toServerEventUser(response)); |
| 48 | + } |
| 49 | + @Override |
| 50 | + public void error(Exception ex) { |
| 51 | + asyncResult.error(ex); |
| 52 | + } |
| 53 | + @Override |
| 54 | + public void complete() { |
| 55 | + asyncResult.complete(); |
| 56 | + } |
| 57 | + } |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public void getChannelSubscribersAsync(AsyncSuccess<List<ServerEventUser>> success){ |
| 62 | + getChannelSubscribersAsync(AsyncUtils.createAsyncResult(success, null)); |
| 63 | + } |
| 64 | + |
| 65 | + public void getChannelSubscribersAsync(AsyncSuccess<List<ServerEventUser>> success, AsyncError error){ |
| 66 | + getChannelSubscribersAsync(AsyncUtils.createAsyncResult(success, error)); |
| 67 | + } |
| 68 | + |
| 69 | + public void updateSubscriberAsync(final UpdateEventSubscriber request, final AsyncResultVoid asyncResult){ |
| 70 | + if (request.getId() == null) |
| 71 | + request.setId(connectionInfo.getId()); |
| 72 | + |
| 73 | + getAndroidClient().postAsync(request, new AsyncResult<UpdateEventSubscriberResponse>() { |
| 74 | + @Override |
| 75 | + public void success(UpdateEventSubscriberResponse response) { |
| 76 | + asyncResult.success(); |
| 77 | + update( |
| 78 | + Func.toArray(request.getSubscribeChannels(), String.class), |
| 79 | + Func.toArray(request.getUnsubscribeChannels(), String.class)); |
| 80 | + } |
| 81 | + @Override |
| 82 | + public void error(Exception ex) { |
| 83 | + asyncResult.error(ex); |
| 84 | + } |
| 85 | + @Override |
| 86 | + public void complete() { |
| 87 | + asyncResult.complete(); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + public void updateSubscriberAsync(final UpdateEventSubscriber request, final AsyncSuccessVoid success){ |
| 93 | + updateSubscriberAsync(request, AsyncUtils.createAsyncResult(success, null)); |
| 94 | + } |
| 95 | + |
| 96 | + public void updateSubscriberAsync(final UpdateEventSubscriber request, final AsyncSuccessVoid success, AsyncError error){ |
| 97 | + updateSubscriberAsync(request, AsyncUtils.createAsyncResult(success, error)); |
| 98 | + } |
| 99 | + |
| 100 | + public void subscribeToChannelsAsync(final String[] channels, final AsyncResultVoid asyncResult) |
| 101 | + { |
| 102 | + getAndroidClient().postAsync(new UpdateEventSubscriber() |
| 103 | + .setId(connectionInfo.getId()) |
| 104 | + .setSubscribeChannels(Func.toList(channels)), |
| 105 | + new AsyncResult<UpdateEventSubscriberResponse>() { |
| 106 | + @Override |
| 107 | + public void success(UpdateEventSubscriberResponse response) { |
| 108 | + asyncResult.success(); |
| 109 | + update(channels, null); |
| 110 | + } |
| 111 | + @Override |
| 112 | + public void error(Exception ex) { |
| 113 | + asyncResult.error(ex); |
| 114 | + } |
| 115 | + @Override |
| 116 | + public void complete() { |
| 117 | + asyncResult.complete(); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + public void subscribeToChannelsAsync(final String[] channels, final AsyncSuccess success){ |
| 123 | + subscribeToChannelsAsync(channels, AsyncUtils.createAsyncResult(success, null)); |
| 124 | + } |
| 125 | + |
| 126 | + public void subscribeToChannelsAsync(final String[] channels, final AsyncSuccess success, final AsyncError error){ |
| 127 | + subscribeToChannelsAsync(channels, AsyncUtils.createAsyncResult(success, error)); |
| 128 | + } |
| 129 | + |
| 130 | + public void unSubscribeFromChannelsAsync(final String[] channels, final AsyncResultVoid asyncResult) |
| 131 | + { |
| 132 | + getAndroidClient().postAsync(new UpdateEventSubscriber() |
| 133 | + .setId(connectionInfo.getId()) |
| 134 | + .setUnsubscribeChannels(Func.toList(channels)), |
| 135 | + new AsyncResult<UpdateEventSubscriberResponse>() { |
| 136 | + @Override |
| 137 | + public void success(UpdateEventSubscriberResponse response) { |
| 138 | + asyncResult.success(); |
| 139 | + update(null, channels); |
| 140 | + } |
| 141 | + @Override |
| 142 | + public void error(Exception ex) { |
| 143 | + asyncResult.error(ex); |
| 144 | + } |
| 145 | + @Override |
| 146 | + public void complete() { |
| 147 | + asyncResult.complete(); |
| 148 | + } |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + public void unSubscribeFromChannelsAsync(final String[] channels, final AsyncSuccess success){ |
| 153 | + unSubscribeFromChannelsAsync(channels, AsyncUtils.createAsyncResult(success, null)); |
| 154 | + } |
| 155 | + |
| 156 | + public void unSubscribeFromChannelsAsync(final String[] channels, final AsyncSuccess success, final AsyncError error){ |
| 157 | + unSubscribeFromChannelsAsync(channels, AsyncUtils.createAsyncResult(success, error)); |
| 158 | + } |
| 159 | +} |
0 commit comments