diff --git a/.idea/compiler.xml b/.idea/compiler.xml index b589d56..b86273d 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml index b268ef3..5a72e7e 100644 --- a/.idea/deploymentTargetSelector.xml +++ b/.idea/deploymentTargetSelector.xml @@ -4,6 +4,14 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index 8978d23..74dd639 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,7 @@ + - + diff --git a/app/src/main/java/com/example/tampopo_client/views/FriendReceivedFragment.java b/app/src/main/java/com/example/tampopo_client/views/FriendReceivedFragment.java index b5a8976..3d4f237 100644 --- a/app/src/main/java/com/example/tampopo_client/views/FriendReceivedFragment.java +++ b/app/src/main/java/com/example/tampopo_client/views/FriendReceivedFragment.java @@ -36,6 +36,15 @@ private Tampopo tampopo; + // RecyclerView とそのアダプターをフィールドとして保持しておく + private RecyclerView recyclerView; + private MyFriendRequestRecyclerViewAdapter adapter; + + // 現在ログインしているユーザー(=この画面の受信者)の ID を保持しておく + // API から返ってきた FriendRequest の中から、このユーザー宛ての申請だけを + // 絞り込んで表示するために利用する + private String receiverId; + /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orientation changes). @@ -70,7 +79,8 @@ FriendViewModel friendViewModel = new ViewModelProvider(this).get(FriendViewModel.class); tampopo = (Tampopo) getActivity().getApplication(); - String receiverId = tampopo.getUserId(); + // フィールドに受信者 ID を保持しておく + receiverId = tampopo.getUserId(); String token = tampopo.getToken(); @@ -81,13 +91,20 @@ // Set the adapter if (view instanceof RecyclerView) { Context context = view.getContext(); - RecyclerView recyclerView = (RecyclerView) view; + recyclerView = (RecyclerView) view; + if (mColumnCount <= 1) { recyclerView.setLayoutManager(new LinearLayoutManager(context)); } else { recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); } - recyclerView.setAdapter(new MyFriendRequestRecyclerViewAdapter(FriendRequestContent.ITEMS, friendViewModel, receiverId, token)); + + // 画面を開くたびに、いったんリストを空にしてから最新データを入れるようにする + FriendRequestContent.ITEMS.clear(); + FriendRequestContent.ITEM_MAP.clear(); + + adapter = new MyFriendRequestRecyclerViewAdapter(FriendRequestContent.ITEMS, friendViewModel, receiverId, token); + recyclerView.setAdapter(adapter); } return view; } @@ -102,10 +119,48 @@ // LiveData に変更があったとき(新しい友達リクエストのリストが届いたとき)に呼ばれるメソッド @Override public void onChanged(List friendRequests) { - for (FriendRequest f: friendRequests) { - FriendRequestContent.addItem(new FriendRequestContent.FriendRequestItem(f.getSenderId(), "ユーザー名")); + // LiveData に新しいリストが来たら、表示用リストを作り直してアダプターに更新を通知 + FriendRequestContent.ITEMS.clear(); + FriendRequestContent.ITEM_MAP.clear(); + + if (friendRequests != null) { + for (FriendRequest f : friendRequests) { + // この端末のユーザー宛て(receiverId が一致)の申請だけを表示する + if (receiverId != null && receiverId.equals(f.getReceiverId())) { + FriendRequestContent.addItem( + new FriendRequestContent.FriendRequestItem( + f.getSenderId(), + "ユーザー名" + ) + ); + } + } + } + + if (adapter != null) { + adapter.notifyDataSetChanged(); } } }); } + + // フラグメントが前面に戻ってきたタイミングでも、最新の申請一覧を取り直す + // これにより、一度「フレンド」画面を開いた後で別画面へ移動し、 + // その間に相手からフレンド申請が送られていた場合でも、 + // 「保留中」タブに戻ってきたときに最新の状態が反映される + @Override + public void onResume() { + super.onResume(); + + if (getActivity() == null) { + return; + } + + if (tampopo == null) { + tampopo = (Tampopo) getActivity().getApplication(); + } + + FriendReceivedRequestViewModel friendReceivedRequestViewModel = new ViewModelProvider(this).get(FriendReceivedRequestViewModel.class); + friendReceivedRequestViewModel.loadReceivedRequests(tampopo.getToken()); + } } \ No newline at end of file