Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / FriendViewModel.java
k-suzuki on 4 Jul 2023 2 KB FriendViewModelを作った
package com.example.nemophila.viewmodels;

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

import com.example.nemophila.entities.Post;
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<String> friendsLiveData;
    private final MutableLiveData<String> requestedLiveData;
    private final MutableLiveData<Collection<Post>> 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<String> getFriendsLiveData() {
        return friendsLiveData;
    }
    public MutableLiveData<String> getRequestedLiveData() {
        return requestedLiveData;
    }
    public MutableLiveData<Collection<Post>> getRequestingLiveData() { return requestingLiveData; }

    public void searchFriend(String uid,String fid, String token) {
        Call<Void> call = friendsRest.putFriend ();
        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);
            }
        });
    }
}