- package com.example.nemophila.viewmodels;
-
- import androidx.lifecycle.MutableLiveData;
- import androidx.lifecycle.ViewModel;
-
- import com.example.nemophila.entities.AccountNameJson;
- import com.example.nemophila.entities.Post;
- import com.example.nemophila.entities.PostJson;
- import com.example.nemophila.resources.AccountsRest;
- import com.example.nemophila.resources.FriendsRest;
-
- import java.util.Collection;
-
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.converter.jackson.JacksonConverterFactory;
-
- public class FriendViewModel extends ViewModel {
- // フィールド
- private final Retrofit retrofit;
- private final FriendsRest friendsRest;
- // ライブデータ
- private final MutableLiveData<Collection<AccountNameJson>> friendsLiveData;
- private final MutableLiveData<Collection<AccountNameJson>> requestedLiveData;
- private final MutableLiveData<Collection<AccountNameJson>> requestingLiveData;
-
- // コンストラクタ
- public FriendViewModel() {
- this.retrofit = new Retrofit.Builder()
- .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/nemophila/")
- .addConverterFactory(JacksonConverterFactory.create())
- .build();
- this.friendsRest = retrofit.create(FriendsRest.class);
- this.friendsLiveData = new MutableLiveData<>();
- this.requestedLiveData = new MutableLiveData<>();
- this.requestingLiveData = new MutableLiveData<>();
- }
-
- // ライブデータの取得(ゲッター)
- public MutableLiveData<Collection<AccountNameJson>> getFriendsLiveData() {
- return friendsLiveData;
- }
- public MutableLiveData<Collection<AccountNameJson>> getRequestedLiveData() {
- return requestedLiveData;
- }
- public MutableLiveData<Collection<AccountNameJson>> getRequestingLiveData() { return requestingLiveData; }
-
-
- //フレンドの情報の取得
- public void getFriends(String uid) {
- Call<Collection<AccountNameJson>> call = friendsRest.getFriends(uid);
- call.enqueue(new Callback<Collection<AccountNameJson>>() {
- @Override
- public void onResponse(Call<Collection<AccountNameJson>> call, Response<Collection<AccountNameJson>> response) {
- if (response.isSuccessful()) {
- System.out.println("Successful");
- Collection<AccountNameJson> accountNameJson = response.body();
- friendsLiveData.setValue(accountNameJson);
-
- } else {
- System.out.println("ResponseError");
- }
- }
- @Override
- public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
- System.out.println(" NetworkError" + t);
- }
- });
- }
-
- public void putFriend(String uid,String fid, String token) {
- Call<Void> call = friendsRest.putFriend(uid,fid,token);
- call.enqueue(new Callback<Void>() {
- @Override
- public void onResponse(Call<Void> call, Response<Void> response) {
- if (response.isSuccessful()) {
- System.out.println("Successful");
- } else {
- System.out.println("ResponseError");
- }
- }
- @Override
- public void onFailure(Call<Void> call, Throwable t) {
- System.out.println(" NetworkError" + t);
- }
- });
- }
- // 対象のアカウントのフレンド登録情報の削除
- public void deleteFriend(String uid,String fid, String token) {
- Call<Void> call = friendsRest.deleteFriend(uid,fid,token);
- call.enqueue(new Callback<Void>() {
- @Override
- public void onResponse(Call<Void> call, Response<Void> response) {
- if (response.isSuccessful()) {
- System.out.println("Successful");
- } else {
- System.out.println("ResponseError");
- }
- }
- @Override
- public void onFailure(Call<Void> call, Throwable t) {
- System.out.println(" NetworkError" + t);
- }
- });
- }
- //フレンド申請を行うときの情報の取得
- public void getRequesting(String uid) {
- Call<Collection<AccountNameJson>> call = friendsRest.getRequesting(uid);
- call.enqueue(new Callback<Collection<AccountNameJson>>() {
- @Override
- public void onResponse(Call<Collection<AccountNameJson>> call, Response<Collection<AccountNameJson>> response) {
- if (response.isSuccessful()) {
- System.out.println("Successful");
- Collection<AccountNameJson> accountNameJson = response.body();
- requestingLiveData.setValue(accountNameJson);
- } else {
- System.out.println("ResponseError" + response.code());
- }
- }
- @Override
- public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
- System.out.println(" NetworkError" + t);
- }
- });
- }
-
- public void putRequesting(String uid,String requesting_id, String token) {
- Call<Void> call = friendsRest.putRequesting(uid,requesting_id,token);
- call.enqueue(new Callback<Void>() {
- @Override
- public void onResponse(Call<Void> call, Response<Void> response) {
- if (response.isSuccessful()) {
- System.out.println("Successful");
- } else {
- System.out.println("ResponseError" + response.code());
- }
- }
- @Override
- public void onFailure(Call<Void> call, Throwable t) {
- System.out.println(" NetworkError" + t);
- }
- });
- }
- // 対象のアカウントのフレンド申請を行うときの情報削除
- public void deleteRequesting(String uid,String requesting_id, String token) {
- Call<Void> call = friendsRest.deleteRequesting(uid,requesting_id,token);
- call.enqueue(new Callback<Void>() {
- @Override
- public void onResponse(Call<Void> call, Response<Void> response) {
- if (response.isSuccessful()) {
- System.out.println("Successful");
- } else {
- System.out.println("ResponseError");
- }
- }
- @Override
- public void onFailure(Call<Void> call, Throwable t) {
- System.out.println(" NetworkError" + t);
- }
- });
- }
- //フレンド申請を行われたときの情報の取得
- public void getRequested(String uid) {
- Call<Collection<AccountNameJson>> call = friendsRest.getRequested(uid);
- call.enqueue(new Callback<Collection<AccountNameJson>>() {
- @Override
- public void onResponse(Call<Collection<AccountNameJson>> call, Response<Collection<AccountNameJson>> response) {
- if (response.isSuccessful()) {
- System.out.println("Successful");
- Collection<AccountNameJson> accountNameJson = response.body();
- requestedLiveData.setValue(accountNameJson);
- } else {
- System.out.println("ResponseError");
- }
- }
- @Override
- public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
- System.out.println(" NetworkError" + t);
- }
- });
- }
- // 対象のアカウントのフレンド申請を行われたときの情報削除
- public void deleteRequested(String uid,String fid, String token) {
- Call<Void> call = friendsRest.deleteRequested(uid,fid,token);
- call.enqueue(new Callback<Void>() {
- @Override
- public void onResponse(Call<Void> call, Response<Void> response) {
- if (response.isSuccessful()) {
- System.out.println("Successful");
- } else {
- System.out.println("ResponseError");
- }
- }
- @Override
- public void onFailure(Call<Void> call, Throwable t) {
- System.out.println(" NetworkError" + t);
- }
- });
- }
- }