Proyectos de Subversion Android Microlearning

Rev

Autoría | Ultima modificación | Ver Log |

package com.cesams.twogetskills.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cesams.twogetskills.Constants;
import com.cesams.twogetskills.R;
import com.cesams.twogetskills.adapter.CompanyListViewAdapter;
import com.cesams.twogetskills.dao.CompanyDao;
import com.cesams.twogetskills.entity.Company;
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
import com.cesams.twogetskills.viewmodel.CompanyViewModel;

import java.util.List;

public class CompanyFragment extends Fragment implements CompanyListViewAdapter.ItemClickListener, LifecycleOwner {
    private final String TAG = "C2GS - CompanyFragment";
    private RecyclerView listView;
    private CompanyListViewAdapter adapter;
    private ITwoGetSkills iTwoGetSkills;
    private CompanyViewModel mCompanyViewModel;


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

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_company, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iTwoGetSkills = (ITwoGetSkills) getActivity();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

        mCompanyViewModel = new ViewModelProvider(requireActivity()).get(CompanyViewModel.class);
        adapter = new CompanyListViewAdapter(getActivity(), mCompanyViewModel.getCompanyList());
        adapter.setClickListener(this);

        Observer<List<Company>> CompanyListUpdateObserver = new Observer<List<Company>>() {
            @Override
            public void onChanged(List<Company> CompanyList) {
                adapter.notifyDataSetChanged();
            }
        };


        mCompanyViewModel.getCompanyMutableLiveData().observe(requireActivity(),CompanyListUpdateObserver);

        
        
        listView = (RecyclerView) getView().findViewById(R.id.fragment_company_listview);
        listView.setAdapter(adapter);
        listView.setLayoutManager( linearLayoutManager  );
        listView.setHasFixedSize(true);

    }

    @Override
    public void onResume() {
        super.onResume();

        Log.d(TAG, "onResume");
        loadData();
    }




    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        Log.d(TAG, "onHiddenChanged : " + (hidden ? "true" : "false"));

        if(!hidden) {
            loadData();
        }
    }

    private void loadData() {

        mCompanyViewModel.getCompanyList().clear();

        CompanyDao companyDao = iTwoGetSkills.getDatabase().getCompanyDao();
        List<Company> dbCompanies = companyDao.selectAll();
        Company company;

        for (Company dbCompany : dbCompanies)
        {
            company = new Company();
            company.setUuid(dbCompany.getUuid());
            company.setName(dbCompany.getName());
            company.setImage(dbCompany.getImage());

            mCompanyViewModel.getCompanyList().add(company);

        }

        iTwoGetSkills.setTitleActionBar(getActivity().getString(R.string.label_companies));
        mCompanyViewModel.getCompanyMutableLiveData().setValue(mCompanyViewModel.getCompanyList());
    }

    @Override
    public void onItemClick(View view, int position)
    {
        iTwoGetSkills.changeCompanyActive(mCompanyViewModel.getCompanyList().get(position).getUuid());
        iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_PROGRESS);
    }
}