| | package org.ntlab.irisclient.viewmodels; |
---|
| | |
---|
| | import org.ntlab.irisclient.entities.RoomJson; |
---|
| | import org.ntlab.irisclient.models.Settings; |
---|
| | import org.ntlab.irisclient.resources.RoomsRest; |
---|
| | |
---|
| | import androidx.lifecycle.LiveData; |
---|
| | import androidx.lifecycle.MutableLiveData; |
---|
| |
---|
| | import retrofit2.converter.jackson.JacksonConverterFactory; |
---|
| | |
---|
| | public class RoomViewModel extends ViewModel { |
---|
| | |
---|
| | private MutableLiveData<RoomJson> roomMutableLiveData; |
---|
| | private Retrofit retrofit; |
---|
| | //フィールド |
---|
| | final private MutableLiveData<RoomJson> roomMutableLiveData; |
---|
| | final private MutableLiveData<Settings> settingsMutableLiveData; |
---|
| | final private Retrofit retrofit; |
---|
| | |
---|
| | //------------------------------------------------ |
---|
| | //更新比較用フィールド |
---|
| | private Settings settingsPreData; |
---|
| | private RoomJson roomJsonPreData; |
---|
| | |
---|
| | //------------------------------------------------------------------ |
---|
| | //コンストラクタ |
---|
| | public RoomViewModel() { |
---|
| | this.roomMutableLiveData = new MutableLiveData<>(); |
---|
| | this.settingsMutableLiveData = new MutableLiveData<>(); |
---|
| | this.retrofit = new Retrofit.Builder() |
---|
| | .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/iris/") |
---|
| | .addConverterFactory(JacksonConverterFactory.create()) |
---|
| | .build(); |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // getter |
---|
| | public LiveData<RoomJson> getLiveData() { |
---|
| | public LiveData<RoomJson> getRoomLiveData() { |
---|
| | return this.roomMutableLiveData; |
---|
| | } |
---|
| | public LiveData<Settings> getSettingsLiveData() { |
---|
| | return this.settingsMutableLiveData; |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | //----------------------------------------------------------------------------- |
---|
| | // updates |
---|
| | //メンバーの更新、設定情報の更新 |
---|
| | public void updateMembers(String rid) { |
---|
| | //部屋の情報の更新(RoomJson) |
---|
| | public void updateRoom(String rid) { |
---|
| | |
---|
| | final RoomsRest roomsRest = retrofit.create(RoomsRest.class); |
---|
| | |
---|
| | Call<RoomJson> call = roomsRest.getRoomMember(rid); |
---|
| | |
---|
| | call.enqueue(new Callback<RoomJson>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<RoomJson> call, Response<RoomJson> response) { |
---|
| | if (response.isSuccessful()){ |
---|
| | roomMutableLiveData.setValue(response.body()); |
---|
| | |
---|
| | if(response.body().equals(roomJsonPreData)){ |
---|
| | //値が一緒なら書き換えない |
---|
| | }else{ |
---|
| | //値が異なるときのみライブデータを上書き |
---|
| | roomMutableLiveData.setValue(response.body()); |
---|
| | roomJsonPreData = response.body(); |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | } |
---|
| | @Override |
---|
| | public void onFailure(Call<RoomJson> call, Throwable t) { |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | //設定情報の更新(Settings) |
---|
| | public void updateSettings(String rid) { |
---|
| | |
---|
| | final RoomsRest roomsRest = retrofit.create(RoomsRest.class); |
---|
| | Call<Settings> call = roomsRest.getSettings(rid); |
---|
| | |
---|
| | call.enqueue(new Callback<Settings>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<Settings> call, Response<Settings> response) { |
---|
| | if (response.isSuccessful()){ |
---|
| | |
---|
| | if(response.body().equals(settingsPreData)){ |
---|
| | //値が一緒なら書き換えない |
---|
| | }else{ |
---|
| | //値が異なるときのみライブデータを上書き |
---|
| | settingsMutableLiveData.setValue(response.body()); |
---|
| | settingsPreData = response.body(); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<RoomJson> call, Throwable t) { |
---|
| | |
---|
| | public void onFailure(Call<Settings> call, Throwable t) { |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | /* |
---|
| | //設定情報の通信、返り値はRoomJsonなら上記のupdateのみで十分なので、不要か? |
---|
| | public void updateSettings(String rid) { |
---|
| | final RoomsRest roomsRest = retrofit.create(RoomsRest.class); |
---|
| | |
---|
| | Call<RoomJson> call = roomsRest.getSettings(rid); |
---|
| | |
---|
| | call.enqueue(new Callback<RoomJson>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<RoomJson> call, Response<RoomJson> response) { |
---|
| | if (response.isSuccessful()){ |
---|
| | roomMutableLiveData.setValue(response.body()); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<RoomJson> call, Throwable t) { |
---|
| | |
---|
| | } |
---|
| | }); |
---|
| | }*/ |
---|
| | } |
---|
| | |
---|
| | |