Membuat Aplikasi Chatting Part 4 - Menambahkan Teman
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_baseline_add_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
dialog_add.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edtID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserID" />
<Button
android:id="@+id/btnOk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseFirestore db;
private FloatingActionButton fabAdd;
private String uid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
db = FirebaseFirestore.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
Intent i = new Intent(MainActivity.this,LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
} else {
goMain();
}
}
};
}
private void goMain() {
FirebaseUser currentUser = mAuth.getCurrentUser();
uid = currentUser.getUid();
fabAdd = findViewById(R.id.fabAdd);
fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setTitle("Enter User UID");
dialog.setContentView(R.layout.dialog_add);
dialog.show();
final EditText edtID = dialog.findViewById(R.id.edtID);
Button btnOk = dialog.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String idUser = edtID.getText().toString();
if (TextUtils.isEmpty(idUser)) {
edtID.setError("required");
} else {
db.collection("user").whereEqualTo("id",idUser)
.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if(queryDocumentSnapshots.isEmpty()) {
edtID.setError("ID not found");
} else {
for (DocumentSnapshot documentSnapshot : queryDocumentSnapshots.getDocuments()) {
String uidFriend = documentSnapshot.getId();
if (uid.equals(uidFriend)) {
edtID.setError("wrong ID");
} else {
dialog.cancel();
checkFriendExist(uidFriend);
}
}
}
}
});
}
}
});
}
});
}
private void checkFriendExist(final String uidFriend) {
db.collection("user").document(uid).collection("friend").document().get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
String idChatRoom = documentSnapshot.get("idChatRoom", String.class);
goChatRoom(idChatRoom,uidFriend);
} else {
createNewChatRoom(uidFriend);
}
}
}
});
}
private void createNewChatRoom(final String uidFriend) {
HashMap<String,Object> dataChatRoom = new HashMap<>();
dataChatRoom.put("dateAdded", FieldValue.serverTimestamp());
db.collection("chatRoom").document(uid+uidFriend).set(dataChatRoom).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//write user data
HashMap<String,Object> dataFriend = new HashMap<>();
dataFriend.put("idChatRoom",uid+uidFriend);
db.collection("user").document(uid).collection("friend").document(uidFriend).set(dataFriend).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//write on user's friend data
HashMap<String,Object> dataUserFriend = new HashMap<>();
dataUserFriend.put("idChatRoom",uid+uidFriend);
db.collection("user").document(uidFriend).collection("friend").document(uid).set(dataUserFriend).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
goChatRoom(uid+uidFriend,uidFriend);
}
});
}
});
}
});
}
private void goChatRoom(String idChatRoom, String uidFriend) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
FirebaseAuth.getInstance().signOut();
Intent i = new Intent(MainActivity.this,LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
} else if (id == R.id.action_profile) {
Intent i = new Intent(MainActivity.this,ProfileActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onStop() {
super.onStop();
mAuth.removeAuthStateListener(mAuthListener);
}
}
Video tutorial
Comments
Post a Comment