Skip to content

Commit

Permalink
get recipient info in chat fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
varunon9 committed Nov 1, 2018
1 parent 73ae802 commit 4b8556a
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

Expand All @@ -22,8 +20,9 @@ public class ChatFragmentActivity extends AppCompatActivity {
private String TAG = "ChatFragmentActivity";
private ProgressDialog progressDialog;
public FirestoreDbUtility firestoreDbUtility;
public String userUid;
public String travellerUserUid;
public String chatInitiatorUid;
public String chatRecipientUid;
public String chatInitiatorName;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -40,8 +39,9 @@ protected void onCreate(Bundle savedInstanceState) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
int navigationLink = bundle.getInt(AppConstants.NAVIGATION_ITEM);
userUid = bundle.getString(AppConstants.USER_UID);
travellerUserUid = bundle.getString(AppConstants.TRAVELLER_USER_UID);
chatInitiatorUid = bundle.getString(AppConstants.CHAT_INITIATOR_UID);
chatRecipientUid = bundle.getString(AppConstants.CHAT_RECIPIENT_UID);
chatInitiatorName = bundle.getString(AppConstants.CHAT_INITIATOR_NAME);
Fragment fragment = getSelectedFragment(navigationLink);
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
Expand Down
19 changes: 13 additions & 6 deletions app/src/main/java/me/varunon9/saathmetravel/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ public void onBackPressed() {
drawer.closeDrawer(GravityCompat.START);
} else {
if (doubleBackToExitPressedOnce) {
// todo: set isOnline false, update lastSeen if user is loggedIn
// updating last seen and isOnline
if (singleton.getFirebaseUser() != null) {
generalUtility.setUserLastSeenStatus(firestoreDbUtility,
singleton.getFirebaseUser().getUid());
}
super.onBackPressed();
return;
}
Expand Down Expand Up @@ -168,7 +172,7 @@ public boolean onNavigationItemSelected(MenuItem item) {
} else if (id == R.id.nav_profile) {
Bundle args = new Bundle();
args.putInt(AppConstants.NAVIGATION_ITEM, id);
args.putString(AppConstants.TRAVELLER_USER_UID, singleton.getFirebaseUser().getUid());
args.putString(AppConstants.CHAT_RECIPIENT_UID, singleton.getFirebaseUser().getUid());
goToChatFragmentActivity(args);
} else if (id == R.id.nav_chats) {
Bundle args = new Bundle();
Expand Down Expand Up @@ -490,7 +494,7 @@ public boolean onMarkerClick(Marker marker) {
showMessage("You need to login to chat with traveller");
} else {
Bundle bundle = new Bundle();
bundle.putString(AppConstants.TRAVELLER_USER_UID, user.getUid());
bundle.putString(AppConstants.CHAT_RECIPIENT_UID, user.getUid());
bundle.putInt(AppConstants.NAVIGATION_ITEM, R.id.nav_profile);
goToChatFragmentActivity(bundle);
}
Expand Down Expand Up @@ -531,9 +535,11 @@ private void firebaseLogout() {
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
singleton.setFirebaseUser(null);
// updating isOnline and lastSeen
generalUtility.setUserLastSeenStatus(firestoreDbUtility,
singleton.getFirebaseUser().getUid());

// todo: update isOnline and lastSeen
singleton.setFirebaseUser(null);
refreshMainActivity();
}
});
Expand All @@ -547,7 +553,8 @@ private void refreshMainActivity() {
}

private void goToChatFragmentActivity(Bundle bundle) {
bundle.putString(AppConstants.USER_UID, singleton.getFirebaseUser().getUid());
bundle.putString(AppConstants.CHAT_INITIATOR_UID, singleton.getFirebaseUser().getUid());
bundle.putString(AppConstants.CHAT_INITIATOR_NAME, singleton.getFirebaseUser().getDisplayName());
Intent intent = new Intent(MainActivity.this, ChatFragmentActivity.class);
intent.putExtras(bundle);
startActivity(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ private Collections() {}
public static final int DEFAULT_RANGE = 50; // 50 KM

public static final String NAVIGATION_ITEM = "navigationItem";
public static final String USER_UID = "userUid";
public static final String TRAVELLER_USER_UID = "travellerUserUid";
public static final String CHAT_INITIATOR_UID = "chatInitiatorUid";
public static final String CHAT_RECIPIENT_UID = "chatRecipientUid";
public static final String CHAT_INITIATOR_NAME = "chatInitiatorName";
public static class ChatFragmentActivityTitle {
private ChatFragmentActivityTitle() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import android.view.View;
import android.view.ViewGroup;

import com.google.firebase.firestore.DocumentSnapshot;

import me.varunon9.saathmetravel.ChatFragmentActivity;
import me.varunon9.saathmetravel.R;
import me.varunon9.saathmetravel.constants.AppConstants;
import me.varunon9.saathmetravel.models.Chat;
import me.varunon9.saathmetravel.models.User;
import me.varunon9.saathmetravel.utils.FirestoreDbOperationCallback;

public class ChatFragment extends Fragment {

Expand All @@ -32,13 +37,36 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
chatViewModel = ViewModelProviders.of(getActivity()).get(ChatViewModel.class);
chatViewModel.getSelectedChat().observe(this, chat -> {
setChatDetails(chat);
chatFragmentActivity.updateActionBarTitle(chat.getRecipientName());
getRecipientProfileFromFirestore(chat.getRecipientUid());
// todo: update messages
});
}

private void setChatDetails(Chat chat) {
chatFragmentActivity.updateActionBarTitle(chat.getRecipientName());
// todo: update UI
private void getRecipientProfileFromFirestore(String recipientUid) {
chatFragmentActivity.showProgressDialog("Fetching Recipient info",
"Please wait", false);
chatFragmentActivity.firestoreDbUtility.getOne(AppConstants.Collections.USERS,
recipientUid, new FirestoreDbOperationCallback() {
@Override
public void onSuccess(Object object) {
DocumentSnapshot documentSnapshot = (DocumentSnapshot) object;
User recipientUser = documentSnapshot.toObject(User.class);
chatFragmentActivity.dismissProgressDialog();

updateLastSeen(recipientUser);
}

@Override
public void onFailure(Object object) {
chatFragmentActivity.dismissProgressDialog();
chatFragmentActivity.showMessage("Failed to fetch recipient info");
}
});
}

private void updateLastSeen(User recipientUser) {
// todo: update last seen
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void getChatListFromFirestore() {
firestoreQueryList.add(new FirestoreQuery(
FirestoreQueryConditionCode.WHERE_ARRAY_CONTAINS,
"participantsUid",
chatFragmentActivity.userUid
chatFragmentActivity.chatInitiatorUid
));

chatFragmentActivity.firestoreDbUtility.getMany(AppConstants.Collections.CHATS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@

import com.google.firebase.firestore.DocumentSnapshot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.varunon9.saathmetravel.ChatFragmentActivity;
import me.varunon9.saathmetravel.R;
import me.varunon9.saathmetravel.constants.AppConstants;
import me.varunon9.saathmetravel.models.Chat;
import me.varunon9.saathmetravel.models.User;
import me.varunon9.saathmetravel.utils.FirestoreDbOperationCallback;

public class ProfileFragment extends Fragment implements View.OnClickListener {

private ProfileViewModel profileViewModel;
private ChatFragmentActivity chatFragmentActivity;
private ChatViewModel chatViewModel;
private EditText nameEditText;
private EditText preferenceEditText;
private Button updateProfileButton;
private Button chatWithTravellerButton;
private RadioGroup genderRadioGroup;
private RadioButton maleRadioButton;
private RadioButton femaleRadioButton;
private User currentUser;
private String TAG = "ProfileFragment";

@Override
Expand All @@ -60,21 +64,20 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
profileViewModel = ViewModelProviders.of(getActivity()).get(ProfileViewModel.class);
chatViewModel = ViewModelProviders.of(this.getActivity()).get(ChatViewModel.class);
getTravellerProfileFromFirestore();
}

private void getTravellerProfileFromFirestore() {
chatFragmentActivity.showProgressDialog("Fetching Traveller info",
"Please wait", false);
chatFragmentActivity.firestoreDbUtility.getOne(AppConstants.Collections.USERS,
chatFragmentActivity.travellerUserUid, new FirestoreDbOperationCallback() {
chatFragmentActivity.chatRecipientUid, new FirestoreDbOperationCallback() {
@Override
public void onSuccess(Object object) {
DocumentSnapshot documentSnapshot = (DocumentSnapshot) object;
User user = documentSnapshot.toObject(User.class);
profileViewModel.setSelectedTraveller(user);
setProfileDetails(user);
currentUser = documentSnapshot.toObject(User.class);
setProfileDetails(currentUser);
chatFragmentActivity.dismissProgressDialog();
}

Expand All @@ -95,7 +98,7 @@ private void setProfileDetails(User user) {
femaleRadioButton.setChecked(true);
}

if (!chatFragmentActivity.userUid.equals(chatFragmentActivity.travellerUserUid)) {
if (!chatFragmentActivity.chatInitiatorUid.equals(chatFragmentActivity.chatRecipientUid)) {
updateProfileButton.setVisibility(View.INVISIBLE);

// disabling form fields
Expand All @@ -111,7 +114,7 @@ public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.updateProfileButton: {
if (chatFragmentActivity.userUid.equals(chatFragmentActivity.travellerUserUid)) {
if (chatFragmentActivity.chatInitiatorUid.equals(chatFragmentActivity.chatRecipientUid)) {
String name = nameEditText.getText().toString();
String preference = preferenceEditText.getText().toString();
String gender = AppConstants.Gender.MALE;
Expand All @@ -135,6 +138,19 @@ public void onClick(View view) {
}

case R.id.chatWithTravellerButton: {
Chat chat = new Chat();
chat.setInitiatorUid(chatFragmentActivity.chatInitiatorUid);
chat.setInitiatorName(chatFragmentActivity.chatInitiatorName);
chat.setRecipientName(currentUser.getName());
chat.setRecipientUid(currentUser.getUid());

List<String> participants = new ArrayList<>();
participants.add(chatFragmentActivity.chatInitiatorUid);
participants.add(currentUser.getUid());
chat.setParticipantsUid(participants);

chatViewModel.setSelectedChat(chat);
chatFragmentActivity.goToChatFragment();
break;
}
}
Expand All @@ -145,7 +161,7 @@ private void updateProfile(Map<String, Object> hashMap) {
chatFragmentActivity.showProgressDialog("Updating profile",
"Please wait", false);
chatFragmentActivity.firestoreDbUtility.update(AppConstants.Collections.USERS,
chatFragmentActivity.userUid,
chatFragmentActivity.chatInitiatorUid,
hashMap, new FirestoreDbOperationCallback() {
@Override
public void onSuccess(Object object) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import com.google.firebase.firestore.GeoPoint;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import me.varunon9.saathmetravel.R;
import me.varunon9.saathmetravel.constants.AppConstants;
import me.varunon9.saathmetravel.models.User;
Expand Down Expand Up @@ -133,4 +137,28 @@ private void setTravellerMarkerOnMap(GoogleMap googleMap, GeoPoint geoPoint, Use
);
marker.setTag(user);
}

private void silentlyUpdateUserProfile(FirestoreDbUtility firestoreDbUtility,
Map<String, Object> hashMap,String userUid) {
firestoreDbUtility.update(AppConstants.Collections.USERS,
userUid,
hashMap, new FirestoreDbOperationCallback() {
@Override
public void onSuccess(Object object) {
}

@Override
public void onFailure(Object object) {
}
});
}

public void setUserLastSeenStatus(FirestoreDbUtility firestoreDbUtility,
String userUid) {
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("online", false);
hashMap.put("lastSeen", new Date());

silentlyUpdateUserProfile(firestoreDbUtility, hashMap, userUid);
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/content_journey_planner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
android:textColor="@color/colorWhite"
android:text="@string/search_travellers"
android:drawableLeft="@drawable/ic_search_travellers"
android:padding="12dp"
android:onClick="onSearchTravellersButtonClicked"/>

<TextView
Expand Down

0 comments on commit 4b8556a

Please sign in to comment.