瀏覽代碼

profile user

sneiasckin 5 年之前
父節點
當前提交
9f666ee162

+ 4 - 1
app/src/main/AndroidManifest.xml

@@ -2,13 +2,16 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.chatapp">
 
+    <uses-permission android:name="android.permission.INTERNET"/>
+
     <application
         android:allowBackup="true"
         android:icon="@drawable/mailicon"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
-        android:theme="@style/AppTheme">
+        android:theme="@style/AppTheme"
+        android:usesCleartextTraffic="true">
         <activity android:name=".MessageActivity"></activity>
         <activity
             android:name=".LoginActivity"

+ 3 - 6
app/src/main/java/com/example/chatapp/MainActivity.java

@@ -17,6 +17,7 @@ import android.widget.TextView;
 
 import com.bumptech.glide.Glide;
 import com.example.chatapp.fragments.ChatsFragment;
+import com.example.chatapp.fragments.ProfileFragment;
 import com.example.chatapp.fragments.UsersFragment;
 import com.example.chatapp.model.User;
 import com.google.android.material.tabs.TabLayout;
@@ -48,7 +49,7 @@ public class MainActivity extends AppCompatActivity {
 
         Toolbar toolbar = findViewById(R.id.toolbarMain);
         setSupportActionBar(toolbar);
-        getSupportActionBar().setTitle("");
+        getSupportActionBar().setTitle("Messages");
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 
         fetchCurrentUser();
@@ -62,6 +63,7 @@ public class MainActivity extends AppCompatActivity {
         ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
         viewPagerAdapter.addFragment(new ChatsFragment(), "Chats");
         viewPagerAdapter.addFragment(new UsersFragment(), "Users");
+        viewPagerAdapter.addFragment(new ProfileFragment(), "Profile");
 
         viewPager.setAdapter(viewPagerAdapter);
         tabLayout.setupWithViewPager(viewPager);
@@ -77,11 +79,6 @@ public class MainActivity extends AppCompatActivity {
                 User user = dataSnapshot.getValue(User.class);
                 usernameOutput.setText(user.getUsername());
                 Picasso.get().load(user.getImageURL()).into(imageProfileOutput);
-//                if (user.getImageURL().equals("default")) {
-//                    imageProfileOutput.setImageResource(R.mipmap.ic_launcher);
-//                } else {
-//                    Glide.with(getApplicationContext()).load(user.getImageURL()).into(imageProfileOutput);
-//                }
             }
 
             @Override

+ 104 - 2
app/src/main/java/com/example/chatapp/fragments/ChatsFragment.java

@@ -1,22 +1,124 @@
 package com.example.chatapp.fragments;
 
 import android.os.Bundle;
+import android.provider.ContactsContract;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
+import androidx.annotation.NonNull;
 import androidx.fragment.app.Fragment;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
 
 import com.example.chatapp.R;
+import com.example.chatapp.adapter.UserAdapter;
+import com.example.chatapp.model.Chat;
+import com.example.chatapp.model.User;
+import com.google.firebase.auth.FirebaseAuth;
+import com.google.firebase.auth.FirebaseUser;
+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.database.ValueEventListener;
+
+import java.util.ArrayList;
+import java.util.List;
 
 
 public class ChatsFragment extends Fragment {
 
+    private RecyclerView chatsListRecyclerView;
+    private UserAdapter userAdapter;
+    private List<User> mUsers;
+    FirebaseUser firebaseUser;
+    DatabaseReference databaseReference;
+    private List<String> usersList;
+
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
-        // Inflate the layout for this fragment
-        return inflater.inflate(R.layout.fragment_chats, container, false);
+        View view = inflater.inflate(R.layout.fragment_chats, container, false);
+
+        chatsListRecyclerView = view.findViewById(R.id.chatsListRecyclerView);
+        chatsListRecyclerView.setHasFixedSize(true);
+        chatsListRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
+
+        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
+
+        usersList = new ArrayList<>();
+
+        databaseReference = FirebaseDatabase.getInstance().getReference("Chats");
+        databaseReference.addValueEventListener(new ValueEventListener() {
+            @Override
+            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
+                usersList.clear();
+
+                for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
+                    Chat chat = snapshot.getValue(Chat.class);
+                    if (chat.getSender().equals(firebaseUser.getUid())) {
+                        usersList.add(chat.getReceiver());
+                    }
+                    if (chat.getReceiver().equals(firebaseUser.getUid())) {
+                        usersList.add(chat.getSender());
+                    }
+                }
+
+                readChats();
+            }
+
+            @Override
+            public void onCancelled(@NonNull DatabaseError databaseError) {
+
+            }
+        });
+
+        return view;
+    }
+
+    private void readChats() {
+
+        mUsers = new ArrayList<>();
+
+        databaseReference = FirebaseDatabase.getInstance().getReference("Users");
+        databaseReference.addValueEventListener(new ValueEventListener() {
+            @Override
+            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
+                mUsers.clear();
+
+                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
+                    User user = snapshot.getValue(User.class);
+
+                    for (String id : usersList) {
+                        if (user.getId().equals(id)) {
+                            if (mUsers.size() != 0) {
+                                for (User user1 : mUsers) {
+                                    Log.d("LOOOOOOOOOOOOOOOOOOOOL", user1.getId());
+
+                                    if (!user.getId().equals(user1.getId())) {
+                                        mUsers.add(user);
+                                    }
+                                }
+                            } else {
+                                mUsers.add(user);
+                            }
+                        }
+                    }
+                }
+
+                userAdapter = new UserAdapter(getContext(), mUsers);
+                chatsListRecyclerView.setAdapter(userAdapter);
+
+            }
+
+            @Override
+            public void onCancelled(@NonNull DatabaseError databaseError) {
+
+            }
+        });
+
     }
 
 }

+ 63 - 0
app/src/main/java/com/example/chatapp/fragments/ProfileFragment.java

@@ -0,0 +1,63 @@
+package com.example.chatapp.fragments;
+
+import android.content.Context;
+import android.net.Uri;
+import android.os.Bundle;
+
+import androidx.annotation.NonNull;
+import androidx.fragment.app.Fragment;
+
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import com.example.chatapp.R;
+import com.example.chatapp.model.User;
+import com.google.firebase.auth.FirebaseAuth;
+import com.google.firebase.auth.FirebaseUser;
+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.database.ValueEventListener;
+import com.squareup.picasso.Picasso;
+
+import de.hdodenhof.circleimageview.CircleImageView;
+
+public class ProfileFragment extends Fragment {
+
+    private CircleImageView profilePhotoImage;
+    private TextView usernameProfileOutput;
+
+    DatabaseReference reference;
+    FirebaseUser firebaseUser;
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        View view = inflater.inflate(R.layout.fragment_profile, container, false);
+
+        profilePhotoImage = view.findViewById(R.id.profilePhotoCircleInProfile);
+        usernameProfileOutput = view.findViewById(R.id.usernameProfileOutput);
+
+        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
+        reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
+        reference.addValueEventListener(new ValueEventListener() {
+            @Override
+            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
+                User user = dataSnapshot.getValue(User.class);
+                usernameProfileOutput.setText(user.getUsername());
+                Picasso.get().load(user.getImageURL()).into(profilePhotoImage);
+            }
+
+            @Override
+            public void onCancelled(@NonNull DatabaseError databaseError) {
+
+            }
+        });
+
+        // Inflate the layout for this fragment
+        return view;
+    }
+}

+ 3 - 1
app/src/main/res/layout/activity_main.xml

@@ -9,7 +9,9 @@
 
     <include
         android:id="@+id/toolbarMain"
-        layout="@layout/toolbar_layout"/>
+        layout="@layout/toolbar_layout"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
 
     <View
         android:id="@+id/viewMain"

+ 5 - 6
app/src/main/res/layout/fragment_chats.xml

@@ -1,15 +1,14 @@
 <?xml version="1.0" encoding="utf-8"?>
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".fragments.ChatsFragment"
     android:background="#00CFCFCF">
 
-    <!-- TODO: Update blank fragment layout -->
-    <TextView
+    <androidx.recyclerview.widget.RecyclerView
+        android:id="@+id/chatsListRecyclerView"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:text="hello_blank_fragment" />
+        android:layout_height="match_parent"/>
 
-</FrameLayout>
+</RelativeLayout>

+ 50 - 0
app/src/main/res/layout/fragment_profile.xml

@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".fragments.ProfileFragment">
+
+    <androidx.cardview.widget.CardView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content" >
+
+        <RelativeLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:padding="8dp" >
+
+            <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Profile"
+                android:textColor="@android:color/holo_red_light"
+                android:textStyle="bold"/>
+
+            <de.hdodenhof.circleimageview.CircleImageView
+                android:layout_width="100dp"
+                android:layout_height="100dp"
+                android:id="@+id/profilePhotoCircleInProfile"
+                android:layout_centerHorizontal="true"
+                android:layout_marginTop="50dp"
+                android:src="@mipmap/ic_launcher"/>
+
+            <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Username"
+                android:layout_below="@+id/profilePhotoCircleInProfile"
+                android:layout_centerHorizontal="true"
+                android:layout_marginTop="15dp"
+                android:textColor="@android:color/holo_red_light"
+                android:textStyle="bold"
+                android:textSize="18sp"
+                android:id="@+id/usernameProfileOutput"/>
+
+
+        </RelativeLayout>
+
+    </androidx.cardview.widget.CardView>
+
+
+</RelativeLayout>