| | package org.ntlab.acanthus_client.views.paint.models; |
---|
| | |
---|
| | import android.util.Log; |
---|
| | |
---|
| | import androidx.lifecycle.MutableLiveData; |
---|
| | import androidx.navigation.ActivityNavigator; |
---|
| | |
---|
| | import org.ntlab.acanthus_client.Acanthus; |
---|
| | import org.ntlab.acanthus_client.entities.Animation; |
---|
| | import org.ntlab.acanthus_client.entities.Position; |
---|
| | import org.ntlab.acanthus_client.entities.Stroke; |
---|
| | import org.ntlab.acanthus_client.resources.gallery.GalleryRest; |
---|
| | import org.ntlab.acanthus_client.resources.gallery.StrokesRest; |
---|
| | |
---|
| | import java.lang.reflect.Array; |
---|
| | import java.util.ArrayList; |
---|
| | import java.util.Collection; |
---|
| | |
---|
| | import retrofit2.Call; |
---|
| | import retrofit2.Callback; |
---|
| | import retrofit2.Response; |
---|
| | import retrofit2.Retrofit; |
---|
| | import retrofit2.converter.jackson.JacksonConverterFactory; |
---|
| | import retrofit2.converter.scalars.ScalarsConverterFactory; |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // 描画通信 |
---|
| | public class PaintConnectionModel { |
---|
| | private Animation editedAnimation; |
---|
| | private Integer currentStrokeNo; |
---|
| | private Retrofit retrofit; |
---|
| | private Acanthus acanthus; |
---|
| | private Collection getPaint; |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | //----------------------------------------------------------------- |
---|
| | // |
---|
| | public PaintConnectionModel(Acanthus acanthus) { |
---|
| | init(acanthus); |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // 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; |
---|
| | this.getPaint = new ArrayList(); |
---|
| | this.retrofit = new Retrofit.Builder() |
---|
| | .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") |
---|
| | .addConverterFactory(ScalarsConverterFactory.create()) |
---|
| | .addConverterFactory(JacksonConverterFactory.create()) |
---|
| | .build(); |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | //----------------------------------------------------------------- |
---|
| | // ローカルでのストロークの追加(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) { |
---|
| | |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<Integer> call, Throwable t) { |
---|
| | |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | //Getを追加 |
---|
| | public void apiGetPosition(MutableLiveData<Collection<Position>> paintPosition, int num) { |
---|
| | final StrokesRest strokesRest = retrofit.create(StrokesRest.class); |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // 筆跡追加API |
---|
| | Call<Collection<Position>> call = strokesRest.getPositions(acanthus.getAid(), num); |
---|
| | call.enqueue(new Callback<Collection<Position>>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<Collection<Position>> call, Response<Collection<Position>> response) { |
---|
| | if (response.isSuccessful()) paintPosition.setValue(response.body()); |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<Collection<Position>> call, Throwable t) { |
---|
| | |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | //Getを追加 |
---|
| | public void getStrokeSize(MutableLiveData<Collection<Stroke>> strokes) { |
---|
| | final StrokesRest strokesRest = retrofit.create(StrokesRest.class); |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // 筆跡追加API |
---|
| | 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()) strokes.setValue(response.body()); |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<Collection<Stroke>> call, Throwable t) { |
---|
| | |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | } |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | //DELETE |
---|
| | public void deleteStrokes() { |
---|
| | final StrokesRest strokesRest = retrofit.create(StrokesRest.class); |
---|
| | |
---|
| | //----------------------------------------------------------------- |
---|
| | // 筆跡をすべて削除する |
---|
| | Call<String> call = strokesRest.deleteStrokes(acanthus.getAid(), acanthus.getPreferenceUid()); |
---|
| | call.enqueue(new Callback<String>() { |
---|
| | @Override |
---|
| | public void onResponse(Call<String> call, Response<String> response) { |
---|
| | } |
---|
| | |
---|
| | @Override |
---|
| | public void onFailure(Call<String> call, Throwable t) { |
---|
| | } |
---|
| | }); |
---|
| | } |
---|
| | //----------------------------------------------------------------- |
---|
| | |
---|
| | } |
---|
| | |