diff --git a/app/src/main/java/org/ntlab/irisclient/viewmodels/RoomViewModel.java b/app/src/main/java/org/ntlab/irisclient/viewmodels/RoomViewModel.java index 4a451f8..10909e2 100644 --- a/app/src/main/java/org/ntlab/irisclient/viewmodels/RoomViewModel.java +++ b/app/src/main/java/org/ntlab/irisclient/viewmodels/RoomViewModel.java @@ -1,7 +1,5 @@ package org.ntlab.irisclient.viewmodels; -import org.ntlab.irisclient.Iris; -import org.ntlab.irisclient.entities.RoomJson; import org.ntlab.irisclient.models.Member; import org.ntlab.irisclient.models.Settings; import org.ntlab.irisclient.resources.RoomsRest; @@ -23,20 +21,21 @@ //フィールド private String rid; - private boolean roomExist; final private Retrofit retrofit; final private RoomsRest roomsRest; - final private MutableLiveData> membersLiveData; - final private MutableLiveData settingsMutableLiveData; - final private MutableLiveData stateLiveData; - //更新比較用フィールド + //更新比較用、保存用フィールド private List sortedMembersList; private List membersPreData; private Settings settingsPreData; private int preState = 0; - //------------------------------------------------------------------ + //ライブデータ + final private MutableLiveData> membersLiveData; + final private MutableLiveData settingsMutableLiveData; + final private MutableLiveData stateLiveData; + + //----------------------------------------------------------------------------- //コンストラクタ public RoomViewModel() { this.membersLiveData = new MutableLiveData<>(); @@ -47,17 +46,13 @@ .addConverterFactory(JacksonConverterFactory.create()) .build(); this.roomsRest = retrofit.create(RoomsRest.class); - setRoomExist(true); } - //----------------------------------------------------------------- + //----------------------------------------------------------------------------- //setter public void setRid(String rid){ this.rid = rid; } - private void setRoomExist(boolean bool) { - this.roomExist = bool; - } // getter public LiveData> getMembersLiveData() { @@ -69,11 +64,12 @@ public LiveData getStateLiveData() { return this.stateLiveData; } - public boolean getRoomExist(){ - return this.roomExist; - } - private boolean compareMembers(List a, List b) { + //----------------------------------------------------------------------------- + //roomViewModelのみの機能的なメソッド + + //2つのメンバーリストを比較。異なればfalse、同じならtrueを返す + private boolean compareMembers(List a, List b) { int i; for(i = 0; i < a.size(); i++ ) { if (Objects.equals(a.get(i).getNickname(), b.get(i).getNickname())) { @@ -94,6 +90,8 @@ //全ての要素が一緒ならtrueを返す return true; } + + //メンバーリストを優先順位順に並び変えるメソッド private List sortMembersList(List oldMembers) { List sortedMembers = new ArrayList<>(); List> priorityInteger = new ArrayList<>(5); @@ -153,47 +151,71 @@ return sortedMembers; } + //2つの設定情報データの比較(Settings) + private boolean compareSettings(Settings a, Settings b) { + + //Drawing関係 + if(a.isDrawingTimer() != b.isDrawingTimer()) { + return false; + } else if(a.getDrawingTimerTimes() != b.getDrawingTimerTimes()) { + return false; + } + + //Game関係 + if(a.isGameTimer() != b.isGameTimer()){ + return false; + } else if(a.getGameTimerTimes() != b.getGameTimerTimes()) { + return false; + } else if(a.getGameTimerFirstThinkingTimes() != b.getGameTimerFirstThinkingTimes()) { + return false; + } + + //全て同じ要素ならtrueを返す + return true; + } + + //----------------------------------------------------------------------------- //各Activityでのボタン処理 - //チーム&マスター変更 + + //チーム&マスター変更(Member & Owner Activity共通) public void changeBelongsAndMaster(String rid, String nickname, String belongs, Boolean isMaster) { - //final RoomsRest roomsRest = retrofit.create(RoomsRest.class); Call call = roomsRest.putMember(rid, nickname, belongs, isMaster); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ - System.out.println("通信成功:changeBelongsAndMaster"); + //System.out.println("通信成功:changeBelongsAndMaster"); + } else { + //System.out.println("通信可能:changeBelongsAndMaster: " + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:changeBelongsAndMaster"); - System.out.println(t); + //System.out.println("通信失敗:changeBelongsAndMaster"); } }); } //メンバー削除(MemberActivityのみ) public void deleteMember(String rid, String nickname) { - //final RoomsRest roomsRest = retrofit.create(RoomsRest.class); Call call = roomsRest.deleteMember(rid, nickname); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ - System.out.println("通信成功:退出しました"); + //System.out.println("通信成功:deleteMember"); + } else { + //System.out.println("通信可能:deleteMember: " + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:"); - System.out.println(t); + //System.out.println("通信失敗:deleteMember"); } }); - } //オーナーが退出ボタンを押した時の通信(OwnerActivityのみ) @@ -204,34 +226,33 @@ @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ - System.out.println("通信成功:ownerLeaving"); deleteRoom(rid); + //System.out.println("通信成功:ownerLeaving"); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:ownerLeaving"); - System.out.println(t); + //System.out.println("通信失敗:ownerLeaving"); } }); } //スタートボタンの処理(OwnerActivityのみ) public void startDrawing(String rid, String nickname) { - //final RoomsRest roomsRest = retrofit.create(RoomsRest.class); Call call = roomsRest.putState(rid,nickname,2); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.isSuccessful()){ - System.out.println("通信成功:startDrawing"); + //System.out.println("通信成功:startDrawing"); + } else { + //System.out.println("通信可能:startDrawing: " + response.code()); } } @Override public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:startDrawing"); - System.out.println(t); + //System.out.println("通信失敗:startDrawing"); } }); } @@ -243,13 +264,12 @@ call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { - System.out.println("ランダムにチーム変更へ"); + //System.out.println("ランダムにチーム変更へ"); } @Override public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:randomTeam"); - System.out.println(t); + //System.out.println("通信失敗:randomTeam"); } }); } @@ -273,20 +293,19 @@ }); } - //部屋の削除 + //部屋の削除(ViewModel内で呼び出し) public void deleteRoom(String rid){ Call call = roomsRest.deleteRooms(rid); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { - setRoomExist(false); + //System.out.println("通信成功:deleteRoom"); } @Override public void onFailure(Call call, Throwable t) { - System.out.println("通信失敗:deleteRoom"); - System.out.println(t); + //System.out.println("通信失敗:deleteRoom"); } }); } @@ -310,9 +329,11 @@ if (response.isSuccessful()){ if (membersPreData != null) { + if (response.body().isEmpty()) { //もしもレスポンスが空なら何もしない } else { + if (response.body().size() != membersPreData.size()) { //配列のサイズが異なるならその時点で並び替えてライブデータ書き換え sortedMembersList = sortMembersList(response.body()); @@ -332,7 +353,8 @@ } } } - } else { + + } else { //(memberPreData == null) //最初のアップデートは無条件でライブデータ書き換え sortedMembersList = sortMembersList(response.body()); membersLiveData.setValue(sortedMembersList); @@ -345,6 +367,7 @@ @Override public void onFailure(Call> call, Throwable t) { + //System.out.println("通信失敗:updateMembers"); } }); } @@ -358,8 +381,21 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()){ - settingsMutableLiveData.setValue(response.body()); - settingsPreData = response.body(); + if(settingsPreData != null) { + + if (response.body() == null) { + //もしもレスポンスが空なら何もしない + } else { + if(compareSettings(response.body(), settingsPreData)) { + settingsMutableLiveData.setValue(response.body()); + settingsPreData = response.body(); + } + } + + } else { //(settingsPreData == null) + settingsMutableLiveData.setValue(response.body()); + settingsPreData = response.body(); + } } } @@ -379,20 +415,11 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()){ + //値が一緒なら書き換えない if(preState != response.body()){ preState = response.body(); stateLiveData.setValue(response.body()); } - - /* - if(response.body().equals(settingsPreData)){ - //値が一緒なら書き換えない - }else{ - //値が異なるときのみライブデータを上書き - settingsMutableLiveData.setValue(response.body()); - settingsPreData = response.body(); - }*/ - } }