-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented basic functionality for games fragment (RecyclerView)
- RecyclerView that inflates a CardView for each game using databinding
- Loading branch information
Showing
10 changed files
with
362 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
app/src/main/java/wjhj/orbital/sportsmatchfindingapp/homepage/GamesCardAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
app/src/main/java/wjhj/orbital/sportsmatchfindingapp/homepage/GamesFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/src/main/java/wjhj/orbital/sportsmatchfindingapp/user/UserProfileViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.