package com.ccwangluo.im.ui.dashboard;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ccwangluo.ccim.modle.Contact;
import com.ccwangluo.ccim.modle.Dialog;
import com.ccwangluo.im.R;
import com.ccwangluo.im.adapter.DialogAdapter;
import com.ccwangluo.im.data.LoginViewModel;
import com.ccwangluo.im.databinding.FragmentDashboardBinding;
import com.ccwangluo.im.databinding.FragmentGroupBinding;
import com.ccwangluo.im.databinding.FragmentMessageBinding;

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


public class MessageFragment extends Fragment {
    private LoginViewModel loginViewModel;
    private FragmentMessageBinding binding;
    private List<Dialog> dialogList = new ArrayList<>();
    private DialogAdapter dialogAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding= FragmentMessageBinding.inflate(inflater, container, false);
        loginViewModel =
                new ViewModelProvider(getActivity()).get(LoginViewModel.class);
        dialogAdapter = new DialogAdapter(dialogList, getContext());
        binding.dialogList.setAdapter(dialogAdapter);
        return binding.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        loginViewModel.dialogList.observe(getActivity(), new Observer<Collection<Dialog>>() {
            @Override
            public void onChanged(Collection<Dialog> dialogs) {
                dialogList.clear();
                dialogList.addAll(dialogs);
                dialogAdapter.refreshData();
            }
        });
    }
}