Skip to content

Commit

Permalink
Merge pull request #4 from AmirBabaei/Lito_returns
Browse files Browse the repository at this point in the history
Lito returns
  • Loading branch information
repineda authored May 26, 2019
2 parents e91c170 + 805320b commit 86b074d
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 39 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
Binary file modified .idea/caches/gradle_models.ser
Binary file not shown.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.schat"
minSdkVersion 16
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
Expand All @@ -26,6 +26,10 @@ dependencies {
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.google.firebase:firebase-auth:17.0.0'
implementation 'com.google.firebase:firebase-database:17.0.0'
implementation 'com.google.firebase:firebase-storage:17.0.0'

implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public int getItemCount() {

public class ChatListViewHolder extends RecyclerView.ViewHolder{
public TextView chatTitle;
LinearLayout layout;
public LinearLayout layout;
public ChatListViewHolder(View view){
super(view);
chatTitle = view.findViewById(R.id.chatTitle);
Expand Down
53 changes: 53 additions & 0 deletions app/src/main/java/com/example/schat/Chat/MediaAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.schat.Chat;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.schat.R;

import java.util.ArrayList;

public class MediaAdapter extends RecyclerView.Adapter<MediaAdapter.MediaViewHolder>{
ArrayList<String>mediaList;
Context context;
public MediaAdapter(Context context, ArrayList<String> mediaList){
this.context = context;
this.mediaList = mediaList;
}

@NonNull
@Override
public MediaViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View layoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_media, null, false);
MediaViewHolder mediaViewHolder = new MediaViewHolder(layoutView);
return mediaViewHolder;
}

@Override
public void onBindViewHolder(@NonNull MediaViewHolder mediaViewHolder, int i) {
Glide.with(context).load(Uri.parse(mediaList.get(i))).into(mediaViewHolder.media);
}

@Override
public int getItemCount() {
return mediaList.size();
}


public class MediaViewHolder extends RecyclerView.ViewHolder {
ImageView media;

public MediaViewHolder(View itemView) {
super(itemView);
media = itemView.findViewById(R.id.media);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.example.schat;
package com.example.schat.Chat;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;

import com.example.schat.R;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.schat;
package com.example.schat.Chat;

public class MessageObject
{
Expand Down
143 changes: 126 additions & 17 deletions app/src/main/java/com/example/schat/ChatActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.schat;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -10,35 +12,46 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.schat.Chat.ChatListAdapter;
import com.example.schat.Chat.ChatObject;
import com.example.schat.Chat.MediaAdapter;
import com.example.schat.Chat.MessageAdapter;
import com.example.schat.Chat.MessageObject;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

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

public class ChatActivity extends AppCompatActivity {
private RecyclerView messageListView;
private RecyclerView.Adapter messageAdapter;
private RecyclerView.LayoutManager messageListLayoutManager;
private RecyclerView messageListView, mediaListView;
private RecyclerView.Adapter messageAdapter, mediaAdapter;
private RecyclerView.LayoutManager messageListLayoutManager, mediaListLayourManager;

ArrayList<MessageObject> messageList;
String chatID;
DatabaseReference chatMessagesDb;

int PICK_IMAGE_INTENT = 1;
ArrayList<String> mediaUriList = new ArrayList<>();
ArrayList<String> mediaIdList = new ArrayList<>();
EditText mMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
chatID = getIntent().getExtras().getString("chatID");
chatMessagesDb = FirebaseDatabase.getInstance().getReference().child("chat").child(chatID);
Button addMedia = findViewById(R.id.addMedia);
Button send = findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -47,9 +60,20 @@ public void onClick(View v)
sendMessage();
}
});
initializeRecyclerView();
addMedia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
openGallery();
}


});
initializeMessage();
initializeMedia();
getChatMessages();
}

private void getChatMessages()
{
chatMessagesDb.addChildEventListener(new ChildEventListener()
Expand All @@ -67,7 +91,7 @@ public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s)

MessageObject mMessage = new MessageObject(dataSnapshot.getKey(), creatorID, text);
messageList.add(mMessage);
messageListLayoutManager.scrollToPosition(messageList.size()-1);
messageListLayoutManager.scrollToPosition(messageList.size()-1);//scrolls down to latest message
messageAdapter.notifyDataSetChanged();
}
}
Expand Down Expand Up @@ -95,20 +119,69 @@ public void onCancelled(@NonNull DatabaseError databaseError)
}
});
}
int mediaPosition = 0;
private void sendMessage()
{
EditText mMessage = findViewById(R.id.messageInput);
if(!mMessage.getText().toString().isEmpty()){
//DatabaseReference newMessageDb = FirebaseDatabase.getInstance().getReference().child("chat").child(chatID).push();
DatabaseReference newMessageDb = chatMessagesDb.push();
Map newMessageMap = new HashMap<>();
newMessageMap.put("text", mMessage.getText().toString());
mMessage = findViewById(R.id.messageInput);
//if(!mMessage.getText().toString().isEmpty()){
String messageId = chatMessagesDb.push().getKey();
final DatabaseReference newMessageDb = chatMessagesDb.child(messageId);
final Map newMessageMap = new HashMap<>();

newMessageMap.put("creator", FirebaseAuth.getInstance().getUid());
newMessageDb.updateChildren(newMessageMap);
}
if(!mMessage.getText().toString().isEmpty())
{
newMessageMap.put("text", mMessage.getText().toString());
}


if(!mediaUriList.isEmpty()){
for(String mediaUri : mediaUriList){
String mediaId = newMessageDb.child("media").push().getKey();
mediaIdList.add(mediaId);
final StorageReference filePath = FirebaseStorage.getInstance().getReference().child("chat").child(messageId).child(mediaId);

UploadTask uploadtask = filePath.putFile(Uri.parse(mediaUri));

uploadtask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
newMessageMap.put("/media/" + mediaIdList.get(mediaPosition) + "/", uri.toString());
mediaPosition++;
if(mediaPosition == mediaUriList.size())
{
updateDatabaseWithNewMessage(newMessageDb, newMessageMap);
}
}
});
}
});
}
}
else
{
if(!mMessage.getText().toString().isEmpty())
{
updateDatabaseWithNewMessage(newMessageDb, newMessageMap);
}
}

mMessage.setText(null);
}
private void initializeRecyclerView() {

private void updateDatabaseWithNewMessage(DatabaseReference dbReference, Map newMessageMap)
{
dbReference.updateChildren(newMessageMap);
mMessage.setText(null);
mediaIdList.clear();
mediaUriList.clear();
mediaAdapter.notifyDataSetChanged();
}

private void initializeMessage() {
messageList = new ArrayList<>();
messageListView = findViewById(R.id.messageList);
messageListView.setNestedScrollingEnabled(false);
Expand All @@ -118,5 +191,41 @@ private void initializeRecyclerView() {
messageAdapter = new MessageAdapter(messageList);
messageListView.setAdapter(messageAdapter);
}
private void initializeMedia() {
mediaUriList = new ArrayList<>();
mediaListView = findViewById(R.id.mediaList);
mediaListView.setNestedScrollingEnabled(false);
mediaListView.setHasFixedSize(false);
mediaListLayourManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.HORIZONTAL, false);
mediaListView.setLayoutManager(mediaListLayourManager);
mediaAdapter = new MediaAdapter(getApplicationContext(), mediaUriList);
mediaListView.setAdapter(mediaAdapter);
}
private void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "select Picture(s)"), PICK_IMAGE_INTENT );
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == PICK_IMAGE_INTENT){
if(data.getClipData() == null) {
mediaUriList.add(data.getData().toString());
}
else
{
for(int i = 0; i < data.getClipData().getItemCount(); i++){
mediaUriList.add(data.getClipData().getItemAt(i).getUri().toString());
}
}

mediaAdapter.notifyDataSetChanged();
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/example/schat/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUserDB.updateChildren(userMap);

}
userIsLoggedIn();
}

@Override
Expand All @@ -140,7 +141,6 @@ public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
// userIsLoggedIn(); This what the only thing in the above if initially. Not sure why.
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public void onCancelled(@NonNull DatabaseError databaseError) {
});

}

//Gets the digit that goes before a phone number based on your country for contacts that don't already have it
//This is because users in our database must add their country code to signup.
private String getCountryISO(){

String iso = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public UserListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i

@Override
public void onBindViewHolder(@NonNull UserListViewHolder userListViewHolder, final int position) {
userListViewHolder.usrPublicKey.setText("Usr Public Key"/*userList.get(i).getPublicKey()*/);// uid for now
userListViewHolder.usrPublicKey.setText("User Public Key"/*userList.get(i).getPublicKey()*/);// uid for now
userListViewHolder.usrName.setText(userList.get(position).getName());
userListViewHolder.usrPhone.setText(userList.get(position).getPhone());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.schat.Utils;

// We used this code from stack overflow
// No copyright, https://stackoverflow.com/questions/10772329/how-to-get-country-phone-prefix-from-iso
/*public class CountryToPhonePrefix {
}*/
import java.util.HashMap;
Expand Down
Loading

0 comments on commit 86b074d

Please sign in to comment.