| |
---|
| | import java.util.concurrent.ScheduledExecutorService; |
---|
| | import java.util.concurrent.ScheduledThreadPoolExecutor; |
---|
| | import java.util.concurrent.TimeUnit; |
---|
| | |
---|
| | import kotlinx.coroutines.sync.Mutex; |
---|
| | import retrofit2.Call; |
---|
| | import retrofit2.Callback; |
---|
| | import retrofit2.Response; |
---|
| | import retrofit2.Retrofit; |
---|
| |
---|
| | |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // |
---|
| | public class PaintViewModel extends ViewModel implements Runnable { |
---|
| | public class PaintViewModel extends ViewModel { |
---|
| | |
---|
| | private MutableLiveData<Animation> animationMutableLiveData; |
---|
| | |
---|
| | private int currentStrokeNo; |
---|
| | private Acanthus acanthus; |
---|
| | private Retrofit retrofit; |
---|
| | private PaintModel paintModel; |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | //----------------------------------------------------------------- |
---|
| | public PaintViewModel() { |
---|
| | Retrofit retrofit = new Retrofit.Builder() |
---|
| | .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") |
---|
| | .addConverterFactory(ScalarsConverterFactory.create()) |
---|
| | .addConverterFactory(JacksonConverterFactory.create()) |
---|
| | .build(); |
---|
| | |
---|
| | startObservationOfChangedStroke(); |
---|
| | } |
---|
| | //----------------------------------------------------------------- |
---|
| | //----------------------------------------------------------------- |
---|
| | // |
---|
| | public void init(Acanthus acanthus){ |
---|
| | paintModel.init(acanthus); |
---|
| | } |
---|
| | |
---|
| | // 一定間隔でサーバー上の筆跡を取得する(GET) |
---|
| | public void startObservationOfChangedStroke() { |
---|
| | } |
---|
| | |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | //----------------------------------------------------------------- |
---|
| | // |
---|
| | public void addStroke() { |
---|
| | final StrokesRest strokesRest = retrofit.create(StrokesRest.class); |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // |
---|
| | Call<Integer> call = strokesRest.addStroke( |
---|
| | acanthus.getAid(),acanthus.getPreferenceUid(), acanthus.getPreferenceToken(),0,0,10); |
---|
| | call.enqueue(new Callback<Integer>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<Integer> call, Response<Integer> response) { |
---|
| | if (response.isSuccessful()) { |
---|
| | // Animationにおける |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<Integer> call, Throwable t) { |
---|
| | |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // 座標追加API(POST) |
---|
| | public void addPosition(Acanthus acanthus, float x, float y) { |
---|
| | final StrokesRest strokesRest = retrofit.create(StrokesRest.class); |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // |
---|
| | Call<Integer> call = strokesRest.addPositions(acanthus.getAid(), this.currentStrokeNo, x, y); |
---|
| | call.enqueue(new Callback<Integer>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<Integer> call, Response<Integer> response) { |
---|
| | if (response.isSuccessful()) { |
---|
| | // Animationの座標を更新していく |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<Integer> call, Throwable t) { |
---|
| | |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // 一定間隔でサーバー上の筆跡を取得する(GET) |
---|
| | public void startObservationOfChangedStroke() { |
---|
| | ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(1); |
---|
| | threadPoolExecutor.scheduleWithFixedDelay(this, 1000L, 100L, TimeUnit.MICROSECONDS); |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // 筆跡を取得してローカルデータを更新する |
---|
| | @Override |
---|
| | public void run() { |
---|
| | final StrokesRest strokesRest = retrofit.create(StrokesRest.class); |
---|
| | |
---|
| | Call<Collection<Stroke>> call = strokesRest.getStrokes(acanthus.getAid()); |
---|
| | call.enqueue(new Callback<Collection<Stroke>>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<Collection<Stroke>> call, Response<Collection<Stroke>> response) { |
---|
| | if (response.isSuccessful()) { |
---|
| | // ローカルのページ情報を更新する |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<Collection<Stroke>> call, Throwable t) { |
---|
| | |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | //----------------------------------------------------------------- |
---|
| | } |
---|
| | |
---|
| | |