11package com .cometchat .javasampleapp .activity ;
22
3+ import android .content .Context ;
34import android .content .Intent ;
45import android .content .res .ColorStateList ;
56import android .os .Bundle ;
7+ import android .util .Log ;
68import android .view .View ;
9+ import android .widget .ImageView ;
710import android .widget .LinearLayout ;
11+ import android .widget .ProgressBar ;
12+ import android .widget .TextView ;
813import android .widget .Toast ;
914
1015import androidx .appcompat .app .AppCompatActivity ;
1116import androidx .appcompat .widget .AppCompatImageView ;
1217import androidx .appcompat .widget .AppCompatTextView ;
1318import androidx .core .content .ContextCompat ;
1419
20+ import com .bumptech .glide .Glide ;
1521import com .cometchat .chatuikit .shared .cometchatuikit .CometChatUIKit ;
1622import com .cometchat .chatuikit .shared .cometchatuikit .UIKitSettings ;
1723import com .cometchat .chatuikit .shared .resources .utils .Utils ;
1824import com .cometchat .chat .core .CometChat ;
1925import com .cometchat .chat .exceptions .CometChatException ;
2026import com .cometchat .javasampleapp .AppConstants ;
2127import com .cometchat .javasampleapp .AppUtils ;
22- import com .cometchat .javasampleapp .Application ;
2328import com .cometchat .javasampleapp .BuildConfig ;
2429import com .cometchat .javasampleapp .R ;
2530import com .cometchat .chat .models .User ;
2631import com .google .android .material .card .MaterialCardView ;
2732
2833import org .json .JSONObject ;
2934
35+ import java .io .IOException ;
36+ import java .io .InputStream ;
37+ import java .util .List ;
38+
3039public class MainActivity extends AppCompatActivity {
3140
32- private MaterialCardView superhero1 ;
41+ private MaterialCardView user1 ;
3342
34- private MaterialCardView superhero2 ;
43+ private MaterialCardView user2 ;
3544
36- private MaterialCardView superhero3 ;
45+ private MaterialCardView user3 ;
3746
38- private MaterialCardView superhero4 ;
47+ private MaterialCardView user4 ;
3948
4049 private AppCompatImageView ivLogo ;
41-
50+ private ProgressBar progressBar ;
4251 private AppCompatTextView tvCometChat ;
4352 private LinearLayout parentView ;
53+ private LinearLayout gridLayoutContainer ;
54+ private TextView stateMessage ;
55+ private LinearLayout stateLayout ;
56+ private TextView user1Name , user2Name , user3Name , user4Name ;
57+ private ImageView user1Avatar , user2Avatar , user3Avatar , user4Avatar ;
58+
4459
4560 @ Override
4661 protected void onCreate (Bundle savedInstanceState ) {
4762 super .onCreate (savedInstanceState );
4863 setContentView (R .layout .activity_main );
4964 parentView = findViewById (R .id .parent_view );
65+ progressBar = findViewById (R .id .progress_bar );
66+ stateMessage = findViewById (R .id .state_message );
67+ stateLayout = findViewById (R .id .state_layout );
68+ gridLayoutContainer = findViewById (R .id .grid_layout_container );
69+ user1 = findViewById (R .id .user1 );
70+ user2 = findViewById (R .id .user2 );
71+ user3 = findViewById (R .id .user3 );
72+ user4 = findViewById (R .id .user4 );
73+ ivLogo = findViewById (R .id .ivLogo );
74+ tvCometChat = findViewById (R .id .tvComet );
75+ user1Name = findViewById (R .id .user1_name );
76+ user2Name = findViewById (R .id .user2_name );
77+ user3Name = findViewById (R .id .user3_name );
78+ user4Name = findViewById (R .id .user4_name );
79+ user1Avatar = findViewById (R .id .user1_avatar_image );
80+ user2Avatar = findViewById (R .id .user2_avatar_image );
81+ user3Avatar = findViewById (R .id .user3_avatar_image );
82+ user4Avatar = findViewById (R .id .user4_avatar_image );
83+
84+ user1 .setVisibility (View .GONE );
85+ user2 .setVisibility (View .GONE );
86+ user3 .setVisibility (View .GONE );
87+ user4 .setVisibility (View .GONE );
88+
89+ gridLayoutContainer .setVisibility (View .INVISIBLE );
90+ stateMessage .setText (R .string .please_wait );
91+ progressBar .setVisibility (View .VISIBLE );
5092 Utils .setStatusBarColor (this , getResources ().getColor (android .R .color .white ));
5193 UIKitSettings uiKitSettings = new UIKitSettings .UIKitSettingsBuilder ().setRegion (AppConstants .REGION ).setAppId (AppConstants .APP_ID ).setAuthKey (AppConstants .AUTH_KEY ).subscribePresenceForAllUsers ().build ();
5294 CometChatUIKit .init (this , uiKitSettings , new CometChat .CallbackListener <String >() {
@@ -65,29 +107,42 @@ public void onError(CometChatException e) {
65107 Toast .makeText (MainActivity .this , e .getMessage (), Toast .LENGTH_SHORT ).show ();
66108 }
67109 });
68- superhero1 = findViewById (R .id .superhero1 );
69- superhero2 = findViewById (R .id .superhero2 );
70- superhero3 = findViewById (R .id .superhero3 );
71- superhero4 = findViewById (R .id .superhero4 );
72- ivLogo = findViewById (R .id .ivLogo );
73- tvCometChat = findViewById (R .id .tvComet );
110+
111+
112+ AppUtils .fetchSampleUsers (new CometChat .CallbackListener <List <User >>() {
113+ @ Override
114+ public void onSuccess (List <User > users ) {
115+ if (!users .isEmpty ()) {
116+ setUsers (users );
117+ } else {
118+ stateLayout .setVisibility (View .VISIBLE );
119+ progressBar .setVisibility (View .GONE );
120+ stateMessage .setText (R .string .no_sample_users_available );
121+ }
122+ }
123+
124+ @ Override
125+ public void onError (CometChatException e ) {
126+ setUsers (AppUtils .processSampleUserList (AppUtils .loadJSONFromAsset (MainActivity .this )));
127+ }
128+ });
74129 findViewById (R .id .login ).setOnClickListener (view -> startActivity (new Intent (MainActivity .this , LoginActivity .class )));
75130
76- superhero1 .setOnClickListener (view -> {
77- findViewById (R .id .superhero1Progressbar ).setVisibility (View .VISIBLE );
78- login ("superhero1" );
131+ user1 .setOnClickListener (view -> {
132+ findViewById (R .id .user1Progressbar ).setVisibility (View .VISIBLE );
133+ login (user1 . getTag (). toString () );
79134 });
80- superhero2 .setOnClickListener (view -> {
81- findViewById (R .id .superhero2Progressbar ).setVisibility (View .VISIBLE );
82- login ("superhero2" );
135+ user2 .setOnClickListener (view -> {
136+ findViewById (R .id .user2Progressbar ).setVisibility (View .VISIBLE );
137+ login (user2 . getTag (). toString () );
83138 });
84- superhero3 .setOnClickListener (view -> {
85- findViewById (R .id .superhero3Progressbar ).setVisibility (View .VISIBLE );
86- login ("superhero3" );
139+ user3 .setOnClickListener (view -> {
140+ findViewById (R .id .user3Progressbar ).setVisibility (View .VISIBLE );
141+ login (user3 . getTag (). toString () );
87142 });
88- superhero4 .setOnClickListener (view -> {
89- findViewById (R .id .superhero4Progressbar ).setVisibility (View .VISIBLE );
90- login ("superhero4" );
143+ user4 .setOnClickListener (view -> {
144+ findViewById (R .id .user4Progressbar ).setVisibility (View .VISIBLE );
145+ login (user4 . getTag (). toString () );
91146 });
92147
93148 if (Utils .isDarkMode (this )) {
@@ -98,6 +153,35 @@ public void onError(CometChatException e) {
98153 setUpUI ();
99154 }
100155
156+ private void setUsers (List <User > users ) {
157+ progressBar .setVisibility (View .GONE );
158+ stateLayout .setVisibility (View .GONE );
159+ gridLayoutContainer .setVisibility (View .VISIBLE );
160+ for (int i = 0 ; i < users .size (); i ++) {
161+ if (i == 0 ) {
162+ user1Name .setText (users .get (i ).getName ());
163+ Glide .with (this ).load (users .get (i ).getAvatar ()).error (R .drawable .ironman ).into (user1Avatar );
164+ user1 .setTag (users .get (i ).getUid ());
165+ user1 .setVisibility (View .VISIBLE );
166+ } else if (i == 1 ) {
167+ user2Name .setText (users .get (i ).getName ());
168+ Glide .with (this ).load (users .get (i ).getAvatar ()).error (R .drawable .captainamerica ).into (user2Avatar );
169+ user2 .setTag (users .get (i ).getUid ());
170+ user2 .setVisibility (View .VISIBLE );
171+ } else if (i == 2 ) {
172+ user3Name .setText (users .get (i ).getName ());
173+ Glide .with (this ).load (users .get (i ).getAvatar ()).error (R .drawable .spiderman ).into (user3Avatar );
174+ user3 .setTag (users .get (i ).getUid ());
175+ user3 .setVisibility (View .VISIBLE );
176+ } else if (i == 3 ) {
177+ user4Name .setText (users .get (i ).getName ());
178+ Glide .with (this ).load (users .get (i ).getAvatar ()).error (R .drawable .wolverine ).into (user4Avatar );
179+ user4 .setTag (users .get (i ).getUid ());
180+ user4 .setVisibility (View .VISIBLE );
181+ }
182+ }
183+ }
184+
101185 private void login (String uid ) {
102186 CometChatUIKit .login (uid , new CometChat .CallbackListener <User >() {
103187 @ Override
0 commit comments