Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / FriendViewModel.java
  1. package com.example.nemophila.viewmodels;
  2.  
  3. import androidx.lifecycle.MutableLiveData;
  4. import androidx.lifecycle.ViewModel;
  5.  
  6. import com.example.nemophila.entities.AccountNameJson;
  7. import com.example.nemophila.entities.Post;
  8. import com.example.nemophila.entities.PostJson;
  9. import com.example.nemophila.resources.AccountsRest;
  10. import com.example.nemophila.resources.FriendsRest;
  11.  
  12. import java.util.Collection;
  13.  
  14. import retrofit2.Call;
  15. import retrofit2.Callback;
  16. import retrofit2.Response;
  17. import retrofit2.Retrofit;
  18. import retrofit2.converter.jackson.JacksonConverterFactory;
  19.  
  20. public class FriendViewModel extends ViewModel {
  21. // フィールド
  22. private final Retrofit retrofit;
  23. private final FriendsRest friendsRest;
  24. // ライブデータ
  25. private final MutableLiveData<Collection<AccountNameJson>> friendsLiveData;
  26. private final MutableLiveData<Collection<AccountNameJson>> requestedLiveData;
  27. private final MutableLiveData<Collection<AccountNameJson>> requestingLiveData;
  28.  
  29. // コンストラクタ
  30. public FriendViewModel() {
  31. this.retrofit = new Retrofit.Builder()
  32. .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/nemophila/")
  33. .addConverterFactory(JacksonConverterFactory.create())
  34. .build();
  35. this.friendsRest = retrofit.create(FriendsRest.class);
  36. this.friendsLiveData = new MutableLiveData<>();
  37. this.requestedLiveData = new MutableLiveData<>();
  38. this.requestingLiveData = new MutableLiveData<>();
  39. }
  40.  
  41. // ライブデータの取得(ゲッター)
  42. public MutableLiveData<Collection<AccountNameJson>> getFriendsLiveData() {
  43. return friendsLiveData;
  44. }
  45. public MutableLiveData<Collection<AccountNameJson>> getRequestedLiveData() {
  46. return requestedLiveData;
  47. }
  48. public MutableLiveData<Collection<AccountNameJson>> getRequestingLiveData() { return requestingLiveData; }
  49.  
  50.  
  51. //フレンドの情報の取得
  52. public void getFriends(String uid) {
  53. Call<Collection<AccountNameJson>> call = friendsRest.getFriends(uid);
  54. call.enqueue(new Callback<Collection<AccountNameJson>>() {
  55. @Override
  56. public void onResponse(Call<Collection<AccountNameJson>> call, Response<Collection<AccountNameJson>> response) {
  57. if (response.isSuccessful()) {
  58. System.out.println("Successful");
  59. Collection<AccountNameJson> accountNameJson = response.body();
  60. friendsLiveData.setValue(accountNameJson);
  61.  
  62. } else {
  63. System.out.println("ResponseError");
  64. }
  65. }
  66. @Override
  67. public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
  68. System.out.println(" NetworkError" + t);
  69. }
  70. });
  71. }
  72.  
  73. public void putFriend(String uid,String fid, String token) {
  74. Call<Void> call = friendsRest.putFriend(uid,fid,token);
  75. call.enqueue(new Callback<Void>() {
  76. @Override
  77. public void onResponse(Call<Void> call, Response<Void> response) {
  78. if (response.isSuccessful()) {
  79. System.out.println("Successful");
  80. } else {
  81. System.out.println("ResponseError");
  82. }
  83. }
  84. @Override
  85. public void onFailure(Call<Void> call, Throwable t) {
  86. System.out.println(" NetworkError" + t);
  87. }
  88. });
  89. }
  90. // 対象のアカウントのフレンド登録情報の削除
  91. public void deleteFriend(String uid,String fid, String token) {
  92. Call<Void> call = friendsRest.deleteFriend(uid,fid,token);
  93. call.enqueue(new Callback<Void>() {
  94. @Override
  95. public void onResponse(Call<Void> call, Response<Void> response) {
  96. if (response.isSuccessful()) {
  97. System.out.println("Successful");
  98. } else {
  99. System.out.println("ResponseError");
  100. }
  101. }
  102. @Override
  103. public void onFailure(Call<Void> call, Throwable t) {
  104. System.out.println(" NetworkError" + t);
  105. }
  106. });
  107. }
  108. //フレンド申請を行うときの情報の取得
  109. public void getRequesting(String uid) {
  110. Call<Collection<AccountNameJson>> call = friendsRest.getRequesting(uid);
  111. call.enqueue(new Callback<Collection<AccountNameJson>>() {
  112. @Override
  113. public void onResponse(Call<Collection<AccountNameJson>> call, Response<Collection<AccountNameJson>> response) {
  114. if (response.isSuccessful()) {
  115. System.out.println("Successful");
  116. Collection<AccountNameJson> accountNameJson = response.body();
  117. requestingLiveData.setValue(accountNameJson);
  118. } else {
  119. System.out.println("ResponseError" + response.code());
  120. }
  121. }
  122. @Override
  123. public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
  124. System.out.println(" NetworkError" + t);
  125. }
  126. });
  127. }
  128.  
  129. public void putRequesting(String uid,String requesting_id, String token) {
  130. Call<Void> call = friendsRest.putRequesting(uid,requesting_id,token);
  131. call.enqueue(new Callback<Void>() {
  132. @Override
  133. public void onResponse(Call<Void> call, Response<Void> response) {
  134. if (response.isSuccessful()) {
  135. System.out.println("Successful");
  136. } else {
  137. System.out.println("ResponseError" + response.code());
  138. }
  139. }
  140. @Override
  141. public void onFailure(Call<Void> call, Throwable t) {
  142. System.out.println(" NetworkError" + t);
  143. }
  144. });
  145. }
  146. // 対象のアカウントのフレンド申請を行うときの情報削除
  147. public void deleteRequesting(String uid,String requesting_id, String token) {
  148. Call<Void> call = friendsRest.deleteRequesting(uid,requesting_id,token);
  149. call.enqueue(new Callback<Void>() {
  150. @Override
  151. public void onResponse(Call<Void> call, Response<Void> response) {
  152. if (response.isSuccessful()) {
  153. System.out.println("Successful");
  154. } else {
  155. System.out.println("ResponseError");
  156. }
  157. }
  158. @Override
  159. public void onFailure(Call<Void> call, Throwable t) {
  160. System.out.println(" NetworkError" + t);
  161. }
  162. });
  163. }
  164. //フレンド申請を行われたときの情報の取得
  165. public void getRequested(String uid) {
  166. Call<Collection<AccountNameJson>> call = friendsRest.getRequested(uid);
  167. call.enqueue(new Callback<Collection<AccountNameJson>>() {
  168. @Override
  169. public void onResponse(Call<Collection<AccountNameJson>> call, Response<Collection<AccountNameJson>> response) {
  170. if (response.isSuccessful()) {
  171. System.out.println("Successful");
  172. Collection<AccountNameJson> accountNameJson = response.body();
  173. requestedLiveData.setValue(accountNameJson);
  174. } else {
  175. System.out.println("ResponseError");
  176. }
  177. }
  178. @Override
  179. public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
  180. System.out.println(" NetworkError" + t);
  181. }
  182. });
  183. }
  184. // 対象のアカウントのフレンド申請を行われたときの情報削除
  185. public void deleteRequested(String uid,String fid, String token) {
  186. Call<Void> call = friendsRest.deleteRequested(uid,fid,token);
  187. call.enqueue(new Callback<Void>() {
  188. @Override
  189. public void onResponse(Call<Void> call, Response<Void> response) {
  190. if (response.isSuccessful()) {
  191. System.out.println("Successful");
  192. } else {
  193. System.out.println("ResponseError");
  194. }
  195. }
  196. @Override
  197. public void onFailure(Call<Void> call, Throwable t) {
  198. System.out.println(" NetworkError" + t);
  199. }
  200. });
  201. }
  202. }