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..69217fb 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/viewmodels/ActivityViewModel.java b/app/src/main/java/com/example/tampopo_client/viewmodels/ActivityViewModel.java index 3d15caf..8c05d7d 100644 --- a/app/src/main/java/com/example/tampopo_client/viewmodels/ActivityViewModel.java +++ b/app/src/main/java/com/example/tampopo_client/viewmodels/ActivityViewModel.java @@ -108,8 +108,17 @@ }); // 自分のフレンドの最新のアクティビティを取得して更新する - if (friendUserIdsLiveData.isInitialized() && friendUserIdsLiveData.getValue() != null) { - for (String userId : friendUserIdsLiveData.getValue()) { + // NOTE: + // 以前は friendUserIdsLiveData.isInitialized() を利用していましたが、 + // その条件が真にならずフレンドのアクティビティ取得処理が一度も + // 実行されないケースがあり、メイン画面表示中にフレンドが + // アクティビティを更新してもFriendIconViewの吹き出しが更新されない + // 問題の原因となっていました。 + // isInitialized() に依存せず、値が存在する場合は常に更新を行うことで + // LiveData を確実に更新し、UI のオブザーバに通知されるようにします。 + List friendIds = friendUserIdsLiveData.getValue(); + if (friendIds != null) { + for (String userId : friendIds) { pullLatestActivity(userId, activitiesResource, new ActivityFetchCallback() { @Override public void onSuccess(Activity activity) { @@ -124,11 +133,11 @@ } } - if (friendUserIdsLiveData.isInitialized()) { - // 最新のフレンドのユーザーIDを取得して更新する - // TODO: 適切なコールバックを使う必要あり - pullLatestFriendUserIds(myUserId, myToken); - } + // 最新のフレンドのユーザーIDを取得して更新する + // isInitialized() に依存せず、定期的にサーバから最新状態を取得する + // ことで、フレンド追加・削除やアクティビティ更新順の変化にも追従する + // ようにする + pullLatestFriendUserIds(myUserId, myToken); }; } @@ -166,10 +175,6 @@ * @param newActivity 新しいアクティビティのテキスト */ public void createActivity(String userId, String token, String newActivity) { - if (!myLatestActivityLiveData.isInitialized()) { - return; - } - Call createActivityCall = activitiesResource.addActivity(userId, token, newActivity); createActivityCall.enqueue(new Callback() { @Override @@ -261,28 +266,38 @@ public void onResponse(@NonNull Call> call, @NonNull Response> response) { if (response.isSuccessful()) { - // MEMO: 入れ込んだだけ - // フレンドのIDをアクティビティ更新順に並べ替える - List friendUserIds = friendUserIdsLiveData.getValue(); - if (friendUserIds == null || !friendUserIds.equals(response.body())) { - friendUserIdsLiveData.postValue(response.body()); - SortedSet friends = new TreeSet<>(new Friend.UpdateTimeComparator()); + // サーバから取得した最新のフレンド一覧 + List latestFriendUserIds = response.body(); - assert friendUserIds != null; - friendUserIds.forEach(userId -> { - List activities = getActivitiesLiveDataFromUserId(userId).getValue(); - if (activities == null || activities.isEmpty()) { - return; - } - - Activity latestActivity = activities.get(0); - friends.add(new Friend(userId, latestActivity.getUpdateTime())); - }); - - // 並び替えたフレンドのユーザーIDを順番に格納して更新する - sortedFriendUserIds.clear(); - friends.forEach(friend -> sortedFriendUserIds.add(friend.getUserId())); + // null セーフティ + if (latestFriendUserIds == null) { + return; } + + // 以前と同じリストであれば何もしない + List currentFriendUserIds = friendUserIdsLiveData.getValue(); + if (currentFriendUserIds != null && currentFriendUserIds.equals(latestFriendUserIds)) { + return; + } + + // LiveData を最新のフレンド一覧で更新 + friendUserIdsLiveData.postValue(latestFriendUserIds); + + // フレンドのIDをアクティビティ更新順に並べ替える + SortedSet friends = new TreeSet<>(new Friend.UpdateTimeComparator()); + latestFriendUserIds.forEach(userId -> { + List activities = getActivitiesLiveDataFromUserId(userId).getValue(); + if (activities == null || activities.isEmpty()) { + return; + } + + Activity latestActivity = activities.get(0); + friends.add(new Friend(userId, latestActivity.getUpdateTime())); + }); + + // 並び替えたフレンドのユーザーIDを順番に格納して更新する + sortedFriendUserIds.clear(); + friends.forEach(friend -> sortedFriendUserIds.add(friend.getUserId())); } }