Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / AccountViewModel.java
  1. package com.example.nemophila.viewmodels;
  2.  
  3. import android.util.Log;
  4. import android.widget.TextView;
  5.  
  6. import com.example.nemophila.R;
  7. import com.example.nemophila.entities.Account;
  8. import com.example.nemophila.entities.AccountJson;
  9. import com.example.nemophila.entities.AccountNameJson;
  10. import com.example.nemophila.entities.ErrorType;
  11. import com.example.nemophila.entities.Post;
  12. import com.example.nemophila.entities.PostJson;
  13. import com.example.nemophila.resources.AccountsRest;
  14.  
  15. import androidx.lifecycle.MutableLiveData;
  16. import androidx.lifecycle.ViewModel;
  17.  
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21.  
  22. import retrofit2.Call;
  23. import retrofit2.Callback;
  24. import retrofit2.Response;
  25. import retrofit2.Retrofit;
  26. import retrofit2.converter.jackson.JacksonConverterFactory;
  27.  
  28. public class AccountViewModel extends ViewModel {
  29. // フィールド
  30. private final Retrofit retrofit;
  31. private final AccountsRest accountsRest;
  32.  
  33. // ライブデータ
  34. private final MutableLiveData<String> nameLiveData;
  35. private final MutableLiveData<String> pwLiveData;
  36. private final MutableLiveData<Collection<Post>> accountPostsLiveData;
  37. private final MutableLiveData<String> pwErrorLiveData;
  38. private final MutableLiveData<Account> accountLiveData;
  39. private final MutableLiveData<String> errorLiveData;
  40.  
  41. // コンストラクタ
  42. public AccountViewModel() {
  43. this.retrofit = new Retrofit.Builder()
  44. .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/nemophila/")
  45. .addConverterFactory(JacksonConverterFactory.create())
  46. .build();
  47. this.accountsRest = retrofit.create(AccountsRest.class);
  48. this.nameLiveData = new MutableLiveData<>();
  49. this.pwLiveData = new MutableLiveData<>();
  50. this.accountPostsLiveData = new MutableLiveData<>();
  51. this.pwErrorLiveData = new MutableLiveData<>();
  52. this.accountLiveData = new MutableLiveData<>();
  53. this.errorLiveData = new MutableLiveData<>();
  54. }
  55.  
  56. // ライブデータの取得(ゲッター)
  57. public MutableLiveData<String> getNameLiveData() {
  58. return nameLiveData;
  59. }
  60. public MutableLiveData<String> getPwLiveData() {
  61. return pwLiveData;
  62. }
  63. public MutableLiveData<Collection<Post>> getAccountPostsLiveData() { return accountPostsLiveData; }
  64. public MutableLiveData<String> getPwErrorLiveData() {
  65. return pwErrorLiveData;
  66. }
  67. public MutableLiveData<Account> getAccountLiveData() {
  68. return accountLiveData;
  69. }
  70. public MutableLiveData<String> getErrorLiveData() {
  71. return errorLiveData;
  72. }
  73.  
  74. // 対象のアカウント情報の削除
  75. public void deleteAccount(String uid, String token) {
  76. Call<Void> call = accountsRest.deleteAccount(uid, token);
  77.  
  78. call.enqueue(new Callback<Void>() {
  79. @Override
  80. public void onResponse(Call<Void> call, Response<Void> response) {
  81. if (response.isSuccessful()) {
  82. System.out.println("DeleteAccount Successful");
  83. } else {
  84. System.out.println("DeleteAccount ResponseError");
  85. }
  86. }
  87. @Override
  88. public void onFailure(Call<Void> call, Throwable t) {
  89. System.out.println("DeleteAccount NetworkError" + t);
  90. }
  91. });
  92. }
  93.  
  94. // 対象のアカウントパスワードの変更
  95. public void changePw(String uid, String oldPw, String newPw, String token) {
  96. Call<Void> call = accountsRest.changePw(uid, oldPw, newPw, token);
  97.  
  98. call.enqueue(new Callback<Void>() {
  99. @Override
  100. public void onResponse(Call<Void> call, Response<Void> response) {
  101. if (response.isSuccessful()) {
  102. pwLiveData.setValue(newPw);
  103. System.out.println(response.code());
  104. System.out.println("Success ChangePW");
  105. } else {
  106. if (response.code() == 401){
  107. pwErrorLiveData.setValue("error");
  108. System.out.println("miss ChangePW");
  109. }
  110.  
  111. }
  112. }
  113. @Override
  114. public void onFailure(Call<Void> call, Throwable t) {
  115. System.out.println("ChangePw NetworkError" + t);
  116. }
  117. });
  118. }
  119.  
  120. // 対象のアカウント名の変更
  121. public void changeName(String uid, String name, String token) {
  122. Call<Void> call = accountsRest.changeName(uid, name, token);
  123.  
  124. call.enqueue(new Callback<Void>() {
  125. @Override
  126. public void onResponse(Call<Void> call, Response<Void> response) {
  127. if (response.isSuccessful()) {
  128. nameLiveData.setValue(name);
  129. System.out.println("Success ChangeName:" + name);
  130. } else {
  131. System.out.println("response error");
  132. }
  133. }
  134. @Override
  135. public void onFailure(Call<Void> call, Throwable t) {
  136. System.out.println("ChangeName NetWorkError" + t);
  137. }
  138. });
  139. }
  140.  
  141. // 対象のアカウントがした投稿の全取得
  142. public void getAccountPosts(String uid) {
  143. Call<Collection<PostJson>> call = accountsRest.getAccountPosts(uid);
  144.  
  145. call.enqueue(new Callback<Collection<PostJson>>() {
  146. @Override
  147. public void onResponse(Call<Collection<PostJson>> call, Response<Collection<PostJson>> response) {
  148. if (response.isSuccessful()) {
  149. Collection<PostJson> postJson = response.body();
  150. setAccountPostLiveDataFromJson(postJson);
  151. System.out.println("Success" + accountPostsLiveData.getValue().toString());
  152. } else {
  153. System.out.println("response error");
  154. }
  155. }
  156. @Override
  157. public void onFailure(Call<Collection<PostJson>> call, Throwable t) {
  158. System.out.println("correspondence error" + t);
  159. }
  160. });
  161. }
  162.  
  163. //PostJsonからPostを作成し,対象のライブデータに設定する
  164. private void setAccountPostLiveDataFromJson(Collection<PostJson> postJson) {
  165. ArrayList<Post> posts = new ArrayList<>();
  166. for(PostJson pj: postJson) {
  167. Post post = new Post(pj);
  168. posts.add(post);
  169. }
  170. accountPostsLiveData.setValue(posts);
  171. }
  172.  
  173. //idからアカウント情報を取得するメソッド
  174. public void fetchAccount(String id) {
  175. Call<AccountJson> call = accountsRest.getAccount(id);
  176.  
  177. call.enqueue(new Callback<AccountJson>() {
  178. @Override
  179. public void onResponse(Call<AccountJson> call, Response<AccountJson> response) {
  180. if (response.isSuccessful()) {
  181. if(response.body() == null){
  182. accountLiveData.setValue(null);
  183. System.out.println("response error");
  184. }else {
  185. Account ac = new Account(response.body());
  186. accountLiveData.setValue(ac);
  187. System.out.println("success");
  188. }
  189. } else {
  190. accountLiveData.setValue(null);
  191. System.out.println("response error");
  192. }
  193. }
  194.  
  195. @Override
  196. public void onFailure(Call<AccountJson> call, Throwable t) {
  197. System.out.println("network error" + t);
  198. }
  199. });
  200. }
  201.  
  202. //同期的にidからアカウント情報を取得するメソッド
  203. public Account sample(String id) {
  204. try {
  205. Response<AccountJson> response = accountsRest.getAccount(id).execute();
  206.  
  207. if (response.isSuccessful()) {
  208. Account account = new Account(response.body());
  209. return account;
  210. } else {
  211. Log.d("message", "error" + response.code());
  212. }
  213.  
  214. } catch (IOException e) {
  215. Log.i("message", "error" + e.getMessage());
  216. }
  217. return null;
  218. }
  219.  
  220. // 対象のアカウントがした投稿の削除
  221. public void deleteAccountPost(String uid, String pid, String token) {
  222. Call<Void> call = accountsRest.deletePost(uid, pid, token);
  223.  
  224. call.enqueue(new Callback<Void>() {
  225. @Override
  226. public void onResponse(Call<Void> call, Response<Void> response) {
  227. if (response.isSuccessful()) {
  228. deleteAccountPostLiveData(pid);
  229. System.out.println("successful");
  230. } else {
  231. System.out.println("response error");
  232. }
  233. }
  234. @Override
  235. public void onFailure(Call<Void> call, Throwable t) {
  236. System.out.println("correspondence error" + t);
  237. }
  238. });
  239. }
  240.  
  241. private void deleteAccountPostLiveData(String id) {
  242. ArrayList<Post> preData = new ArrayList<>();
  243. for (Post post: accountPostsLiveData.getValue()) {
  244. if (post.getPid().equals(id)) {
  245. continue;
  246. }
  247. preData.add(post);
  248. }
  249. accountPostsLiveData.setValue(preData);
  250. }
  251.  
  252. private String parseStatusCode(Integer stats) {
  253. switch (stats) {
  254. case 404:
  255. return ErrorType.ResponseNoyFound.getText();
  256. case 401:
  257. return ErrorType.InvalidToken.getText();
  258. case 400:
  259. return ErrorType.ResponseError.getText();
  260. default:
  261. return ErrorType.UnknownError.getText();
  262. }
  263. }
  264. }