Proyectos de Subversion Android Microlearning - Inconcert

Rev

Rev 3 | Rev 6 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 gabriel 1
package com.cesams.twogetskills.inconcert.fragment;
2
 
3
import android.content.Intent;
4
import android.net.Uri;
5
import android.os.Bundle;
6
 
7
import androidx.fragment.app.Fragment;
8
import androidx.recyclerview.widget.LinearLayoutManager;
9
import androidx.recyclerview.widget.RecyclerView;
10
 
11
import android.util.Log;
12
import android.view.LayoutInflater;
13
import android.view.View;
14
import android.view.ViewGroup;
15
import android.widget.TextView;
16
import android.widget.Toast;
17
 
18
import com.cesams.twogetskills.inconcert.R;
19
import com.cesams.twogetskills.inconcert.adapter.CardViewAdapter;
20
import com.cesams.twogetskills.inconcert.dao.NotificationCenterDao;
21
import com.cesams.twogetskills.inconcert.skeleton.ITwoGetSkills;
22
 
23
import java.util.ArrayList;
24
import java.util.HashMap;
25
import java.util.List;
5 gabriel 26
import java.util.Objects;
1 gabriel 27
 
28
/**
29
 * A simple {@link Fragment} subclass.
30
 * Use the {@link NotificationCenter#newInstance} factory method to
31
 * create an instance of this fragment.
32
 */
33
public class NotificationCenter extends Fragment implements CardViewAdapter.ClickListener {
34
 
35
   RecyclerView notificaciones;
36
   CardViewAdapter notificacionadapter;
37
   private ITwoGetSkills iTwoGetSkills;
5 gabriel 38
   TextView notienenuevas, itemdefecha;
1 gabriel 39
 
40
    ArrayList<HashMap<String, String>> notificacionList;
41
 
42
 
43
    public NotificationCenter() {
44
        // Required empty public constructor
45
    }
46
 
47
    public static NotificationCenter newInstance() {
48
        NotificationCenter fragment = new NotificationCenter();
49
        return fragment;
50
    }
51
 
52
    @Override
53
    public void onCreate(Bundle savedInstanceState) {
54
        super.onCreate(savedInstanceState);
55
 
56
    }
57
 
58
    @Override
59
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
60
                             Bundle savedInstanceState) {
61
        View view =inflater.inflate(R.layout.fragment_notification_center, container, false);
62
 
63
        iTwoGetSkills = (ITwoGetSkills) getActivity();
64
 
65
        notificaciones = view.findViewById(R.id.notificacioneslista);
5 gabriel 66
        itemdefecha = view.findViewById(R.id.textView17);
1 gabriel 67
        notificacionList= new ArrayList<>();
68
        notienenuevas= view.findViewById(R.id.textView18);
69
        notienenuevas.setVisibility(View.VISIBLE);
70
 
71
        notificacionadapter = new CardViewAdapter(getContext(),notificacionList);
72
 
73
        LinearLayoutManager layoutnotifi = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL, true);
74
        layoutnotifi.setStackFromEnd(true);
75
 
76
        notificaciones.setLayoutManager(layoutnotifi);
77
        notificaciones.setAdapter(notificacionadapter);
78
 
79
        notificacionadapter.setClickListener(this);
80
 
5 gabriel 81
        notificaciones.setOnScrollChangeListener(new View.OnScrollChangeListener() {
82
            @Override
83
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
84
 
85
                int itemPosition = ((LinearLayoutManager) Objects.requireNonNull(notificaciones.getLayoutManager())).findFirstCompletelyVisibleItemPosition();
86
 
87
               // itemdefecha.setText(notificacionList.get(itemPosition).get("date"));
88
 
89
             //   Log.e("Ultimo visible"," aqui"+itemPosition);
90
 
91
            }
92
        });
93
 
94
 
1 gabriel 95
       // loaddata();
96
 
97
 
98
        return view;
99
    }
100
 
101
    private void loaddata(){
102
 
103
        notificacionList.clear();
104
        NotificationCenterDao notificacionDao;
105
 
106
        notificacionDao =iTwoGetSkills.getDatabase().getNotificationCenterDao();
107
 
108
        //Log.e("Notificaciones","estas:"+notificacionDao.selectAllNotification().toString());
109
 
110
        List<com.cesams.twogetskills.inconcert.entity.NotificationCenter> lista;
111
        lista=notificacionDao.selectAllNotification();
112
 
3 gabriel 113
       // Toast.makeText(getContext(), "Hay registradas "+lista.size()+" notificaciones", Toast.LENGTH_SHORT).show();
1 gabriel 114
        HashMap<String, String> m_li;
115
 
116
        for(com.cesams.twogetskills.inconcert.entity.NotificationCenter notificacion: lista)
117
        {
118
            Log.e("Esto",""+notificacion.getTitle());
119
            m_li = new HashMap<>();
120
 
121
            m_li.put("titulo",notificacion.getTitle());
122
            m_li.put("date",notificacion.getDate());
123
            m_li.put("descripcion",notificacion.getDescription());
124
           // Log.e("Viewer: ",notificacion.getViewed());
125
            m_li.put("color",notificacion.getViewed());
126
            m_li.put("url",notificacion.getUrl());
127
 
128
            notificacionList.add(m_li);
129
            notienenuevas.setVisibility(View.GONE);
130
 
131
            //Agregar notificacion a la lista de vista
132
            com.cesams.twogetskills.inconcert.entity.NotificationCenter notificacionupdate = new com.cesams.twogetskills.inconcert.entity.NotificationCenter();
133
            notificacionupdate.setId(notificacion.getId());
134
            notificacionupdate.setDescription(notificacion.getDescription());
135
            notificacionupdate.setDate(notificacion.getDate());
136
            notificacionupdate.setTitle(notificacion.getTitle());
137
            notificacionupdate.setUrl(notificacion.getUrl());
138
            notificacionupdate.setViewed("white");
139
            notificacionDao.update(notificacionupdate);
140
 
141
            notificacionadapter.notifyDataSetChanged();
142
 
143
 
144
        }
145
    }
146
 
147
    @Override
148
    public void onResume() {
149
        super.onResume();
150
 
151
        getActivity().runOnUiThread(new Runnable() {
152
            @Override
153
            public void run() {
154
                loaddata();
155
 
156
            }
157
        });
158
        notificaciones.scrollTo(0, getView().getBottom());
159
 
160
    }
161
 
162
    @Override
163
    public void onHiddenChanged(boolean hidden) {
164
        super.onHiddenChanged(hidden);
165
        if(!hidden) {
166
 
167
            getActivity().runOnUiThread(new Runnable() {
168
                @Override
169
                public void run() {
170
                    loaddata();
171
 
172
                }
173
            });
174
            notificaciones.scrollTo(0, getView().getBottom());
175
 
176
        }
177
    }
178
 
179
    @Override
180
    public void onItemClick(int position, View v) {
181
        Log.e("Aqui", "pise en el fragment"+position);
182
        //        notificacionList.get(position).put("color","white");
183
       // loaddata();
184
 
185
        if(!notificacionList.get(position).get("url").isEmpty())
186
        {
187
            Log.e("Tiene url", "vamos alla");
188
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(notificacionList.get(position).get("url")));
189
            try{
190
                startActivity(browserIntent);
191
            }catch (Exception e)
192
            {
193
                Toast.makeText(getContext(), "Necesitas un navegador para ver esta web", Toast.LENGTH_SHORT).show();
194
            }
195
        }
196
        else
197
        {
198
            Log.e("No hay url", "no hago nada");
199
        }
200
    }
201
}