Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / viewmodels / BooksViewModel.java
  1. package com.example.citrusclient.viewmodels;
  2.  
  3. import androidx.lifecycle.MutableLiveData;
  4. import androidx.lifecycle.ViewModel;
  5.  
  6. import com.example.citrusclient.models.Book;
  7. import com.example.citrusclient.rest.BooksRest;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11.  
  12. import retrofit2.Call;
  13. import retrofit2.Callback;
  14. import retrofit2.Response;
  15. import retrofit2.Retrofit;
  16. import retrofit2.converter.jackson.JacksonConverterFactory;
  17.  
  18. public class BooksViewModel extends ViewModel {
  19.  
  20. private final Retrofit retrofit;
  21. private final BooksRest booksRest;
  22. private final MutableLiveData<HashMap<Integer, Book>> booksLiveData;
  23. private final MutableLiveData<String> titleLiveData;
  24. private final MutableLiveData<String> colorLiveData;
  25. private final MutableLiveData<String> publicityLiveData;
  26. private final MutableLiveData<String> favoritedLiveData;
  27.  
  28. public BooksViewModel() {
  29. this.retrofit = new Retrofit.Builder()
  30. .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/")
  31. .addConverterFactory(JacksonConverterFactory.create())
  32. .build();
  33. this.booksRest = retrofit.create(BooksRest.class);
  34. this.booksLiveData = new MutableLiveData<>();
  35. this.titleLiveData = new MutableLiveData<>();
  36. this.colorLiveData = new MutableLiveData<>();
  37. this.publicityLiveData = new MutableLiveData<>();
  38. this.favoritedLiveData = new MutableLiveData<>();
  39. }
  40.  
  41. public MutableLiveData<HashMap<Integer, Book>> getBookLiveData() { return this.booksLiveData;}//本の一覧を返す
  42. public MutableLiveData<String> getTitleLiveData() {return this.titleLiveData;}//本のタイトルを返す
  43. public MutableLiveData<String> getPublicityLiveData() {return this.publicityLiveData;}//本の公開状態を返す
  44.  
  45. public void createBook(String accountId, String title, String color, Boolean publicity, String token ){
  46. Call<HashMap<Integer, Book>> call = booksRest.createBook(accountId , title, color, publicity, token);
  47.  
  48. call.enqueue(new Callback<HashMap<Integer, Book>>() {
  49. @Override
  50. public void onResponse(Call<HashMap<Integer,Book>> call, Response<HashMap<Integer,Book>> response) {
  51. if(response.isSuccessful()) {
  52. HashMap<Integer,Book> book = response.body();
  53. booksLiveData.setValue(book);
  54. System.out.println("success!" + response.body());
  55. }else {
  56. System.out.println("fail");
  57. parseStatusCode(response.code());
  58. }
  59. }
  60. @Override
  61. public void onFailure(Call<HashMap<Integer,Book>> call, Throwable t) {
  62. System.out.println("NetWorkError" + t);
  63. }
  64. });
  65. }
  66.  
  67. public void loadBooks(String accountId, String token){
  68. Call<HashMap<Integer, Book>> call = booksRest.getBooks(accountId, token);
  69.  
  70. call.enqueue(new Callback<HashMap<Integer, Book>>() {
  71. @Override
  72. public void onResponse(Call<HashMap<Integer, Book>> call, Response<HashMap<Integer, Book>> response) {
  73. if (response.isSuccessful()) {
  74. HashMap<Integer, Book> book = response.body();
  75. booksLiveData.setValue(book);
  76. System.out.println(response.code());
  77. }else System.out.println(response.code());
  78. }
  79.  
  80. @Override
  81. public void onFailure(Call<HashMap<Integer, Book>> call, Throwable t) {
  82. System.out.println("NetWorkError" + t);
  83. }
  84. });
  85. }
  86.  
  87. public void deleteBook(String accountId, Integer bookId, String token){
  88. Call<HashMap<Integer, Book>> call = booksRest.deleteBook(accountId, bookId, token);
  89.  
  90. call.enqueue(new Callback<HashMap<Integer, Book>>() {
  91. @Override
  92. public void onResponse(Call<HashMap<Integer, Book>> call, Response<HashMap<Integer, Book>> response) {
  93. if (response.isSuccessful()) {
  94. HashMap<Integer, Book> book = response.body();
  95. book.remove(bookId);
  96. booksLiveData.setValue(book);
  97. System.out.println("DELETE");
  98. } else {
  99. System.out.println("response error");
  100. }
  101. }
  102.  
  103. @Override
  104. public void onFailure(Call<HashMap<Integer, Book>> call, Throwable t) {
  105. System.out.println("correspondence error" + t);
  106. }
  107. });
  108. }
  109.  
  110. public void setTitle(String accountId, Integer bookId, String title, String token){
  111. Call<String> call = booksRest.putTitle(accountId, bookId, title, token);
  112.  
  113. call.enqueue(new Callback<String>() {
  114. @Override
  115. public void onResponse(Call<String> call, Response<String> response) {
  116. if (response.isSuccessful()){
  117. titleLiveData.setValue(title);
  118. System.out.println("Success SetTiTle" + title);
  119. } else {
  120. System.out.println("response error");
  121. }
  122. }
  123.  
  124. @Override
  125. public void onFailure(Call<String> call, Throwable t) {
  126. System.out.println("NetWorkError" + t);
  127. }
  128. });
  129. }
  130.  
  131. public void setColor(String accountId, Integer bookId, String color, String token){
  132. Call<String> call = booksRest.putColor(accountId, bookId, color, token);
  133.  
  134. call.enqueue(new Callback<String>() {
  135. @Override
  136. public void onResponse(Call<String> call, Response<String> response) {
  137. if (response.isSuccessful()){
  138. colorLiveData.setValue(color);
  139. System.out.println("Success SetTiTle" + color);
  140. } else {
  141. System.out.println("response error");
  142. }
  143. }
  144.  
  145. @Override
  146. public void onFailure(Call<String> call, Throwable t) {
  147. System.out.println("NetWorkError" + t);
  148. }
  149. });
  150. }
  151.  
  152. public void setPublicity(String accountId, Integer bookId, Boolean publicity, String token){
  153. Call<String> call = booksRest.putPublicity(accountId, bookId, publicity, token);
  154.  
  155. call.enqueue(new Callback<String>() {
  156. @Override
  157. public void onResponse(Call<String> call, Response<String> response) {
  158. if (response.isSuccessful()){
  159. String pub = String.valueOf(publicity);
  160. publicityLiveData.setValue(pub);
  161. System.out.println("Success SetTiTle" + publicity);
  162. } else {
  163. System.out.println("response error");
  164. }
  165. }
  166.  
  167. @Override
  168. public void onFailure(Call<String> call, Throwable t) {
  169. System.out.println("NetWorkError" + t);
  170. }
  171. });
  172. }
  173.  
  174. private String parseStatusCode(Integer code) {
  175. switch (code) {
  176. case 404:
  177. System.out.println("見つかりませんでした");
  178. return null;
  179. case 401:
  180. System.out.println("トークンが違います");
  181. return null;
  182. case 400:
  183. System.out.println("レスポンスエラー");
  184. return null;
  185. case 500:
  186. System.out.println("サーバーエラー");
  187. return null;
  188. default:
  189. System.out.println("不明なエラー");
  190. return null;
  191. }
  192. }
  193. }