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<HashMap<Integer,Book>> booksIdLiveData;
  24. private final MutableLiveData<String> titleLiveData;
  25. private final MutableLiveData<String> colorLiveData;
  26. private final MutableLiveData<String> publicityLiveData;
  27. private final MutableLiveData<String> favoritedLiveData;
  28. private final MutableLiveData<String> errorLiveData;
  29. private final MutableLiveData<Boolean> successDeleteBookLiveData;
  30. private final MutableLiveData<String> delBookErrorLivedata;
  31.  
  32. public BooksViewModel(MutableLiveData<HashMap<Integer, Book>> booksIdLiveData) {
  33. this.retrofit = new Retrofit.Builder()
  34. .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/")
  35. .addConverterFactory(JacksonConverterFactory.create())
  36. .build();
  37. this.booksRest = retrofit.create(BooksRest.class);
  38. this.booksLiveData = new MutableLiveData<>();
  39. this.booksIdLiveData = booksIdLiveData;
  40. this.titleLiveData = new MutableLiveData<>();
  41. this.colorLiveData = new MutableLiveData<>();
  42. this.publicityLiveData = new MutableLiveData<>();
  43. this.favoritedLiveData = new MutableLiveData<>();
  44. this.errorLiveData = new MutableLiveData<>();
  45. this.successDeleteBookLiveData = new MutableLiveData<>();
  46. this.delBookErrorLivedata = new MutableLiveData<>();
  47. }
  48.  
  49. public MutableLiveData<HashMap<Integer, Book>> getBookLiveData() { return this.booksLiveData;}//本の一覧を返す
  50. public MutableLiveData<HashMap<Integer,Book>> getBooksIdLiveData() {return this.booksIdLiveData;}//本の情報を取得
  51. public MutableLiveData<String> getTitleLiveData() {return this.titleLiveData;}//本のタイトルを返す
  52. public MutableLiveData<String> getPublicityLiveData() {return this.publicityLiveData;}//本の公開状態を返す
  53.  
  54.  
  55. public MutableLiveData<Boolean> getSuccessDeleteBookLiveData() {return successDeleteBookLiveData;}
  56.  
  57. public void createBook(String accountId, String title, String color, Boolean publicity, String token ){
  58. Call<Book> call = booksRest.createBook(accountId , title, color, publicity, token);
  59.  
  60. call.enqueue(new Callback<Book>() {
  61. @Override
  62. public void onResponse(Call<Book> call, Response<Book> response) {
  63. if(response.isSuccessful()) {
  64. System.out.println("success!" + response.body());
  65. }else {
  66. System.out.println("fail");
  67. errorLiveData.setValue(parseStatusCode(response.code()));
  68. }
  69. }
  70.  
  71. @Override
  72. public void onFailure(Call<Book> call, Throwable t) {
  73.  
  74. }
  75. });
  76.  
  77. }
  78.  
  79. public void loadBooks(String account_id, String token){
  80. Call<HashMap<Integer, Book>> call = booksRest.getBooks(account_id, token);
  81.  
  82. call.enqueue(new Callback<HashMap<Integer, Book>>() {
  83. @Override
  84. public void onResponse(Call<HashMap<Integer, Book>> call, Response<HashMap<Integer, Book>> response) {
  85. if (response.isSuccessful()) {
  86. HashMap<Integer, Book> book = response.body();
  87. booksLiveData.setValue(book);
  88. System.out.println(response.code());
  89. }else System.out.println(response.code());
  90. }
  91.  
  92. @Override
  93. public void onFailure(Call<HashMap<Integer, Book>> call, Throwable t) {
  94. System.out.println("fail");
  95. }
  96. });
  97. }
  98.  
  99. public void deleteBook(String account_id, Integer book_id, String token){
  100. Call<String> call = booksRest.deleteBook(account_id, book_id, token);
  101.  
  102. call.enqueue(new Callback<String>() {
  103. @Override
  104. public void onResponse(Call<String> call, Response<String> response) {
  105. if (response.isSuccessful()) {
  106. successDeleteBookLiveData.setValue(true);
  107. System.out.println("DELETE");
  108. } else {
  109. System.out.println("response error");
  110. }
  111. }
  112.  
  113. @Override
  114. public void onFailure(Call<String> call, Throwable t) {
  115. System.out.println("correspondence error" + t);
  116. }
  117. });
  118. }
  119.  
  120. /*private void deleteBookLiveData(String account_id, Integer book_id) {
  121. HashMap<Integer,Book> delData = new HashMap<>();
  122. for (Book book: booksLiveData.getValue()) {
  123. if (book.getBookId().equals(book_id)) {
  124. continue;
  125. }
  126. delData.add(book);
  127. }
  128. booksLiveData.setValue(delData);
  129. }*/
  130.  
  131. public void setTitle(String account_id, Integer book_id, String title, String token){
  132. Call<String> call = booksRest.putTitle(account_id, book_id, title, 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. titleLiveData.setValue(title);
  139. System.out.println(response.code());
  140. System.out.println("Success SetTiTle" + title);
  141. } else {
  142. System.out.println("response error");
  143. }
  144. }
  145.  
  146. @Override
  147. public void onFailure(Call<String> call, Throwable t) {
  148. System.out.println("NetWorkError" + t);
  149. }
  150. });
  151. }
  152.  
  153. public void setColor(String account_id, Integer book_id, String color, String token){
  154. Call<String> call = booksRest.putColor(account_id, book_id, color, token);
  155.  
  156. call.enqueue(new Callback<String>() {
  157. @Override
  158. public void onResponse(Call<String> call, Response<String> response) {
  159. if (response.isSuccessful()){
  160. colorLiveData.setValue(color);
  161. System.out.println(response.code());
  162. System.out.println("Success SetTiTle" + color);
  163. } else {
  164. System.out.println("response error");
  165. }
  166. }
  167.  
  168. @Override
  169. public void onFailure(Call<String> call, Throwable t) {
  170. System.out.println("NetWorkError" + t);
  171. }
  172. });
  173. }
  174.  
  175. public void setPublicity(String account_id, Integer book_id, Boolean publicity, String token){
  176. Call<String> call = booksRest.putPublicity(account_id, book_id, publicity, token);
  177.  
  178. call.enqueue(new Callback<String>() {
  179. @Override
  180. public void onResponse(Call<String> call, Response<String> response) {
  181. if (response.isSuccessful()){
  182. String pub = String.valueOf(publicity);
  183. publicityLiveData.setValue(pub);
  184. System.out.println(response.code());
  185. System.out.println("Success SetTiTle" + publicity);
  186. } else {
  187. System.out.println("response error");
  188. }
  189. }
  190.  
  191. @Override
  192. public void onFailure(Call<String> call, Throwable t) {
  193. System.out.println("NetWorkError" + t);
  194. }
  195. });
  196. }
  197.  
  198. private String parseStatusCode(Integer code) {
  199. switch (code) {
  200. case 404:
  201. System.out.println("見つかりませんでした");
  202. return null;
  203. case 401:
  204. System.out.println("トークンが違います");
  205. return null;
  206. case 400:
  207. System.out.println("レスポンスエラー");
  208. return null;
  209. case 500:
  210. System.out.println("サーバーエラー");
  211. return null;
  212. default:
  213. System.out.println("不明なエラー");
  214. return null;
  215. }
  216. }
  217. }