| | package org.ntlab.irisclient.viewmodels; |
---|
| | |
---|
| | import org.ntlab.irisclient.models.DrawingState; |
---|
| | import org.ntlab.irisclient.resources.DrawingRest; |
---|
| | import org.ntlab.irisclient.resources.RoomsRest; |
---|
| | |
---|
| | import androidx.lifecycle.LiveData; |
---|
| | import androidx.lifecycle.MutableLiveData; |
---|
| | import androidx.lifecycle.ViewModel; |
---|
| | |
---|
| | import java.util.List; |
---|
| | |
---|
| | import retrofit2.Retrofit; |
---|
| | import retrofit2.Call; |
---|
| | import retrofit2.Callback; |
---|
| | import retrofit2.Response; |
---|
| | import retrofit2.converter.jackson.JacksonConverterFactory; |
---|
| | |
---|
| | public class DrawingStateViewModel extends ViewModel { |
---|
| | |
---|
| | final private MutableLiveData<Integer> stateMutableLiveData=new MutableLiveData<>(); |
---|
| | final private MutableLiveData<Integer> dnoMutableLiveData=new MutableLiveData<>(); |
---|
| | final private Retrofit retrofit; |
---|
| | |
---|
| | private Integer drawingStatePreData; |
---|
| | private Integer dnoPreData; |
---|
| | |
---|
| | //フィールド |
---|
| | public DrawingStateViewModel(MutableLiveData<Integer> drawingStateMutableLiveData) { |
---|
| | this.retrofit = new Retrofit.Builder() |
---|
| | .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/iris/") |
---|
| | .addConverterFactory(JacksonConverterFactory.create()) |
---|
| | .build(); |
---|
| | } |
---|
| | |
---|
| | //ゲッター |
---|
| | public LiveData<Integer> getDrawingStateLiveData(){ |
---|
| | return this.stateMutableLiveData; |
---|
| | } |
---|
| | |
---|
| | public void updateState(String rid) { |
---|
| | |
---|
| | final RoomsRest roomsRest = retrofit.create(RoomsRest.class); |
---|
| | final DrawingRest drawingRest = retrofit.create(DrawingRest.class); |
---|
| | Call<Integer> call = roomsRest.getState(rid); |
---|
| | |
---|
| | call.enqueue(new Callback<Integer>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<Integer> call, Response<Integer> response) { |
---|
| | if (response.isSuccessful()){ |
---|
| | |
---|
| | if(response.body().equals(drawingStatePreData)){ |
---|
| | //値が一緒なら書き換えない |
---|
| | }else{ |
---|
| | //値が異なるときのみライブデータを上書き |
---|
| | stateMutableLiveData.setValue(response.body()); |
---|
| | drawingStatePreData = response.body(); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<Integer> call, Throwable t) { |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | // public void updateDno(String rid) { |
---|
| | // |
---|
| | // final RoomsRest roomsRest = retrofit.create(RoomsRest.class); |
---|
| | // final DrawingRest drawingRest = retrofit.create(DrawingRest.class); |
---|
| | // Call<Integer> call = drawingRest.getDno(rid,nickname); |
---|
| | // |
---|
| | // call.enqueue(new Callback<Integer>() { |
---|
| | // @Override |
---|
| | // public void onResponse(Call<Integer> call, Response<Integer> response) { |
---|
| | // if (response.isSuccessful()){ |
---|
| | // |
---|
| | // if(response.body().equals(drawingStatePreData)){ |
---|
| | // //値が一緒なら書き換えない |
---|
| | // }else{ |
---|
| | // //値が異なるときのみライブデータを上書き |
---|
| | // dnoMutableLiveData.setValue(response.body()); |
---|
| | // dnoPreData = response.body(); |
---|
| | // } |
---|
| | // } |
---|
| | // } |
---|
| | // |
---|
| | // @Override |
---|
| | // public void onFailure(Call<Integer> call, Throwable t) { |
---|
| | // } |
---|
| | // }); |
---|
| | // } |
---|
| | public List<String> getKeywords(String rid){ |
---|
| | return null; |
---|
| | } |
---|
| | public LiveData<Integer> getDno(){ |
---|
| | return this.dnoMutableLiveData; |
---|
| | } |
---|
| | public LiveData<Integer> getCno(){ |
---|
| | return this.stateMutableLiveData; |
---|
| | } |
---|
| | |
---|
| | } |
---|
| | |
---|
| | |
DrawingStateViewModelを追加
RemoteOkazaki0726
d12e9a6
intomaster
fromRemoteOkazaki0726
on 26 Jul 2022