Proyectos de Subversion Android Microlearning - Inconcert

Rev

Rev 1 | Rev 5 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

package com.cesams.twogetskills.inconcert.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.cesams.twogetskills.inconcert.R;
import com.cesams.twogetskills.inconcert.adapter.CardViewAdapter;
import com.cesams.twogetskills.inconcert.dao.NotificationCenterDao;
import com.cesams.twogetskills.inconcert.skeleton.ITwoGetSkills;

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

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NotificationCenter#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NotificationCenter extends Fragment implements CardViewAdapter.ClickListener {

   RecyclerView notificaciones;
   CardViewAdapter notificacionadapter;
   private ITwoGetSkills iTwoGetSkills;
   TextView notienenuevas;

    ArrayList<HashMap<String, String>> notificacionList;


    public NotificationCenter() {
        // Required empty public constructor
    }

    public static NotificationCenter newInstance() {
        NotificationCenter fragment = new NotificationCenter();
        return fragment;
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_notification_center, container, false);

        iTwoGetSkills = (ITwoGetSkills) getActivity();

        notificaciones = view.findViewById(R.id.notificacioneslista);

        notificacionList= new ArrayList<>();
        notienenuevas= view.findViewById(R.id.textView18);
        notienenuevas.setVisibility(View.VISIBLE);

        notificacionadapter = new CardViewAdapter(getContext(),notificacionList);

        LinearLayoutManager layoutnotifi = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL, true);
        layoutnotifi.setStackFromEnd(true);

        notificaciones.setLayoutManager(layoutnotifi);
        notificaciones.setAdapter(notificacionadapter);

        notificacionadapter.setClickListener(this);

       // loaddata();


        return view;
    }

    private void loaddata(){

        notificacionList.clear();
        NotificationCenterDao notificacionDao;

        notificacionDao =iTwoGetSkills.getDatabase().getNotificationCenterDao();

        //Log.e("Notificaciones","estas:"+notificacionDao.selectAllNotification().toString());

        List<com.cesams.twogetskills.inconcert.entity.NotificationCenter> lista;
        lista=notificacionDao.selectAllNotification();

       // Toast.makeText(getContext(), "Hay registradas "+lista.size()+" notificaciones", Toast.LENGTH_SHORT).show();
        HashMap<String, String> m_li;

        for(com.cesams.twogetskills.inconcert.entity.NotificationCenter notificacion: lista)
        {
            Log.e("Esto",""+notificacion.getTitle());
            m_li = new HashMap<>();

            m_li.put("titulo",notificacion.getTitle());
            m_li.put("date",notificacion.getDate());
            m_li.put("descripcion",notificacion.getDescription());
           // Log.e("Viewer: ",notificacion.getViewed());
            m_li.put("color",notificacion.getViewed());
            m_li.put("url",notificacion.getUrl());

            notificacionList.add(m_li);
            notienenuevas.setVisibility(View.GONE);

            //Agregar notificacion a la lista de vista
            com.cesams.twogetskills.inconcert.entity.NotificationCenter notificacionupdate = new com.cesams.twogetskills.inconcert.entity.NotificationCenter();
            notificacionupdate.setId(notificacion.getId());
            notificacionupdate.setDescription(notificacion.getDescription());
            notificacionupdate.setDate(notificacion.getDate());
            notificacionupdate.setTitle(notificacion.getTitle());
            notificacionupdate.setUrl(notificacion.getUrl());
            notificacionupdate.setViewed("white");
            notificacionDao.update(notificacionupdate);

            notificacionadapter.notifyDataSetChanged();


        }
    }

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

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loaddata();

            }
        });
        notificaciones.scrollTo(0, getView().getBottom());

    }

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

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    loaddata();

                }
            });
            notificaciones.scrollTo(0, getView().getBottom());

        }
    }

    @Override
    public void onItemClick(int position, View v) {
        Log.e("Aqui", "pise en el fragment"+position);
        //        notificacionList.get(position).put("color","white");
       // loaddata();

        if(!notificacionList.get(position).get("url").isEmpty())
        {
            Log.e("Tiene url", "vamos alla");
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(notificacionList.get(position).get("url")));
            try{
                startActivity(browserIntent);
            }catch (Exception e)
            {
                Toast.makeText(getContext(), "Necesitas un navegador para ver esta web", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            Log.e("No hay url", "no hago nada");
        }
    }
}