Skip to content

Commit

Permalink
Implemented basic functionality for games fragment (RecyclerView)
Browse files Browse the repository at this point in the history
- RecyclerView that inflates a CardView for each game using databinding
  • Loading branch information
weiijiie committed Jun 22, 2019
1 parent 4f2a42c commit 824e413
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 7 deletions.
35 changes: 35 additions & 0 deletions .idea/misc.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Game {

private String details;
private Location location;
private String gameName;
private String name;
private int minPlayers;
private int maxPlayers;
private List<String> participating;
Expand All @@ -29,7 +29,7 @@ public Game() {
private Game(Game.Builder builder) {
details = builder.details;
location = builder.location;
gameName = builder.gameName;
name = builder.gameName;
minPlayers = builder.minPlayers;
maxPlayers = builder.maxPlayers;
participating = builder.participating;
Expand Down Expand Up @@ -61,11 +61,11 @@ public void setLocation(Location location) {
}

public String getName() {
return gameName;
return name;
}

public void setName(String name) {
this.gameName = name;
this.name = name;
}

public Difficulty getSkill() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package wjhj.orbital.sportsmatchfindingapp.homepage;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import wjhj.orbital.sportsmatchfindingapp.databinding.GamesCardViewBinding;
import wjhj.orbital.sportsmatchfindingapp.game.Game;

public class GamesCardAdapter extends RecyclerView.Adapter<GamesCardAdapter.CardViewHolder> {

private List<Game> games;

public static class CardViewHolder extends RecyclerView.ViewHolder {

public CardView cardView;
private GamesCardViewBinding cardBinding;

public CardViewHolder(@NonNull CardView itemView, GamesCardViewBinding binding) {
super(itemView);
cardView = itemView;
cardBinding = binding;
}

private void setGame(Game game) {
cardBinding.setGame(game);
}
}

public GamesCardAdapter(List<Game> games) {
this.games = games;
}

public void remove() {
//todo: proof of concept test. refactor
games.remove(0);
notifyItemRemoved(0);
}

@NonNull
@Override
public GamesCardAdapter.CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// create view for each data in the set
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
GamesCardViewBinding binding = GamesCardViewBinding.inflate(inflater, parent, false);

return new CardViewHolder((CardView) binding.getRoot(), binding);
}

@Override
public void onBindViewHolder(@NonNull CardViewHolder holder, int position) {
holder.setGame(games.get(position));
}

@Override
public int getItemCount() {
return games.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package wjhj.orbital.sportsmatchfindingapp.homepage;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import wjhj.orbital.sportsmatchfindingapp.R;
import wjhj.orbital.sportsmatchfindingapp.databinding.FragmentGamesBinding;
import wjhj.orbital.sportsmatchfindingapp.user.UserProfileViewModel;

/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link GamesFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link GamesFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class GamesFragment extends Fragment {

private static String GAMES_PAGE_DEBUG = "games page";

private UserProfileViewModel userProfileViewModel;
private FragmentGamesBinding binding;
private OnFragmentInteractionListener mListener;

public GamesFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
* @return A new instance of fragment GamesFragment.
*/
// TODO: Rename and change types and number of parameters
public static GamesFragment newInstance() {
return new GamesFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(GAMES_PAGE_DEBUG, "Created fragment");
userProfileViewModel = ViewModelProviders.of(getActivity()).get(UserProfileViewModel.class);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(GAMES_PAGE_DEBUG, "Created view");
binding = FragmentGamesBinding.inflate(inflater, container, false);
binding.setUserProfile(userProfileViewModel);
binding.setLifecycleOwner(getActivity());

RecyclerView recyclerView = binding.confirmedGamesRecyclerView;
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
GamesCardAdapter adapter = new GamesCardAdapter(userProfileViewModel.getConfirmedGames().getValue());
recyclerView.setAdapter(adapter);

binding.testFilterButton.setOnClickListener(view -> adapter.remove());

return binding.getRoot();
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.fragment.app.FragmentManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
Expand All @@ -19,7 +20,7 @@
import wjhj.orbital.sportsmatchfindingapp.auth.LoginActivity;
import wjhj.orbital.sportsmatchfindingapp.databinding.HomepageActivityBinding;

public class HomepageActivity extends AppCompatActivity {
public class HomepageActivity extends AppCompatActivity implements GamesFragment.OnFragmentInteractionListener {

public static final String CURR_USER_TAG = "current_user";
public static final String HOMEPAGE_DEBUG = "homepage";
Expand Down Expand Up @@ -50,7 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
//todo
break;
case R.id.nav_games:
//todo
fragment = GamesFragment.newInstance();
break;
case R.id.nav_search:
//todo
Expand Down Expand Up @@ -90,4 +91,13 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
public void onFragmentInteraction(Uri uri) {
//TODO: implement/check if necessary
}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {
//TODO: implement/check if necessary
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import wjhj.orbital.sportsmatchfindingapp.game.GameStatus;

public class UserProfile {
public static final String USER_DEBUG = "user";
private static final String USER_DEBUG = "user";

private String uid;
private Gender gender;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package wjhj.orbital.sportsmatchfindingapp.user;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

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

import wjhj.orbital.sportsmatchfindingapp.game.Game;

public class UserProfileViewModel extends ViewModel {

private MutableLiveData<List<Game>> confirmedGames;

public MutableLiveData<List<Game>> getConfirmedGames() {

//TESTS
List<Game> games = new ArrayList<>();
Game game1 = Game.builder()
.addName("SOCCER :D")
.addDetails("TMR 6pm OR DIE")
.build();

Game game2 = Game.builder()
.addDetails("Haw not invited")
.addName("dota lan anyone?")
.build();

Game game3 = Game.builder()
.addName("no details...")
.build();

games.add(game1);
games.add(game2);
games.add(game3);
games.add(game1);
games.add(game2);
games.add(game3);
games.add(game1);
games.add(game2);
games.add(game3);

MutableLiveData<List<Game>> liveData = new MutableLiveData<>();
liveData.setValue(games);
confirmedGames = liveData;

return confirmedGames;
}

}
33 changes: 33 additions & 0 deletions app/src/main/res/layout/fragment_games.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="userProfile"
type="wjhj.orbital.sportsmatchfindingapp.user.UserProfileViewModel" />
</data>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".homepage.GamesFragment">

<com.google.android.material.button.MaterialButton
android:id="@+id/test_filter_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:text="Remove first game"
android:layout_alignParentTop="true"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/confirmed_games_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/test_filter_button"/>

</RelativeLayout>
</layout>
Loading

0 comments on commit 824e413

Please sign in to comment.