Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / FriendViewModel.java
package com.example.nemophila.viewmodels;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.nemophila.entities.AccountNameJson;
import com.example.nemophila.entities.ErrorType;
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.ArrayList;
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;
    private final MutableLiveData<String> errorLiveData;

    // コンストラクタ
    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<>();
        this.errorLiveData = new MutableLiveData<>();
    }

    // ライブデータの取得(ゲッター)
    public MutableLiveData<Collection<AccountNameJson>> getFriendsLiveData() {
        return friendsLiveData;
    }
    public MutableLiveData<Collection<AccountNameJson>> getRequestedLiveData() {
        return requestedLiveData;
    }
    public MutableLiveData<Collection<AccountNameJson>> getRequestingLiveData() { return requestingLiveData; }
    public MutableLiveData<String> getErrorLiveData() {
        return errorLiveData;
    }


//フレンドの情報の取得
    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");
                    friendsLiveData.setValue(response.body());
                } else {
                    System.out.println("ResponseError: " + response.code());
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }
            @Override
            public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
                System.out.println(" NetworkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }

    public void putFriend(String uid, String fid, String name, 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()) {
                    removeRequestedLiveData(fid);
                    System.out.println("Successful");
                } else {
                    System.out.println("ResponseError: " + response.code());
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println(" NetworkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }
    // 対象のアカウントのフレンド登録情報の削除
    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()) {
                    removeFriendLiveData(fid);
                    System.out.println("Successful");
                } else {
                    System.out.println("ResponseError: " + response.code());
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println(" NetworkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }
    //フレンド申請を行うときの情報の取得
    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()) {
                    requestingLiveData.setValue(response.body());
                    System.out.println("Successful");
                } else {
                    System.out.println("ResponseError: " + response.code());
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }
            @Override
            public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
                System.out.println(" NetworkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }

    public void putRequesting(String uid, String requesting_id, String name, 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");
                    addRequestingLiveData(requesting_id, name);
                } else {
                    System.out.println("ResponseError: " + response.code());
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println(" NetworkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }
    // 対象のアカウントのフレンド申請を行うときの情報削除
    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()) {
                    removeRequestingLiveData(requesting_id);
                    System.out.println("Successful Delete");
                } else {
                    System.out.println("ResponseError: " + response.code());
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println(" NetworkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }
    //フレンド申請を行われたときの情報の取得
    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: " + response.code());
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }
            @Override
            public void onFailure(Call<Collection<AccountNameJson>> call, Throwable t) {
                System.out.println(" NetworkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }
    // 対象のアカウントのフレンド申請を行われたときの情報削除
    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");
                    removeRequestedLiveData(fid);
                } else {
                    System.out.println("ResponseError: " + response.code());
                    errorLiveData.setValue(parseStatusCode(response.code()));
                }
            }
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                System.out.println(" NetworkError: " + t);
                errorLiveData.setValue(ErrorType.NetworkError.getText());
            }
        });
    }

    private AccountNameJson createAccountNameJson(String uid, String name) {
        AccountNameJson anj = new AccountNameJson();
        anj.setUid(uid);
        anj.setName(name);
        return anj;
    }

    private void addRequestingLiveData(String uid, String name) {
        ArrayList<AccountNameJson> preData = new ArrayList<>();
        for (AccountNameJson anj: requestingLiveData.getValue()) {
            if(!anj.getUid().equals(uid)) {
                preData.add(anj);
            }
        }
        preData.add(createAccountNameJson(uid, name));
        requestingLiveData.setValue(preData);
    }

    private void removeFriendLiveData(String id) {
        ArrayList<AccountNameJson> preData = new ArrayList<>();
        for (AccountNameJson anj: friendsLiveData.getValue()) {
            if (anj.getUid().equals(id)) {
                continue;
            }
            preData.add(anj);
        }
        friendsLiveData.setValue(preData);
    }

    private void removeRequestingLiveData(String id) {
        ArrayList<AccountNameJson> preData = new ArrayList<>();
        for (AccountNameJson anj: requestingLiveData.getValue()) {
            if (anj.getUid().equals(id)) {
                continue;
            }
            preData.add(anj);
        }
        requestingLiveData.setValue(preData);
    }

    private void removeRequestedLiveData(String id) {
        ArrayList<AccountNameJson> preData = new ArrayList<>();
        for (AccountNameJson anj: requestedLiveData.getValue()) {
            if (anj.getUid().equals(id)) {
                continue;
            }
            preData.add(anj);
        }
        requestedLiveData.setValue(preData);
    }

    private String parseStatusCode(Integer stats) {
        switch (stats) {
            case 404:
                return ErrorType.ResponseNoyFound.getText();
            case 401:
                return ErrorType.InvalidToken.getText();
            case 400:
                return ErrorType.ResponseError.getText();
            default:
                return ErrorType.UnknownError.getText();
        }
    }
}