diff --git a/app/src/main/java/com/dowerx/quack/ProfileFeedFragment.java b/app/src/main/java/com/dowerx/quack/ProfileFeedFragment.java new file mode 100644 index 0000000..b804521 --- /dev/null +++ b/app/src/main/java/com/dowerx/quack/ProfileFeedFragment.java @@ -0,0 +1,85 @@ +package com.dowerx.quack; + +import android.content.Context; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.loader.app.LoaderManager; +import androidx.loader.content.Loader; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.dowerx.quack.model.Post; +import com.dowerx.quack.service.PostService; + +import java.util.List; + +public class ProfileFeedFragment extends Fragment implements LoaderManager.LoaderCallbacks> { + + private RecyclerView recyclerView; + private String username; + private final static String USERNAME = "username"; + + + public ProfileFeedFragment() { + } + + @SuppressWarnings("unused") + public static ProfileFeedFragment newInstance(String username) { + ProfileFeedFragment fragment = new ProfileFeedFragment(); + Bundle args = new Bundle(); + args.putString(USERNAME, username); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (getArguments() != null) { + this.username = getArguments().getString(USERNAME); + } + + LoaderManager.getInstance(this).initLoader(1, null, this); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_feed_list, container, false); + + if (view instanceof RecyclerView) { + Context context = view.getContext(); + recyclerView = (RecyclerView) view; + recyclerView.setLayoutManager(new LinearLayoutManager(context)); + } + return view; + } + + @NonNull + @Override + public Loader> onCreateLoader(int id, @Nullable Bundle args) { + Log.d(this.getClass().getName(), "onCreateLoader"); + return new PostService.GetProfileFeed(getContext(), this.username); + } + + @Override + public void onLoadFinished(@NonNull Loader> loader, List data) { + Log.d(this.getClass().getName(), "onLoadFinished"); + Log.d(this.getClass().getName(), data.toString()); + recyclerView.setAdapter(new PostRecyclerViewAdapter(data)); + } + + @Override + public void onLoaderReset(@NonNull Loader loader) { + + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/dowerx/quack/ProfileFragment.java b/app/src/main/java/com/dowerx/quack/ProfileFragment.java index ca2a103..02fca0f 100644 --- a/app/src/main/java/com/dowerx/quack/ProfileFragment.java +++ b/app/src/main/java/com/dowerx/quack/ProfileFragment.java @@ -4,24 +4,34 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.loader.app.LoaderManager; +import androidx.loader.content.Loader; + +import com.dowerx.quack.model.User; +import com.dowerx.quack.service.AuthService; +import com.dowerx.quack.service.UserService; -public class ProfileFragment extends Fragment { +public class ProfileFragment extends Fragment implements LoaderManager.LoaderCallbacks { - private static final String ARG_PARAM1 = "userid"; + private static final String ID = "id"; - private String userID; + private String id; + private View view; public ProfileFragment() { // Required empty public constructor } - public static ProfileFragment newInstance(String param1) { + public static ProfileFragment newInstance(String id) { ProfileFragment fragment = new ProfileFragment(); Bundle args = new Bundle(); - args.putString(ARG_PARAM1, param1); + args.putString(ID, id); fragment.setArguments(args); return fragment; } @@ -30,19 +40,39 @@ public class ProfileFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { - userID = getArguments().getString(ARG_PARAM1); + id = getArguments().getString(ID); + } else { + id = AuthService.getId(); } + + LoaderManager.getInstance(this).initLoader(1, null, this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - View view = inflater.inflate(R.layout.fragment_profile, container, false); - getParentFragmentManager() - .beginTransaction() - .replace(R.id.profile_posts_holder, new FeedFragment()) - .commit(); + view = inflater.inflate(R.layout.fragment_profile, container, false); return view; } + + @NonNull + @Override + public Loader onCreateLoader(int id, @Nullable Bundle args) { + return new UserService.GetUserById(getContext(), this.id); + } + + @Override + public void onLoadFinished(@NonNull Loader loader, User data) { + ((TextView)view.findViewById(R.id.profile_username)).setText(data.getUsername()); + getParentFragmentManager() + .beginTransaction() + .replace(R.id.profile_posts_holder, ProfileFeedFragment.newInstance(data.getUsername())) + .commit(); + } + + @Override + public void onLoaderReset(@NonNull Loader loader) { + + } } \ No newline at end of file diff --git a/app/src/main/java/com/dowerx/quack/service/AuthService.java b/app/src/main/java/com/dowerx/quack/service/AuthService.java index 6d3c720..6589811 100644 --- a/app/src/main/java/com/dowerx/quack/service/AuthService.java +++ b/app/src/main/java/com/dowerx/quack/service/AuthService.java @@ -16,6 +16,12 @@ import java.util.HashMap; import java.util.concurrent.ExecutionException; public class AuthService { + public static String getId() { + return id; + } + + private static String id; + // email, password public static class Login extends AsyncTask { Activity activity; @@ -28,7 +34,8 @@ public class AuthService { protected Boolean doInBackground(String... loginDetails) { FirebaseAuth fba = FirebaseAuth.getInstance(); try { - Tasks.await(fba.signInWithEmailAndPassword(loginDetails[0], loginDetails[1])); + AuthResult result = Tasks.await(fba.signInWithEmailAndPassword(loginDetails[0], loginDetails[1])); + id = result.getUser().getUid(); return true; } catch (ExecutionException | InterruptedException e) { return false; @@ -68,6 +75,7 @@ public class AuthService { data.put("email", loginDetails[0]); data.put("username", loginDetails[2]); data.put("picture", "/assets/placeholder-profile-picture.png"); + data.put("id", result.getUser().getUid()); ffs.collection("user").document(loginDetails[2]).set(data); diff --git a/app/src/main/java/com/dowerx/quack/service/PostService.java b/app/src/main/java/com/dowerx/quack/service/PostService.java index 541e44d..38d3579 100644 --- a/app/src/main/java/com/dowerx/quack/service/PostService.java +++ b/app/src/main/java/com/dowerx/quack/service/PostService.java @@ -14,6 +14,7 @@ import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.DocumentSnapshot; +import com.google.firebase.firestore.Filter; import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.Query; import com.google.firebase.firestore.QueryDocumentSnapshot; @@ -42,9 +43,54 @@ public class PostService { for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post").limit(10).get())) { DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get()); String username = user.get("username", String.class); - String userid = user.get("id", String.class); + String email = user.get("email", String.class); + String id = user.get("id", String.class); posts.add(new Post( - new User(username, "", userid), + new User(username, email, id), + post.get("content", String.class), + post.get("image", String.class), + 0, + false, + post.getId() + )); + } + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + + return posts; + } + + @Override + protected void onStartLoading() { + forceLoad(); + } + } + + public static class GetProfileFeed extends AsyncTaskLoader> { + + private final String username; + + public GetProfileFeed(@NonNull Context context, String username) { + super(context); + this.username = username; + } + + @Nullable + @Override + public List loadInBackground() { + List posts = new LinkedList<>(); + + FirebaseFirestore db = FirebaseFirestore.getInstance(); + try { + for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post") + .where(Filter.equalTo("user", db.document("/user/"+username))).get())) { + DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get()); + String username = user.get("username", String.class); + String email = user.get("email", String.class); + String id = user.get("id", String.class); + posts.add(new Post( + new User(username, email, id), post.get("content", String.class), post.get("image", String.class), 0, diff --git a/app/src/main/java/com/dowerx/quack/service/UserService.java b/app/src/main/java/com/dowerx/quack/service/UserService.java new file mode 100644 index 0000000..0560d04 --- /dev/null +++ b/app/src/main/java/com/dowerx/quack/service/UserService.java @@ -0,0 +1,81 @@ +package com.dowerx.quack.service; + +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.loader.content.AsyncTaskLoader; + +import com.dowerx.quack.model.Post; +import com.dowerx.quack.model.User; +import com.google.android.gms.tasks.Tasks; +import com.google.firebase.firestore.DocumentSnapshot; +import com.google.firebase.firestore.Filter; +import com.google.firebase.firestore.FirebaseFirestore; +import com.google.firebase.firestore.QueryDocumentSnapshot; +import com.google.firebase.firestore.QuerySnapshot; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.ExecutionException; + +public class UserService { + public static class GetUserByUsername extends AsyncTaskLoader { + + private final String username; + + public GetUserByUsername(@NonNull Context context, String username) { + super(context); + this.username = username; + } + + @Nullable + @Override + public User loadInBackground() { + FirebaseFirestore db = FirebaseFirestore.getInstance(); + try { + DocumentSnapshot user = Tasks.await(db.document("/user/" + username).get()); + return new User( + user.get("username", String.class), + user.get("email", String.class), + user.get("id", String.class)); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void onStartLoading() { + forceLoad(); + } + } + public static class GetUserById extends AsyncTaskLoader { + + private final String id; + + public GetUserById(@NonNull Context context, String id) { + super(context); + this.id = id; + } + + @Nullable + @Override + public User loadInBackground() { + FirebaseFirestore db = FirebaseFirestore.getInstance(); + try { + DocumentSnapshot user = Tasks.await(db.collection("user").where(Filter.equalTo("id", id)).get()).getDocuments().get(0); + return new User( + user.get("username", String.class), + user.get("email", String.class), + user.get("id", String.class)); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void onStartLoading() { + forceLoad(); + } + } +}