Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / viewmodels / BooksViewModel.java
package com.example.citrusclient.viewmodels;

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

import com.example.citrusclient.models.Book;
import com.example.citrusclient.rest.BooksRest;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

public class BooksViewModel extends ViewModel {

    private final Retrofit retrofit;
    private final BooksRest booksRest;
    private final MutableLiveData<HashMap<Integer, Book>> booksLiveData;
    private final MutableLiveData<Book> book;
    private final MutableLiveData<String> titleLiveData;
    private final MutableLiveData<String> colorLiveData;
    private final MutableLiveData<String> publicityLiveData;
    private final MutableLiveData<String> favoritedLiveData;

    public BooksViewModel() {
        this.retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        this.booksRest = retrofit.create(BooksRest.class);
        this.booksLiveData = new MutableLiveData<>();
        this.titleLiveData = new MutableLiveData<>();
        this.colorLiveData = new MutableLiveData<>();
        this.publicityLiveData = new MutableLiveData<>();
        this.favoritedLiveData = new MutableLiveData<>();
        this.book = new MutableLiveData<>();
    }

    public MutableLiveData<HashMap<Integer, Book>> getBookLiveData() { return this.booksLiveData;}//本の一覧を返す
    public MutableLiveData<String> getTitleLiveData() {return this.titleLiveData;}//本のタイトルを返す
    public MutableLiveData<String> getPublicityLiveData() {return this.publicityLiveData;}//本の公開状態を返す

    public MutableLiveData<Book> getBook() {return this.book;};

    public void createBook(String accountId, String title, String color, Boolean publicity, String token ){
        Call<HashMap<Integer, Book>> call = booksRest.createBook(accountId , title, color, publicity, token);

            call.enqueue(new Callback<HashMap<Integer, Book>>() {
                @Override
                public  void onResponse(Call<HashMap<Integer,Book>> call, Response<HashMap<Integer,Book>> response) {
                    if(response.isSuccessful()) {
                        HashMap<Integer,Book> book = response.body();
                        booksLiveData.setValue(book);
                        System.out.println("success!" + response.body());
                    }else {
                        System.out.println("fail");
                        parseStatusCode(response.code());
                    }
                }
                @Override
                public void onFailure(Call<HashMap<Integer,Book>> call, Throwable t) {
                    System.out.println("NetWorkError" + t);
                }
            });
    }

    public void loadBooks(String accountId, String token){
        Call<HashMap<Integer, Book>> call = booksRest.getBooks(accountId, token);

        call.enqueue(new Callback<HashMap<Integer, Book>>() {
            @Override
            public void onResponse(Call<HashMap<Integer, Book>> call, Response<HashMap<Integer, Book>> response) {
                if (response.isSuccessful()) {
                    HashMap<Integer, Book> book = response.body();
                    booksLiveData.setValue(book);
                    System.out.println(response.code());
                }else System.out.println(response.code());
            }

            @Override
            public void onFailure(Call<HashMap<Integer, Book>> call, Throwable t) {
                System.out.println("NetWorkError" + t);
            }
        });
    }

    public void loadBook(String accountId, String token, int bookId) {
        Call<Book> call = booksRest.getBook(accountId, bookId, token);
        call.enqueue(new Callback<Book>() {
            @Override
            public void onResponse(Call<Book> call, Response<Book> response) {
                if(response.isSuccessful()) {
                    book.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<Book> call, Throwable t) {
                System.out.println("network error");
            }
        });
    }

    public void deleteBook(String accountId, Integer bookId, String token){
        Call<HashMap<Integer, Book>> call = booksRest.deleteBook(accountId, bookId, token);

        call.enqueue(new Callback<HashMap<Integer, Book>>() {
            @Override
            public void onResponse(Call<HashMap<Integer, Book>> call, Response<HashMap<Integer, Book>> response) {
                if (response.isSuccessful()) {
                    HashMap<Integer, Book> book = response.body();
                    book.remove(bookId);
                    booksLiveData.setValue(book);
                    System.out.println("DELETE");
                } else {
                    System.out.println("response error");
                }
            }

            @Override
            public void onFailure(Call<HashMap<Integer, Book>> call, Throwable t) {
                System.out.println("correspondence error" + t);
            }
        });
    }

    public void setTitle(String accountId, Integer bookId, String title, String token){
        Call<String> call = booksRest.putTitle(accountId, bookId, title, token);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()){
                    titleLiveData.setValue(title);
                    System.out.println("Success SetTiTle" + title);
                } else {
                    System.out.println("response error");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError" + t);
            }
        });
    }

    public void setColor(String accountId, Integer bookId, String color, String token){
        Call<String> call = booksRest.putColor(accountId, bookId, color, token);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()){
                    colorLiveData.setValue(color);
                    System.out.println("Success SetTiTle" + color);
                } else {
                    System.out.println("response error");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError" + t);
            }
        });
    }

    public void setPublicity(String accountId, Integer bookId, Boolean publicity, String token){
        Call<String> call = booksRest.putPublicity(accountId, bookId, publicity, token);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()){
                    String pub = String.valueOf(publicity);
                    publicityLiveData.setValue(pub);
                    System.out.println("Success SetTiTle" + publicity);
                } else {
                    System.out.println("response error");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError" + t);
            }
        });
    }

    public void registerFavoriteCount(String accountId, Integer bookId){
        Call<String> call = booksRest.registerFavoriteCount(accountId, bookId);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()){
                    System.out.println("Success registerFavorited");
                } else {
                    System.out.println("response error");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError" + t);
            }
        });
    }

    public void unregisterFavoriteCount(String accountId, Integer bookId){
        Call<String> call = booksRest.unregisterFavoriteCount(accountId, bookId);

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()){
                    System.out.println("Success unresterFavorited");
                } else {
                    System.out.println("response error");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("NetWorkError" + t);
            }
        });
    }

    private String parseStatusCode(Integer code) {
        switch (code) {
            case 404:
                System.out.println("見つかりませんでした");
                return null;
            case 401:
                System.out.println("トークンが違います");
                return null;
            case 400:
                System.out.println("レスポンスエラー");
                return null;
            case 500:
                System.out.println("サーバーエラー");
                return null;
            default:
                System.out.println("不明なエラー");
                return null;
        }
    }
}