Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / PostsViewModel.java
package com.example.nemophila.viewmodels;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.nemophila.entities.Post;
import com.example.nemophila.entities.PostJson;
import com.example.nemophila.resources.PostsRest;

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;

public class PostsViewModel extends ViewModel {

    //Field
    final private Retrofit retrofit;
    final private PostsRest postsRest;
    private String pid;

    //LiveData
    final private MutableLiveData<Collection<Post>> accountPostsLiveData;
    final private MutableLiveData<ArrayList<Post>> shopPostsLiveData;

    //テスト用のリスト
    private ArrayList<Post> testPostsList;
    //テスト用のメソッド
    private void createSamplePost() {
        Post post1 = new Post();
        Post post2 = new Post();

        post1.setId("ID1");
        post2.setId("ID2");

        testPostsList.add(post1);
        testPostsList.add(post2);
    }

    //コンストラクタ
    public PostsViewModel() {
        this.accountPostsLiveData = new MutableLiveData<>();
        this.shopPostsLiveData = new MutableLiveData<>();
        this.retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/Nemophila/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        this.postsRest = retrofit.create(PostsRest.class);
        this.pid = null;
        this.testPostsList = new ArrayList<>();
        createSamplePost();
    }

    //getter
    public LiveData<Collection<Post>> getLiveData() {
        return this.accountPostsLiveData;
    }

    //API通信メソッド
    public void getAccountPosts(String uid) {
        Call<Collection<Post>> call = postsRest.getAccountPosts(uid);
        call.enqueue(new Callback<Collection<Post>>() {
            @Override
            public void onResponse(Call<Collection<Post>> call, Response<Collection<Post>> response) {
                if (response.isSuccessful()) {
                    accountPostsLiveData.setValue(response.body());
                } else {
                    //レスポンスエラーを通知
                }
            }

            @Override
            public void onFailure(Call<Collection<Post>> call, Throwable t) {
                //通信エラーを通知
            }
        });
    }

    //投稿作成
    public String createPost(String uid, String token, String sid,
                           String rate, String genre, String comment,
                           String image1, String image2, String image3) {
        Call<String> call = postsRest.postAccountPost(uid ,token, sid, rate, genre, comment, image1, image2, image3);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    pid = response.body();
                } else {
                    //レスポンスエラーを通知
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                //通信エラーを通知
            }
        });
        return pid;
    }

    public void getShopPostJson(String sid) {
        Call<ArrayList<Post>> call = postsRest.getShopPosts(sid);
        call.enqueue(new Callback<ArrayList<Post>>() {
            @Override
            public void onResponse(Call<ArrayList<Post>> call, Response<ArrayList<Post>> response) {
                if (response.isSuccessful()) {
                    shopPostsLiveData.setValue(response.body());
                } else {
                    //レスポンスエラー
                }
            }

            @Override
            public void onFailure(Call<ArrayList<Post>> call, Throwable t) {
                //通信エラー
            }
        });
    }
}