Skip to content

Commit

Permalink
v4.3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekCometChat committed Jun 19, 2024
1 parent 9b741c1 commit 2d631e5
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 253 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "com.cometchat.javasampleapp"
minSdk 21
targetSdk 33
versionCode 10
versionName "4.3.6"
versionCode 11
versionName "4.3.11"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
manifestPlaceholders = [file_provider: "com.cometchat.javasampleapp"]
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/assets/SampleUsers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"users": [
{
"uid": "superhero1",
"name": "Iron Man",
"avatar": "https://assets.cometchat.io/sampleapp/users/ironman.png"
},
{
"uid": "superhero2",
"name": "Captain America",
"avatar": "https://assets.cometchat.io/sampleapp/users/captainamerica.png"
},
{
"uid": "superhero3",
"name": "Spiderman",
"avatar": "https://assets.cometchat.io/sampleapp/users/spiderman.png"
},
{
"uid": "superhero4",
"name": "Wolverine",
"avatar": "https://assets.cometchat.io/sampleapp/users/wolverine.png"
},
{
"uid": "superhero5",
"name": "Cyclops",
"avatar": "https://assets.cometchat.io/sampleapp/users/cyclops.png"
}
]
}
82 changes: 80 additions & 2 deletions app/src/main/java/com/cometchat/javasampleapp/AppUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@
import com.cometchat.chat.core.CometChat;
import com.cometchat.chat.exceptions.CometChatException;
import com.cometchat.chat.models.Group;
import com.cometchat.chat.models.TextMessage;
import com.cometchat.chat.models.User;
import com.cometchat.chatuikit.shared.resources.utils.Utils;
import com.cometchat.javasampleapp.constants.StringConstants;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AppUtils {
private static Group group;
private static User user;
private static List<User> userList = new ArrayList<>();

public static void fetchDefaultObjects() {
CometChat.getGroup("supergroup", new CometChat.CallbackListener<Group>() {
Expand All @@ -31,7 +47,7 @@ public void onError(CometChatException e) {

}
});
CometChat.getUser("superhero5", new CometChat.CallbackListener<User>() {
CometChat.getUser(!userList.isEmpty() ? userList.get(userList.size() - 1).getUid() : "superhero5", new CometChat.CallbackListener<User>() {
@Override
public void onSuccess(User user_) {
user = user_;
Expand All @@ -44,6 +60,68 @@ public void onError(CometChatException e) {
});
}

public static void fetchSampleUsers(CometChat.CallbackListener<List<User>> listener) {
if (userList.isEmpty()) {
Request request = new Request.Builder().url(StringConstants.SAMPLE_APP_USERS_URL).method("GET", null).build();
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
Utils.runOnMainThread(() -> listener.onError(new CometChatException("11", e.getMessage())));
}

public void onResponse(Call call, Response response) {
if (response.isSuccessful() && response.body() != null) {
try {
userList = processSampleUserList(response.body().string());
} catch (IOException e) {
Utils.runOnMainThread(() -> listener.onError(new CometChatException("10", e.getMessage())));
}
Utils.runOnMainThread(() -> listener.onSuccess(userList));
} else {
Utils.runOnMainThread(() -> listener.onError(new CometChatException("Unexpected code ", String.valueOf(response.code()))));
}
}
});
} else {
Utils.runOnMainThread(() -> listener.onSuccess(userList));
}
}

public static List<User> processSampleUserList(String jsonString) {
List<User> users = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray(StringConstants.KEY_USER);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject userJson = jsonArray.getJSONObject(i);
User user = new User();
user.setUid(userJson.getString(StringConstants.UID));
user.setName(userJson.getString(StringConstants.NAME));
user.setAvatar(userJson.getString(StringConstants.AVATAR));
users.add(user);
}
} catch (Exception ignore) {

}
return users;
}

public static String loadJSONFromAsset(Context context) {
String json = null;
try {
InputStream is = context.getAssets().open("SampleUsers.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}

public static Group getDefaultGroup() {
return group;
}
Expand Down
132 changes: 108 additions & 24 deletions app/src/main/java/com/cometchat/javasampleapp/activity/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,94 @@
package com.cometchat.javasampleapp.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.cometchat.chatuikit.shared.cometchatuikit.CometChatUIKit;
import com.cometchat.chatuikit.shared.cometchatuikit.UIKitSettings;
import com.cometchat.chatuikit.shared.resources.utils.Utils;
import com.cometchat.chat.core.CometChat;
import com.cometchat.chat.exceptions.CometChatException;
import com.cometchat.javasampleapp.AppConstants;
import com.cometchat.javasampleapp.AppUtils;
import com.cometchat.javasampleapp.Application;
import com.cometchat.javasampleapp.BuildConfig;
import com.cometchat.javasampleapp.R;
import com.cometchat.chat.models.User;
import com.google.android.material.card.MaterialCardView;

import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private MaterialCardView superhero1;
private MaterialCardView user1;

private MaterialCardView superhero2;
private MaterialCardView user2;

private MaterialCardView superhero3;
private MaterialCardView user3;

private MaterialCardView superhero4;
private MaterialCardView user4;

private AppCompatImageView ivLogo;

private ProgressBar progressBar;
private AppCompatTextView tvCometChat;
private LinearLayout parentView;
private LinearLayout gridLayoutContainer;
private TextView stateMessage;
private LinearLayout stateLayout;
private TextView user1Name, user2Name, user3Name, user4Name;
private ImageView user1Avatar, user2Avatar, user3Avatar, user4Avatar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentView = findViewById(R.id.parent_view);
progressBar = findViewById(R.id.progress_bar);
stateMessage = findViewById(R.id.state_message);
stateLayout = findViewById(R.id.state_layout);
gridLayoutContainer = findViewById(R.id.grid_layout_container);
user1 = findViewById(R.id.user1);
user2 = findViewById(R.id.user2);
user3 = findViewById(R.id.user3);
user4 = findViewById(R.id.user4);
ivLogo = findViewById(R.id.ivLogo);
tvCometChat = findViewById(R.id.tvComet);
user1Name = findViewById(R.id.user1_name);
user2Name = findViewById(R.id.user2_name);
user3Name = findViewById(R.id.user3_name);
user4Name = findViewById(R.id.user4_name);
user1Avatar = findViewById(R.id.user1_avatar_image);
user2Avatar = findViewById(R.id.user2_avatar_image);
user3Avatar = findViewById(R.id.user3_avatar_image);
user4Avatar = findViewById(R.id.user4_avatar_image);

user1.setVisibility(View.GONE);
user2.setVisibility(View.GONE);
user3.setVisibility(View.GONE);
user4.setVisibility(View.GONE);

gridLayoutContainer.setVisibility(View.INVISIBLE);
stateMessage.setText(R.string.please_wait);
progressBar.setVisibility(View.VISIBLE);
Utils.setStatusBarColor(this, getResources().getColor(android.R.color.white));
UIKitSettings uiKitSettings = new UIKitSettings.UIKitSettingsBuilder().setRegion(AppConstants.REGION).setAppId(AppConstants.APP_ID).setAuthKey(AppConstants.AUTH_KEY).subscribePresenceForAllUsers().build();
CometChatUIKit.init(this, uiKitSettings, new CometChat.CallbackListener<String>() {
Expand All @@ -65,29 +107,42 @@ public void onError(CometChatException e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
superhero1 = findViewById(R.id.superhero1);
superhero2 = findViewById(R.id.superhero2);
superhero3 = findViewById(R.id.superhero3);
superhero4 = findViewById(R.id.superhero4);
ivLogo = findViewById(R.id.ivLogo);
tvCometChat = findViewById(R.id.tvComet);


AppUtils.fetchSampleUsers(new CometChat.CallbackListener<List<User>>() {
@Override
public void onSuccess(List<User> users) {
if (!users.isEmpty()) {
setUsers(users);
} else {
stateLayout.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
stateMessage.setText(R.string.no_sample_users_available);
}
}

@Override
public void onError(CometChatException e) {
setUsers(AppUtils.processSampleUserList(AppUtils.loadJSONFromAsset(MainActivity.this)));
}
});
findViewById(R.id.login).setOnClickListener(view -> startActivity(new Intent(MainActivity.this, LoginActivity.class)));

superhero1.setOnClickListener(view -> {
findViewById(R.id.superhero1Progressbar).setVisibility(View.VISIBLE);
login("superhero1");
user1.setOnClickListener(view -> {
findViewById(R.id.user1Progressbar).setVisibility(View.VISIBLE);
login(user1.getTag().toString());
});
superhero2.setOnClickListener(view -> {
findViewById(R.id.superhero2Progressbar).setVisibility(View.VISIBLE);
login("superhero2");
user2.setOnClickListener(view -> {
findViewById(R.id.user2Progressbar).setVisibility(View.VISIBLE);
login(user2.getTag().toString());
});
superhero3.setOnClickListener(view -> {
findViewById(R.id.superhero3Progressbar).setVisibility(View.VISIBLE);
login("superhero3");
user3.setOnClickListener(view -> {
findViewById(R.id.user3Progressbar).setVisibility(View.VISIBLE);
login(user3.getTag().toString());
});
superhero4.setOnClickListener(view -> {
findViewById(R.id.superhero4Progressbar).setVisibility(View.VISIBLE);
login("superhero4");
user4.setOnClickListener(view -> {
findViewById(R.id.user4Progressbar).setVisibility(View.VISIBLE);
login(user4.getTag().toString());
});

if (Utils.isDarkMode(this)) {
Expand All @@ -98,6 +153,35 @@ public void onError(CometChatException e) {
setUpUI();
}

private void setUsers(List<User> users) {
progressBar.setVisibility(View.GONE);
stateLayout.setVisibility(View.GONE);
gridLayoutContainer.setVisibility(View.VISIBLE);
for (int i = 0; i < users.size(); i++) {
if (i == 0) {
user1Name.setText(users.get(i).getName());
Glide.with(this).load(users.get(i).getAvatar()).error(R.drawable.ironman).into(user1Avatar);
user1.setTag(users.get(i).getUid());
user1.setVisibility(View.VISIBLE);
} else if (i == 1) {
user2Name.setText(users.get(i).getName());
Glide.with(this).load(users.get(i).getAvatar()).error(R.drawable.captainamerica).into(user2Avatar);
user2.setTag(users.get(i).getUid());
user2.setVisibility(View.VISIBLE);
} else if (i == 2) {
user3Name.setText(users.get(i).getName());
Glide.with(this).load(users.get(i).getAvatar()).error(R.drawable.spiderman).into(user3Avatar);
user3.setTag(users.get(i).getUid());
user3.setVisibility(View.VISIBLE);
} else if (i == 3) {
user4Name.setText(users.get(i).getName());
Glide.with(this).load(users.get(i).getAvatar()).error(R.drawable.wolverine).into(user4Avatar);
user4.setTag(users.get(i).getUid());
user4.setVisibility(View.VISIBLE);
}
}
}

private void login(String uid) {
CometChatUIKit.login(uid, new CometChat.CallbackListener<User>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

public class StringConstants {

public static final String MODULE="module";
public static final String CONVERSATIONS="Conversations";
public static final String USERS="Users";
public static final String GROUPS="Groups";
public static final String MESSAGES="Messages";
public static final String SHARED="Shared";
public static final String CALLS="Calls";

public static final String MODULE = "module";
public static final String CONVERSATIONS = "Conversations";
public static final String USERS = "Users";
public static final String GROUPS = "Groups";
public static final String MESSAGES = "Messages";
public static final String SHARED = "Shared";
public static final String CALLS = "Calls";
public static final String KEY_USER = "users";
public static final String SAMPLE_APP_USERS_URL = "https://assets.cometchat.io/sampleapp/sampledata.json";
public static final String UID = "uid";
public static final String NAME = "name";
public static final String AVATAR = "avatar";

}
Loading

0 comments on commit 2d631e5

Please sign in to comment.