Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Profile header displays followers and following count #124

Merged
merged 11 commits into from
Apr 16, 2023
16 changes: 16 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions .idea/saveactions_settings.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ProfileFragmentTest {
ViewInteraction userNameText = onView(withId(R.id.username_text));
ViewInteraction editButton = onView(withId(R.id.edit_button));
ViewInteraction followingButton = onView(withId(R.id.profile_following_button));
ViewInteraction followersButton = onView(withId(R.id.profile_followers_button));
ViewInteraction defaultCollection = onView(withId(R.id.collection_list));
ViewInteraction addMediaButton = onView(withId(R.id.add_media_button));
ViewInteraction addCollectionButton = onView(withId(R.id.add_collection_button));
Expand Down Expand Up @@ -70,18 +71,24 @@ public void setUp() {
});
}

// Test whether the "Friends" button is displayed and contains the correct text
// Test whether the "Following" button is displayed and contains the correct text
@Test
public void testFollowingButton() {
followingButton.check(matches(isDisplayed()));
followingButton.check(matches(withText(R.string.following)));
}

// Test whether the "Followers" button is displayed and contains the correct text
@Test
public void testFollowersButton() {
followersButton.check(matches(isDisplayed()));
followersButton.check(matches(withText(R.string.followers)));
}

// Test whether the "Edit" button is displayed and contains the correct text
@Test
public void testEditButton() {
editButton.check(matches(isDisplayed()));
editButton.check(matches(withText("Edit")));
}

// Test whether the profile picture is displayed
Expand Down
51 changes: 38 additions & 13 deletions app/src/main/java/com/github/sdp/mediato/ProfileFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -21,11 +22,11 @@
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.sdp.mediato.data.CollectionsDatabase;
import com.github.sdp.mediato.data.UserDatabase;
import com.github.sdp.mediato.model.Review;
import com.github.sdp.mediato.model.media.Collection;
import com.github.sdp.mediato.ui.MyFollowersFragment;
import com.github.sdp.mediato.ui.MyFollowingFragment;
import com.github.sdp.mediato.ui.viewmodel.ProfileViewModel;
import com.github.sdp.mediato.utility.PhotoPicker;
Expand All @@ -43,8 +44,9 @@ public class ProfileFragment extends Fragment {

private ProfileViewModel viewModel;
private PhotoPicker photoPicker;
private Button editButton;
private ImageButton editButton;
private Button followingButton;
private Button followersButton;
private Button addCollectionButton;
private TextView usernameView;
private ImageView profileImage;
Expand Down Expand Up @@ -78,6 +80,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Get all UI components
editButton = view.findViewById(R.id.edit_button);
followingButton = view.findViewById(R.id.profile_following_button);
followersButton = view.findViewById(R.id.profile_followers_button);
addCollectionButton = view.findViewById(R.id.add_collection_button);
usernameView = view.findViewById(R.id.username_text);
profileImage = view.findViewById(R.id.profile_image);
Expand All @@ -86,17 +89,29 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Initialize components
photoPicker = setupPhotoPicker();
setupFollowingButton(followingButton);
setupFollowersButton(followersButton);
collectionlistAdapter = setupCollections(collectionListRecyclerView);
setupAddCollectionsButton(addCollectionButton);

// Observe the view model's live data to update UI components
observeUsername();
observeProfilePic();
observeCollections(collectionlistAdapter);
observeFollowingCount();
observeFollowersCount();
updateFollowerButtonCounts();

return view;
}

private void updateFollowerButtonCounts() {
UserDatabase.getUser(USERNAME).thenAccept(user -> {
viewModel.setFollowing(user.getFollowingCount());
System.out.println("follwers count " + user.getFollowersCount() + " " + user.getFollowers());
viewModel.setFollowers(user.getFollowersCount());
});
}

private CollectionListAdapter setupCollections(RecyclerView recyclerView) {
// Check if a collection is already in the viewModel, if not create the default one
List<Collection> collections = viewModel.getCollections();
Expand Down Expand Up @@ -145,6 +160,18 @@ private void observeProfilePic() {
bitmap -> profileImage.setImageBitmap(bitmap));
}

private void observeFollowingCount() {
viewModel.getFollowingLiveData().observe(getViewLifecycleOwner(), followingCount -> {
followingButton.setText(followingCount + " " + getResources().getString(R.string.following));
});
}

private void observeFollowersCount() {
viewModel.getFollowersLiveData().observe(getViewLifecycleOwner(), followersCount -> {
followersButton.setText(followersCount + " " + getResources().getString(R.string.followers));
});
}

private PhotoPicker setupPhotoPicker() {
PhotoPicker photoPicker = new PhotoPicker(this, profileImage);

Expand All @@ -160,14 +187,13 @@ private PhotoPicker setupPhotoPicker() {
);
return photoPicker;
}

private void setupFollowingButton(Button followingButton) {
followingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMyFollowingFragment();
}
});
followingButton.setOnClickListener(v -> switchFragment(new MyFollowingFragment()));
}

private void setupFollowersButton(Button followersButton) {
followersButton.setOnClickListener(v -> switchFragment(new MyFollowersFragment()));
}

private void showEnterCollectionNameDialog() {
Expand Down Expand Up @@ -235,15 +261,14 @@ private void downloadProfilePicWithRetry(String username) {
});
}

private void openMyFollowingFragment() {
MyFollowingFragment myFollowingFragment = new MyFollowingFragment();
private void switchFragment(Fragment fragment) {
Bundle args = new Bundle();
args.putString("username", USERNAME);
myFollowingFragment.setArguments(args);
fragment.setArguments(args);

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_container, myFollowingFragment);
fragmentTransaction.replace(R.id.main_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/java/com/github/sdp/mediato/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.sdp.mediato.errorCheck.Preconditions;
import com.github.sdp.mediato.model.media.Collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -63,21 +62,33 @@ public List<String> getFollowers() {
}

/**
* Returns a list of the usernames of the users the user is following by adapting the map attribute
* Returns a list of the usernames of the users the user is following by adapting the map
* attribute
*/
public List<String> getFollowing() {
List<String> followingList = new ArrayList<>();
for (String userFollowing : following.keySet()) {
if (following.get(userFollowing)) followingList.add(userFollowing);
if (following.get(userFollowing)) {
followingList.add(userFollowing);
}
}
return followingList;
}

public int getFollowingCount() {
return getFollowing().size();
}

public int getFollowersCount() {
return getFollowers().size();
}

public Map<String, Collection> getCollections() {
return collections;
}

public static class UserBuilder {

private String id = "";
private String username = "";
private String email = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.sdp.mediato.ui;

import android.os.Bundle;
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.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.github.sdp.mediato.R;
import com.github.sdp.mediato.ui.viewmodel.MyFollowersViewModel;
import com.github.sdp.mediato.utility.adapters.UserAdapter;

public class MyFollowersFragment extends Fragment {

private static String USERNAME;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
USERNAME = getArguments().getString("username");
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_followers, container, false);

// Create and init the Search User ViewModel
MyFollowersViewModel myFollowersViewModel = new ViewModelProvider(this).get(
MyFollowersViewModel.class);
myFollowersViewModel.setUserName(USERNAME);

// Set the Search User RecyclerView with its adapter
RecyclerView recyclerView = view.findViewById(R.id.myFollowing_recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerView.setAdapter(new UserAdapter(myFollowersViewModel, this));

return view;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ public void onCreate(@Nullable Bundle savedInstanceState) {

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_following, container, false);

// Create and init the Search User ViewModel
MyFollowingViewModel myFollowingViewModelViewModel = new ViewModelProvider(this).get(MyFollowingViewModel.class);
myFollowingViewModelViewModel.setUserName(USERNAME);
MyFollowingViewModel myFollowingViewModel = new ViewModelProvider(getActivity()).get(MyFollowingViewModel.class);
myFollowingViewModel.setUserName(USERNAME);

// Set the Search User RecyclerView with its adapter
RecyclerView recyclerView = view.findViewById(R.id.myFollowing_recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerView.setAdapter(new UserAdapter(myFollowingViewModelViewModel));
recyclerView.setAdapter(new UserAdapter(myFollowingViewModel, this));

return view;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@

import static com.github.sdp.mediato.data.UserDatabase.getUser;

import android.view.View;


import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.sdp.mediato.R;
import com.github.sdp.mediato.model.LocalFilmDatabase;
import com.github.sdp.mediato.model.User;
import com.github.sdp.mediato.model.media.Media;
import com.github.sdp.mediato.ui.viewmodel.SearchUserViewModel;
import com.github.sdp.mediato.utility.adapters.UserAdapter;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -98,7 +93,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Set the Search User RecyclerView with its adapter
recyclerView = searchView.findViewById(R.id.searchactivity_recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerView.setAdapter(new UserAdapter(searchUserViewModel));
recyclerView.setAdapter(new UserAdapter(searchUserViewModel, this));

// get the search view and bind it to a listener
this.searchView = searchView.findViewById(R.id.searchactivity_searchview_searchbar);
Expand Down
Loading