[add]PaintModelを追加
[update]PaintViewModelを更新
1 parent 532fa10 commit 1945a44966459687a721306777fda72090ab4abc
k-fujii authored on 18 Jun 2021
Showing 2 changed files
View
128
app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintModel.java 0 → 100644
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<Integer> call = strokesRest.addStroke(
// acanthus.getAid(), acanthus.getPreferenceUid(), acanthus.getPreferenceToken(),
// 0, 0, 10);
//
// // strokeNoを更新
// call.enqueue(new Callback<Integer>() {
// @Override
// public void onResponse(Call<Integer> call, Response<Integer> response) {
// if (response.isSuccessful()) setCurrentStrokeNo(response.body());
// }
//
// @Override
// public void onFailure(Call<Integer> call, Throwable t) {
//
// }
// });
}
 
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// ローカルでのストロークの追加(POST)
public void addLocalStroke() {
final StrokesRest strokesRest = retrofit.create(StrokesRest.class);
 
//-----------------------------------------------------------------
// ストローク追加API
Call<Integer> call = strokesRest.addStroke(
acanthus.getAid(), acanthus.getPreferenceUid(), acanthus.getPreferenceToken(),
0, 0, 10);
 
// strokeNoを更新
call.enqueue(new Callback<Integer>() {
@Override
public void onResponse(Call<Integer> call, Response<Integer> response) {
if (response.isSuccessful()) setCurrentStrokeNo(response.body());
}
 
@Override
public void onFailure(Call<Integer> call, Throwable t) {
 
}
});
}
 
//-----------------------------------------------------------------
// ローカルでの筆跡追加(POST)
public void addPosition(float x, float y) {
final StrokesRest strokesRest = retrofit.create(StrokesRest.class);
 
//-----------------------------------------------------------------
// 筆跡追加API
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()) {
 
}
}
 
@Override
public void onFailure(Call<Integer> call, Throwable t) {
 
}
});
}
//-----------------------------------------------------------------
 
}
View
105
app/src/main/java/org/ntlab/acanthus_client/views/paint/PaintViewModel.java
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) {
 
}
});
}
//-----------------------------------------------------------------
}