1
1
package com .cometchat .javasampleapp .activity ;
2
2
3
+ import android .content .Context ;
3
4
import android .content .Intent ;
4
5
import android .content .res .ColorStateList ;
5
6
import android .os .Bundle ;
7
+ import android .util .Log ;
6
8
import android .view .View ;
9
+ import android .widget .ImageView ;
7
10
import android .widget .LinearLayout ;
11
+ import android .widget .ProgressBar ;
12
+ import android .widget .TextView ;
8
13
import android .widget .Toast ;
9
14
10
15
import androidx .appcompat .app .AppCompatActivity ;
11
16
import androidx .appcompat .widget .AppCompatImageView ;
12
17
import androidx .appcompat .widget .AppCompatTextView ;
13
18
import androidx .core .content .ContextCompat ;
14
19
20
+ import com .bumptech .glide .Glide ;
15
21
import com .cometchat .chatuikit .shared .cometchatuikit .CometChatUIKit ;
16
22
import com .cometchat .chatuikit .shared .cometchatuikit .UIKitSettings ;
17
23
import com .cometchat .chatuikit .shared .resources .utils .Utils ;
18
24
import com .cometchat .chat .core .CometChat ;
19
25
import com .cometchat .chat .exceptions .CometChatException ;
20
26
import com .cometchat .javasampleapp .AppConstants ;
21
27
import com .cometchat .javasampleapp .AppUtils ;
22
- import com .cometchat .javasampleapp .Application ;
23
28
import com .cometchat .javasampleapp .BuildConfig ;
24
29
import com .cometchat .javasampleapp .R ;
25
30
import com .cometchat .chat .models .User ;
26
31
import com .google .android .material .card .MaterialCardView ;
27
32
28
33
import org .json .JSONObject ;
29
34
35
+ import java .io .IOException ;
36
+ import java .io .InputStream ;
37
+ import java .util .List ;
38
+
30
39
public class MainActivity extends AppCompatActivity {
31
40
32
- private MaterialCardView superhero1 ;
41
+ private MaterialCardView user1 ;
33
42
34
- private MaterialCardView superhero2 ;
43
+ private MaterialCardView user2 ;
35
44
36
- private MaterialCardView superhero3 ;
45
+ private MaterialCardView user3 ;
37
46
38
- private MaterialCardView superhero4 ;
47
+ private MaterialCardView user4 ;
39
48
40
49
private AppCompatImageView ivLogo ;
41
-
50
+ private ProgressBar progressBar ;
42
51
private AppCompatTextView tvCometChat ;
43
52
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
+
44
59
45
60
@ Override
46
61
protected void onCreate (Bundle savedInstanceState ) {
47
62
super .onCreate (savedInstanceState );
48
63
setContentView (R .layout .activity_main );
49
64
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 );
50
92
Utils .setStatusBarColor (this , getResources ().getColor (android .R .color .white ));
51
93
UIKitSettings uiKitSettings = new UIKitSettings .UIKitSettingsBuilder ().setRegion (AppConstants .REGION ).setAppId (AppConstants .APP_ID ).setAuthKey (AppConstants .AUTH_KEY ).subscribePresenceForAllUsers ().build ();
52
94
CometChatUIKit .init (this , uiKitSettings , new CometChat .CallbackListener <String >() {
@@ -65,29 +107,42 @@ public void onError(CometChatException e) {
65
107
Toast .makeText (MainActivity .this , e .getMessage (), Toast .LENGTH_SHORT ).show ();
66
108
}
67
109
});
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
+ });
74
129
findViewById (R .id .login ).setOnClickListener (view -> startActivity (new Intent (MainActivity .this , LoginActivity .class )));
75
130
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 () );
79
134
});
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 () );
83
138
});
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 () );
87
142
});
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 () );
91
146
});
92
147
93
148
if (Utils .isDarkMode (this )) {
@@ -98,6 +153,35 @@ public void onError(CometChatException e) {
98
153
setUpUI ();
99
154
}
100
155
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
+
101
185
private void login (String uid ) {
102
186
CometChatUIKit .login (uid , new CometChat .CallbackListener <User >() {
103
187
@ Override
0 commit comments