11package servicestack .net .androidchat ;
22
3- import android .app .Activity ;
43import android .content .Intent ;
54import android .content .res .Configuration ;
65import android .graphics .Bitmap ;
76import android .os .Bundle ;
8- import android .os .PersistableBundle ;
97import android .support .design .widget .NavigationView ;
108import android .support .v4 .util .LruCache ;
119import android .support .v4 .widget .DrawerLayout ;
3836
3937public class MainActivity extends AppCompatActivity {
4038
41- public static final String BaseUrl = "http://chat.servicestack.net/" ;
39+ // Note: Your consumer key and secret should be obfuscated in your source code before shipping.
4240 private ChatActionBarDrawerToggle drawerToggle ;
4341 private DrawerLayout drawerLayout ;
4442 private ListView rightDrawer ;
@@ -48,7 +46,6 @@ public class MainActivity extends AppCompatActivity {
4846
4947 private List <Exception > errors = new ArrayList <>();
5048
51- private AndroidServerEventsClient client ;
5249 private ChatCommandHandler cmdReceiver ;
5350
5451 private List <ServerEventUser > subscriberList = new ArrayList <>();
@@ -61,9 +58,14 @@ public class MainActivity extends AppCompatActivity {
6158 "Logout" , "/logout"
6259 );
6360
61+ public AndroidServerEventsClient getClient (){
62+ return App .get ().getServerEventsClient ();
63+ }
64+
6465 @ Override
6566 protected void onCreate (Bundle savedInstanceState ) {
6667 super .onCreate (savedInstanceState );
68+
6769 setContentView (R .layout .activity_main );
6870
6971 Button sendButton = (Button )findViewById (R .id .sendMessageButton );
@@ -87,18 +89,17 @@ protected void onCreate(Bundle savedInstanceState) {
8789 this , new ArrayList <>(), () -> this .subscriberList );
8890 messageHistoryList .setAdapter (messageHistoryAdapter );
8991
90- String [] channels = new String [] { "home" };
9192 MainActivity mainActivity = this ;
9293 cmdReceiver = new ChatCommandHandler (mainActivity , messageHistoryAdapter , "home" );
9394
94- client = new AndroidServerEventsClient ( BaseUrl , channels );
95- client .setOnConnect (connectMsg -> {
96- Extensions .updateChatHistory (client , cmdReceiver );
95+ getClient ()
96+ .setOnConnect (connectMsg -> {
97+ Extensions .updateChatHistory (getClient () , cmdReceiver );
9798 Extensions .updateUserProfile (connectMsg , mainActivity );
9899 })
99100 .setOnCommand (command -> {
100101 if (command instanceof ServerEventJoin ){
101- client .getChannelSubscribersAsync (r -> {
102+ getClient () .getChannelSubscribersAsync (r -> {
102103 subscriberList = r ;
103104 // Refresh profile icons when users join
104105 messageHistoryAdapter .notifyDataSetChanged ();
@@ -162,17 +163,17 @@ public boolean onChannelClick(MenuItem menuItem)
162163 if (Objects .equals (itemText , UiHelpers .CreateChannelLabel )){
163164 UiHelpers .showChannelDialog (this , nChannel -> {
164165 try {
165- List <String > nChannels = Func .toList (client .getChannels ());
166+ List <String > nChannels = Func .toList (getClient () .getChannels ());
166167 nChannels .add (nChannel );
167168 UiHelpers .resetChannelDrawer (this , navigationView , Func .toArray (nChannels , String .class ));
168- Extensions .changeChannel (client , nChannel , cmdReceiver );
169+ Extensions .changeChannel (getClient () , nChannel , cmdReceiver );
169170 cmdReceiver .syncAdapter ();
170171 } catch (Exception ex ){
171172 errors .add (ex );
172173 }
173174 });
174175 } else {
175- Extensions .changeChannel (client , itemText , cmdReceiver );
176+ Extensions .changeChannel (getClient () , itemText , cmdReceiver );
176177 }
177178 drawerLayout .closeDrawer (navigationView );
178179 return true ;
@@ -189,19 +190,19 @@ public void onSendClick(View v)
189190 : messageBox .getText ().toString ();
190191
191192 if (Objects .equals (selector , "cmd.chat" )){
192- client . getAndroidClient ().postAsync (new PostChatToChannel ()
193+ App . get (). getServiceClient ().postAsync (new PostChatToChannel ()
193194 .setChannel (cmdReceiver .getCurrentChannel ())
194- .setFrom (client .getSubscriptionId ())
195+ .setFrom (getClient () .getSubscriptionId ())
195196 .setMessage (message )
196197 .setSelector (selector ),
197198 ignore -> {});
198199 }
199200 else if (Objects .equals (selector , "logout" )){
200201 performLogout ();
201202 } else {
202- client . getAndroidClient ().postAsync (new PostRawToChannel ()
203+ App . get (). getServiceClient ().postAsync (new PostRawToChannel ()
203204 .setChannel (cmdReceiver .getCurrentChannel ())
204- .setFrom (client .getSubscriptionId ())
205+ .setFrom (getClient () .getSubscriptionId ())
205206 .setMessage (message )
206207 .setSelector (selector ),
207208 () -> {});
@@ -213,7 +214,7 @@ else if (Objects.equals(selector, "logout")){
213214 private void performLogout () {
214215 TextView txtUser = (TextView )findViewById (R .id .txtUserName );
215216// AccountStore.Create(this).Delete(new Account(txtUser.getText()), "Twitter");
216- client .getServiceClient ().clearCookies ();
217+ App . get () .getServiceClient ().clearCookies ();
217218 Intent intent = new Intent (getBaseContext (), LoginActivity .class );
218219 intent .addFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP | Intent .FLAG_ACTIVITY_NEW_TASK );
219220
@@ -249,11 +250,11 @@ public boolean onCreateOptionsMenu(Menu menu) {
249250 public void onPostCreate (Bundle savedInstanceState ) {
250251 super .onPostCreate (savedInstanceState );
251252
252- Extensions .updateCookiesFromIntent (this , client );
253+ Extensions .updateCookiesFromIntent (this , getClient () );
253254
254255 drawerToggle .syncState ();
255- UiHelpers .resetChannelDrawer (this , navigationView , client .getChannels ());
256- client .start ();
256+ UiHelpers .resetChannelDrawer (this , navigationView , getClient () .getChannels ());
257+ getClient () .start ();
257258 }
258259
259260 @ Override
@@ -264,7 +265,7 @@ public void onConfigurationChanged(Configuration newConfig) {
264265
265266 @ Override
266267 protected void onDestroy () {
267- client .stop ();
268+ getClient () .stop ();
268269 cmdReceiver = null ;
269270 super .onDestroy ();
270271 }
0 commit comments