package org.mercury_im.messenger.ui.chatlist;

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

import org.mercury_im.messenger.MercuryImApplication;
import org.mercury_im.messenger.data.repository.DirectChatRepository;
import org.mercury_im.messenger.entity.chat.DirectChat;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;

public class ChatListViewModel extends ViewModel {

    @Inject
    DirectChatRepository chatRepository;

    private CompositeDisposable disposable = new CompositeDisposable();

    private final MutableLiveData<List<DirectChat>> chats = new MutableLiveData<>(new ArrayList<>());

    public ChatListViewModel() {
        MercuryImApplication.getApplication().getAppComponent().inject(this);

        disposable.add(chatRepository.observeAllDirectChats()
                .subscribe(chats::setValue));
    }

    public LiveData<List<DirectChat>> getChats() {
        return chats;
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        disposable.dispose();
    }
}
