Proyectos de Subversion Android Microlearning - Inconcert

Rev

Rev 6 | 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.Constants;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 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>> notificacionListToday;


    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);
        notificacionListToday= new ArrayList<>();
        notienenuevas= view.findViewById(R.id.textView18);
        notienenuevas.setVisibility(View.VISIBLE);

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

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

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

        notificacionadapter.setClickListener(this);



       // loaddata();


        return view;
    }

    private void loaddata(){
        iTwoGetSkills.setTitleActionBar("Centro de notificaciones");

        notificacionListToday.clear();
        NotificationCenterDao notificacionDao;

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

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

        List<com.cesams.twogetskills.inconcert.entity.NotificationCenter> grupo;
        grupo=notificacionDao.selectAllNotificationOrderDay();

        boolean hoy = false,ayer=false,other=false;

        for (com.cesams.twogetskills.inconcert.entity.NotificationCenter notificaciongrupos: grupo){

            String diaactual= notificaciongrupos.getDate();

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

            HashMap<String, String> m_li,dias;

            for(com.cesams.twogetskills.inconcert.entity.NotificationCenter notificacion: lista)
            {
                m_li = new HashMap<>(); dias = new HashMap<>();

                m_li.put("titulo",notificacion.getTitle());
                m_li.put("date",notificacion.getDate());
                m_li.put("descripcion",notificacion.getDescription());
                m_li.put("color",notificacion.getViewed());
                m_li.put("url",notificacion.getUrl());
                dias.put("titulo","");
                dias.put("date",notificacion.getDate());
                dias.put("descripcion","");
                dias.put("color","");
                dias.put("url","");

                Calendar calendar = Calendar.getInstance();
                Date date = calendar.getTime();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATE_SERVICE);
                String  dateOn = simpleDateFormat.format(date);


                final Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -1);
                Date date2 = cal.getTime();
                SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(Constants.FORMAT_DATE_SERVICE);
                String  dateOn2 = simpleDateFormat2.format(date2);

                if(Objects.equals(notificacion.getDate(), dateOn))
                {
                    if(!hoy){

                        dias.put("day","today");
                        notificacionListToday.add(dias);
                        hoy=true;
                    }
                    m_li.put("day","");
                    notificacionListToday.add(m_li);

                }
                else if (Objects.equals(notificacion.getDate(), dateOn2))
                {
                    if(!ayer){
                        dias.put("day","yesterday");
                        notificacionListToday.add(dias);
                        ayer=true;
                    }
                    m_li.put("day","");
                    notificacionListToday.add(m_li);

                }
                else if(!Objects.equals(notificacion.getDate(), dateOn) || !Objects.equals(notificacion.getDate(), dateOn2)) {

                    if(!other){

                        dias.put("day","other");
                        notificacionListToday.add(dias);
                        other=true;
                    }
                    m_li.put("day","");
                    notificacionListToday.add(m_li);

                }


                notienenuevas.setVisibility(View.GONE);

                //Agregar notificacion a la lista de vistas
                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.notifyItemChanged(notificacionListToday.size()-1);


            }


        }

        Log.e("Notificaciones: ",""+notificacionListToday.toString());

    }

    @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(!notificacionListToday.get(position).get("url").isEmpty())
        {
            Log.e("Tiene url", "vamos alla");
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(notificacionListToday.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");
        }
    }
}