package com.quickblox.sample.groupchatwebrtc.utils;
import android.content.Context;
import android.text.TextUtils;
import com.quickblox.sample.core.utils.SharedPrefsHelper;
import com.quickblox.sample.groupchatwebrtc.db.QbUsersDbManager;
import com.quickblox.users.model.QBUser;
import java.util.ArrayList;
import java.util.List;
/**
* Created by tereha on 09.06.16.
*/
public class UsersUtils {
private static SharedPrefsHelper sharedPrefsHelper;
private static QbUsersDbManager dbManager;
public static String getUserNameOrId(QBUser qbUser, Integer userId) {
if (qbUser == null) {
return String.valueOf(userId);
}
String fullName = qbUser.getFullName();
return TextUtils.isEmpty(fullName) ? String.valueOf(userId) : fullName;
}
public static ArrayList<QBUser> getListAllUsersFromIds(ArrayList<QBUser> existedUsers, List<Integer> allIds) {
ArrayList<QBUser> qbUsers = new ArrayList<>();
for (Integer userId : allIds) {
QBUser stubUser = createStubUserById(userId);
if (!existedUsers.contains(stubUser)) {
qbUsers.add(stubUser);
}
}
qbUsers.addAll(existedUsers);
return qbUsers;
}
private static QBUser createStubUserById(Integer userId) {
QBUser stubUser = new QBUser(userId);
stubUser.setFullName(String.valueOf(userId));
return stubUser;
}
public static ArrayList<Integer> getIdsNotLoadedUsers(ArrayList<QBUser> existedUsers, List<Integer> allIds) {
ArrayList<Integer> idsNotLoadedUsers = new ArrayList<>();
for (Integer userId : allIds) {
QBUser stubUser = createStubUserById(userId);
if (!existedUsers.contains(stubUser)) {
idsNotLoadedUsers.add(userId);
}
}
return idsNotLoadedUsers;
}
public static void removeUserData(Context context) {
if (sharedPrefsHelper == null) {
sharedPrefsHelper = SharedPrefsHelper.getInstance();
}
sharedPrefsHelper.clearAllData();
if (dbManager == null) {
dbManager = QbUsersDbManager.getInstance(context);
}
dbManager.clearDB();
}
}