Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Add loader chatbot fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Ta0uf19 committed Jun 20, 2020
1 parent 7b75eeb commit 8d885fc
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 67 deletions.
31 changes: 0 additions & 31 deletions app/src/main/java/io/mycoach/fixtures/FixturesData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.UUID;

abstract class FixturesData {

Expand Down Expand Up @@ -72,35 +71,5 @@ abstract class FixturesData {
}
};

static String getRandomId() {
return Long.toString(UUID.randomUUID().getLeastSignificantBits());
}

static String getRandomAvatar() {
return avatars.get(rnd.nextInt(avatars.size()));
}

static String getRandomGroupChatImage() {
return groupChatImages.get(rnd.nextInt(groupChatImages.size()));
}

static String getRandomGroupChatTitle() {
return groupChatTitles.get(rnd.nextInt(groupChatTitles.size()));
}

static String getRandomName() {
return names.get(rnd.nextInt(names.size()));
}

static String getRandomMessage() {
return messages.get(rnd.nextInt(messages.size()));
}

static String getRandomImage() {
return images.get(rnd.nextInt(images.size()));
}

static boolean getRandomBoolean() {
return rnd.nextBoolean();
}
}
17 changes: 16 additions & 1 deletion app/src/main/java/io/mycoach/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.stfalcon.chatkit.commons.models.IUser;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -116,5 +117,19 @@ public void setWorkouts(Map<String, String> workouts) {
this.workouts = workouts;
}


@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", email='" + email + '\'' +
", name='" + name + '\'' +
", password='" + password + '\'' +
", avatar='" + avatar + '\'' +
", type_workout='" + type_workout + '\'' +
", weekly_days=" + Arrays.toString(weekly_days) +
", weekly_workout='" + weekly_workout + '\'' +
", workouts=" + workouts +
", isNew=" + isNew +
'}';
}
}
60 changes: 53 additions & 7 deletions app/src/main/java/io/mycoach/repository/AuthRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,23 @@ public class AuthRepository {
private static FirebaseFirestore db = FirebaseFirestore.getInstance();
private static CollectionReference usersRef = db.collection("users");


/**
* Find a user by email
* @param email
*/
public static void find(String email) {
public static LiveData<User> find(String email) {
MutableLiveData<User> userLive = new MutableLiveData<User>();

DocumentReference uidRef = usersRef.document(email);
uidRef.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if(document.exists()) {
document.getData();
Log.d(TAG, "User : " + document.toObject(User.class));
User user = document.toObject(User.class);
userLive.setValue(user);
Log.d(TAG, "User : " + user);
}
else {
Log.d(TAG, "no such user");
Expand All @@ -55,6 +59,8 @@ public static void find(String email) {
Log.d(TAG, "get failed with ", task.getException());
}
});

return userLive;
}

/**
Expand Down Expand Up @@ -84,13 +90,48 @@ public void onComplete(@NonNull Task<AuthResult> task) {
return status;
}

/**
* Update a user
* @param user
* @return
*/
public static LiveData<Boolean> update(User user) {
MutableLiveData<Boolean> status = new MutableLiveData<>(false);

DocumentReference uidRef = usersRef.document(user.getEmail());
uidRef.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if(document.exists()) {
document.getData();
uidRef.set(user).addOnCompleteListener(userCreationTask -> {
if (userCreationTask.isSuccessful()) {
Log.e(TAG, "updatedUser:ok");
status.setValue(true);
} else {
Log.e(TAG, userCreationTask.getException().getMessage());
}
});

Log.d(TAG, "update user : " + document.toObject(User.class));
}
else {
Log.d(TAG, "no such user to update");
}
} else {
Log.d(TAG, "update failed with ", task.getException());
}
});

return status;
}

/**
* Create a user
* @param user
* @return boolean to indicate if user is created or not
*/
public static LiveData<Boolean> save(User user) {
public static LiveData<Boolean> create(User user) {
MutableLiveData<Boolean> status = new MutableLiveData<>(false);

if(user == null || Strings.isNullOrEmpty(user.getEmail()) || Strings.isNullOrEmpty(user.getPassword())) {
Expand All @@ -103,14 +144,15 @@ public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Log.d(TAG, "createUserWithEmail:success");
user.setId(auth.getCurrentUser().getUid());
user.setNew(true);

// update display name for auth user
updateAuthDisplayName(user.getName());

// save user in firestore database
saveInFireStore(user);

status.setValue(true);
saveInFireStore(user).observeForever( stat -> {
if(stat) status.setValue(true);
});
}
else {
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Expand Down Expand Up @@ -166,7 +208,8 @@ public static void logout() {
auth.signOut();
}

private static void saveInFireStore(User user) {
private static LiveData<Boolean> saveInFireStore(User user) {
MutableLiveData<Boolean> status = new MutableLiveData<>(false);

DocumentReference uidRef = usersRef.document(user.getEmail());
uidRef.get().addOnCompleteListener(uidTask -> {
Expand All @@ -178,6 +221,7 @@ private static void saveInFireStore(User user) {
uidRef.set(user).addOnCompleteListener(userCreationTask -> {
if (userCreationTask.isSuccessful()) {
Log.e(TAG, "createdUserInFireStore:ok");
status.setValue(true);
} else {
Log.e(TAG, userCreationTask.getException().getMessage());
}
Expand All @@ -189,6 +233,8 @@ private static void saveInFireStore(User user) {
Log.e(TAG, uidTask.getException().getMessage());
}
});

return status;
}

private static void updateAuthDisplayName(String name) {
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/io/mycoach/ui/auth/RegisterViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ private void createUser(User user) {
public void onChanged(Boolean status) {
if(status)
registerState.setValue(RegisterState.REGISTERED_SUCCESS);
else
registerState.setValue(RegisterState.ALREADY_EXISTS);
// else
// registerState.setValue(RegisterState.ALREADY_EXISTS);
}
};

AuthRepository.save(user).observeForever(observer);
AuthRepository.create(user).observeForever(observer);
}

@Override
protected void onCleared() {
super.onCleared();
AuthRepository.save(user).removeObserver(observer);
AuthRepository.create(user).removeObserver(observer);
}
}
Loading

0 comments on commit 8d885fc

Please sign in to comment.