profile page

This commit is contained in:
Benedek László 2024-05-05 17:50:04 +02:00
parent adc30fe90a
commit 505a27df08
5 changed files with 264 additions and 14 deletions

View File

@ -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<List<Post>> {
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<List<Post>> 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<List<Post>> loader, List<Post> 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) {
}
}

View File

@ -4,24 +4,34 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; 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<User> {
private static final String ARG_PARAM1 = "userid"; private static final String ID = "id";
private String userID; private String id;
private View view;
public ProfileFragment() { public ProfileFragment() {
// Required empty public constructor // Required empty public constructor
} }
public static ProfileFragment newInstance(String param1) { public static ProfileFragment newInstance(String id) {
ProfileFragment fragment = new ProfileFragment(); ProfileFragment fragment = new ProfileFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1); args.putString(ID, id);
fragment.setArguments(args); fragment.setArguments(args);
return fragment; return fragment;
} }
@ -30,19 +40,39 @@ public class ProfileFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (getArguments() != null) { if (getArguments() != null) {
userID = getArguments().getString(ARG_PARAM1); id = getArguments().getString(ID);
} else {
id = AuthService.getId();
} }
LoaderManager.getInstance(this).initLoader(1, null, this);
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_profile, container, false); view = inflater.inflate(R.layout.fragment_profile, container, false);
getParentFragmentManager()
.beginTransaction()
.replace(R.id.profile_posts_holder, new FeedFragment())
.commit();
return view; return view;
} }
@NonNull
@Override
public Loader<User> onCreateLoader(int id, @Nullable Bundle args) {
return new UserService.GetUserById(getContext(), this.id);
}
@Override
public void onLoadFinished(@NonNull Loader<User> 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<User> loader) {
}
} }

View File

@ -16,6 +16,12 @@ import java.util.HashMap;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
public class AuthService { public class AuthService {
public static String getId() {
return id;
}
private static String id;
// email, password // email, password
public static class Login extends AsyncTask<String, Void, Boolean> { public static class Login extends AsyncTask<String, Void, Boolean> {
Activity activity; Activity activity;
@ -28,7 +34,8 @@ public class AuthService {
protected Boolean doInBackground(String... loginDetails) { protected Boolean doInBackground(String... loginDetails) {
FirebaseAuth fba = FirebaseAuth.getInstance(); FirebaseAuth fba = FirebaseAuth.getInstance();
try { 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; return true;
} catch (ExecutionException | InterruptedException e) { } catch (ExecutionException | InterruptedException e) {
return false; return false;
@ -68,6 +75,7 @@ public class AuthService {
data.put("email", loginDetails[0]); data.put("email", loginDetails[0]);
data.put("username", loginDetails[2]); data.put("username", loginDetails[2]);
data.put("picture", "/assets/placeholder-profile-picture.png"); data.put("picture", "/assets/placeholder-profile-picture.png");
data.put("id", result.getUser().getUid());
ffs.collection("user").document(loginDetails[2]).set(data); ffs.collection("user").document(loginDetails[2]).set(data);

View File

@ -14,6 +14,7 @@ import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks; import com.google.android.gms.tasks.Tasks;
import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.Filter;
import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query; import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot; import com.google.firebase.firestore.QueryDocumentSnapshot;
@ -42,9 +43,54 @@ public class PostService {
for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post").limit(10).get())) { for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post").limit(10).get())) {
DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get()); DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get());
String username = user.get("username", String.class); 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( 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<List<Post>> {
private final String username;
public GetProfileFeed(@NonNull Context context, String username) {
super(context);
this.username = username;
}
@Nullable
@Override
public List<Post> loadInBackground() {
List<Post> 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("content", String.class),
post.get("image", String.class), post.get("image", String.class),
0, 0,

View File

@ -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<User> {
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<User> {
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();
}
}
}