diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintModel.java b/app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintModel.java new file mode 100644 index 0000000..d03cf13 --- /dev/null +++ b/app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintModel.java @@ -0,0 +1,127 @@ +package org.ntlab.acanthus_client.views.paint; + +import androidx.navigation.ActivityNavigator; + +import org.ntlab.acanthus_client.Acanthus; +import org.ntlab.acanthus_client.entities.Animation; +import org.ntlab.acanthus_client.resources.gallery.GalleryRest; +import org.ntlab.acanthus_client.resources.gallery.StrokesRest; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; +import retrofit2.converter.scalars.ScalarsConverterFactory; + +//----------------------------------------------------------------- +// +public class PaintModel { + private Animation editedAnimation; + private Acanthus acanthus; + private Integer currentStrokeNo; + + private Retrofit retrofit; + + //----------------------------------------------------------------- + //----------------------------------------------------------------- + // + public PaintModel() { + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") + .addConverterFactory(ScalarsConverterFactory.create()) + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + } + + + //----------------------------------------------------------------- + // setter + public void setEditedAnimation(Animation editedAnimation) { + this.editedAnimation = editedAnimation; + } + + private void setCurrentStrokeNo(int strokeNo) { + this.currentStrokeNo = strokeNo; + } + + //----------------------------------------------------------------- + //----------------------------------------------------------------- + // init + public void init(Acanthus acanthus){ + this.acanthus = acanthus; + + final GalleryRest galleryRest = retrofit.create(GalleryRest.class); +// //----------------------------------------------------------------- +// // ストローク追加API +// Call call = strokesRest.addStroke( +// acanthus.getAid(), acanthus.getPreferenceUid(), acanthus.getPreferenceToken(), +// 0, 0, 10); +// +// // strokeNoを更新 +// call.enqueue(new Callback() { +// @Override +// public void onResponse(Call call, Response response) { +// if (response.isSuccessful()) setCurrentStrokeNo(response.body()); +// } +// +// @Override +// public void onFailure(Call call, Throwable t) { +// +// } +// }); + } + + //----------------------------------------------------------------- + //----------------------------------------------------------------- + // ローカルでのストロークの追加(POST) + public void addLocalStroke() { + final StrokesRest strokesRest = retrofit.create(StrokesRest.class); + + //----------------------------------------------------------------- + // ストローク追加API + Call call = strokesRest.addStroke( + acanthus.getAid(), acanthus.getPreferenceUid(), acanthus.getPreferenceToken(), + 0, 0, 10); + + // strokeNoを更新 + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) setCurrentStrokeNo(response.body()); + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } + + //----------------------------------------------------------------- + // ローカルでの筆跡追加(POST) + public void addPosition(float x, float y) { + final StrokesRest strokesRest = retrofit.create(StrokesRest.class); + + //----------------------------------------------------------------- + // 筆跡追加API + Call call = strokesRest.addPositions(acanthus.getAid(), this.currentStrokeNo, x, y); + + // 筆跡を更新 + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) { + + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } + //----------------------------------------------------------------- + +} diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintViewModel.java b/app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintViewModel.java index 1c453cf..18fd869 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintViewModel.java +++ b/app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintViewModel.java @@ -21,6 +21,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import kotlinx.coroutines.sync.Mutex; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -31,103 +32,28 @@ //----------------------------------------------------------------- // -public class PaintViewModel extends ViewModel implements Runnable { +public class PaintViewModel extends ViewModel { private MutableLiveData 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 addStroke() { - final StrokesRest strokesRest = retrofit.create(StrokesRest.class); - - //----------------------------------------------------------------- - // - Call call = strokesRest.addStroke( - acanthus.getAid(),acanthus.getPreferenceUid(), acanthus.getPreferenceToken(),0,0,10); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - // Animationにおける - } - } - - @Override - public void onFailure(Call call, Throwable t) { - - } - }); - - + public void init(Acanthus acanthus){ + paintModel.init(acanthus); } - //----------------------------------------------------------------- - // 座標追加API(POST) - public void addPosition(Acanthus acanthus, float x, float y) { - final StrokesRest strokesRest = retrofit.create(StrokesRest.class); - - //----------------------------------------------------------------- - // - Call call = strokesRest.addPositions(acanthus.getAid(), this.currentStrokeNo, x, y); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - // Animationの座標を更新していく - } - } - - @Override - public void onFailure(Call 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> call = strokesRest.getStrokes(acanthus.getAid()); - call.enqueue(new Callback>() { - @Override - public void onResponse(Call> call, Response> response) { - if (response.isSuccessful()) { - // ローカルのページ情報を更新する - } - } - - @Override - public void onFailure(Call> call, Throwable t) { - - } - }); - } //----------------------------------------------------------------- }