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.ErrorType;
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;

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

    //コンストラクタ
    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.errorLiveData = new MutableLiveData<>();
    }

    //getter
    public LiveData<Collection<Post>> getAccountPostLiveData() {
        return this.accountPostsLiveData;
    }
    public LiveData<ArrayList<Post>> getShopPostLiveData() {
        return this.shopPostsLiveData;
    }
    public MutableLiveData<String> getErrorLiveData() {
        return errorLiveData;
    }


    //AccountのpostJsonを取得
    public void loadAccountPosts(String uid) {
        Call<Collection<PostJson>> call = postsRest.getAccountPosts(uid);
        call.enqueue(new Callback<Collection<PostJson>>() {
            @Override
            public void onResponse(Call<Collection<PostJson>> call, Response<Collection<PostJson>> response) {
                if (response.isSuccessful()) {
                    setAccountPostLiveData(response.body());
                } else {
                    System.out.println("AccountPosts ResponseError");
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }

            @Override
            public void onFailure(Call<Collection<PostJson>> call, Throwable t) {
                System.out.println("AccountPosts NetWorkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }

    //PostJsonからPostを作成し,それをLiveDataにセット
    private void setAccountPostLiveData(Collection<PostJson> postJson) {
        ArrayList<Post> posts = new ArrayList<>();
        for(PostJson pj: postJson) {
            Post post = new Post(pj);
            posts.add(post);
        }
        accountPostsLiveData.setValue(posts);
    }

    //ShopのPostJsonを取得
    public void loadShopPost(String sid) {
        Call<ArrayList<PostJson>> call = postsRest.getShopPosts(sid);
        call.enqueue(new Callback<ArrayList<PostJson>>() {
            @Override
            public void onResponse(Call<ArrayList<PostJson>> call, Response<ArrayList<PostJson>> response) {
                if (response.isSuccessful()) {
                    ArrayList<PostJson> postJson = response.body();
                    setShopPostLiveData(postJson);
                } else {
                    System.out.println("ShopPosts ResponseError");
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }

            @Override
            public void onFailure(Call<ArrayList<PostJson>> call, Throwable t) {
                System.out.println("ShopPosts NetWorkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }
    //PostJsonからPostを作成し,それをLiveDataにセット
    private void setShopPostLiveData(ArrayList<PostJson> postJson) {
        ArrayList<Post> posts = new ArrayList<>();
        for(PostJson pj: postJson) {
            Post post = new Post(pj);
            posts.add(post);
        }
        shopPostsLiveData.setValue(posts);
    }

    //投稿作成
    public void createPost(String uid, String token, String sid,
                           int 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()) {
                    System.out.println("Success CreatePost" + response.body());
                } else {
                    System.out.println("CreatePosts ResponseError");
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("CreatePosts NetWorkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }

    public ArrayList<String> getUserIds() {
        ArrayList<String> ids = new ArrayList<>();
        for(Post post: shopPostsLiveData.getValue()) {
            ids.add(post.getUid());
        }
        return ids;
    }

    private String parseStatusCode(Integer stats) {
        switch (stats) {
            case 404:
                return ErrorType.ResponseNotFound.getText();
            case 401:
                return ErrorType.InvalidToken.getText();
            case 400:
                return ErrorType.ResponseError.getText();
            case 500:
                return ErrorType.ServerError.getText();
            default:
                return ErrorType.UnknownError.getText();
        }
    }
}