Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

Rev 19 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 gabriel 1
package com.cesams.twogetskills.fragment;
2
 
3
import android.os.Bundle;
4
import android.util.Log;
5
import android.view.LayoutInflater;
6
import android.view.Menu;
7
import android.view.MenuInflater;
8
import android.view.View;
9
import android.view.ViewGroup;
10
 
11
import androidx.annotation.NonNull;
12
import androidx.annotation.Nullable;
13
import androidx.fragment.app.Fragment;
14
import androidx.lifecycle.LifecycleOwner;
15
import androidx.lifecycle.Observer;
16
import androidx.lifecycle.ViewModelProvider;
17
import androidx.recyclerview.widget.RecyclerView;
18
 
19 gabriel 19
import com.cesams.twogetskills.Constants;
1 gabriel 20
import com.cesams.twogetskills.R;
3 gabriel 21
import com.cesams.twogetskills.activity.MainActivity;
1 gabriel 22
import com.cesams.twogetskills.adapter.CapsuleListViewAdapter;
23
import com.cesams.twogetskills.dao.CapsuleDao;
24
import com.cesams.twogetskills.dao.ProgressDao;
25
import com.cesams.twogetskills.dao.TopicDao;
26
import com.cesams.twogetskills.entity.Capsule;
27
import com.cesams.twogetskills.entity.Progress;
28
import com.cesams.twogetskills.entity.Topic;
29
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
30
import com.cesams.twogetskills.viewmodel.CapsuleViewModel;
31
 
32
import java.util.ArrayList;
33
import java.util.List;
34
 
35
public class CapsuleFragment extends Fragment implements CapsuleListViewAdapter.ItemClickListener, LifecycleOwner {
36
    private final String TAG = "C2GS - CapsuleFragment";
37
    private RecyclerView listView;
38
    private CapsuleListViewAdapter adapter;
39
    private ITwoGetSkills iTwoGetSkills;
40
    private CapsuleViewModel mCapsuleViewModel;
41
 
42
 
19 gabriel 43
 
1 gabriel 44
    @Override
45
    public void onCreate(@Nullable Bundle savedInstanceState) {
46
        super.onCreate(savedInstanceState);
47
        setHasOptionsMenu(true);
48
 
49
 
50
    }
51
 
52
    @Override
53
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
54
        super.onCreateOptionsMenu(menu, inflater);
55
        menu.clear();
56
    }
57
 
58
    @Override
59
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
60
 
61
        return inflater.inflate(R.layout.fragment_capsule, container, false);
62
    }
63
 
64
    @Override
65
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
66
        super.onViewCreated(view, savedInstanceState);
67
        iTwoGetSkills = (ITwoGetSkills) getActivity();
68
 
69
 
19 gabriel 70
 
1 gabriel 71
        mCapsuleViewModel = new ViewModelProvider(requireActivity()).get(CapsuleViewModel.class);
72
        adapter = new CapsuleListViewAdapter(requireActivity(), mCapsuleViewModel.getCapsuleArrayList());
73
        adapter.setClickListener(this);
74
 
3 gabriel 75
 
1 gabriel 76
        Log.d(TAG, "Declarando el Observador");
3 gabriel 77
 
1 gabriel 78
        Observer<ArrayList<Capsule>> capsuleListUpdateObserver = new Observer<ArrayList<Capsule>>() {
79
            @Override
80
            public void onChanged(ArrayList<Capsule> capsuleArrayList) {
3 gabriel 81
 
1 gabriel 82
                adapter.notifyDataSetChanged();
83
            }
84
        };
85
 
86
 
87
        mCapsuleViewModel.getCapsuleMutableLiveData().observe(requireActivity(),capsuleListUpdateObserver);
88
 
89
 
3 gabriel 90
        listView = getView().findViewById(R.id.fragment_capsule_listview);
1 gabriel 91
        listView.setAdapter(adapter);
92
        listView.setHasFixedSize(true);
93
 
94
 
95
 
19 gabriel 96
        loadData();
3 gabriel 97
 
98
 
1 gabriel 99
    }
100
 
101
 
102
    @Override
103
    public void onItemClick(View view, int position)
104
    {
71 efrain 105
        iTwoGetSkills.changeCapsuleActiveSourceNavigationTopics(mCapsuleViewModel.getCapsuleArrayList().get(position).getUuid());
1 gabriel 106
    }
107
 
108
    @Override
109
    public void onResume() {
110
        super.onResume();
111
 
19 gabriel 112
        loadData();
3 gabriel 113
 
1 gabriel 114
    }
115
 
116
 
117
 
118
 
119
    @Override
120
    public void onHiddenChanged(boolean hidden) {
121
        super.onHiddenChanged(hidden);
122
 
123
        Log.d(TAG, "onHiddenChanged : " + (hidden ? "true" : "false"));
124
 
125
        if(!hidden) {
19 gabriel 126
            loadData();
1 gabriel 127
        }
128
    }
129
 
19 gabriel 130
    private void loadData() {
1 gabriel 131
 
19 gabriel 132
        int fragmentIdxActive = iTwoGetSkills.getPreference().getFragmentIdxActive();
133
 
134
        if (fragmentIdxActive != Constants.IDX_FRAGMENT_GALLERY
135
                && fragmentIdxActive != Constants.IDX_FRAGMENT_SLIDES
136
                && fragmentIdxActive != Constants.IDX_FRAGMENT_CAPSULES
137
        ) {
138
            return;
139
        }
140
 
141
        Log.d("BUG 2PLANO", "CapsuleFragment - loadData");
142
        Log.d("BUG 2PLANO", "CapsuleFragment - TopicUuid : " + iTwoGetSkills.getTopicUuidActive());
143
 
144
 
145
        String userUuid = iTwoGetSkills.getPreference().getUserUuid();
1 gabriel 146
        TopicDao topicDao = iTwoGetSkills.getDatabase().getTopicDao();
3 gabriel 147
        CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();
148
        List<Capsule> dbCapsules;
1 gabriel 149
 
150
 
19 gabriel 151
        Topic topic = topicDao.selectByUuid(iTwoGetSkills.getTopicUuidActive());
152
        iTwoGetSkills.setTitleActionBar(topic.getName());
1 gabriel 153
 
19 gabriel 154
        dbCapsules = capsuleDao.selectAllByTopicUuid(topic.getUuid());
155
        Log.e("Cargando","Desde elemento topic");
1 gabriel 156
 
157
 
3 gabriel 158
 
159
 
1 gabriel 160
 
3 gabriel 161
            mCapsuleViewModel.getCapsuleArrayList().clear();
162
 
163
            Capsule capsule;
164
            Progress progress;
165
            ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
166
            for(Capsule dbCapsule : dbCapsules)
167
            {
168
                capsule = new Capsule();
169
                capsule.setTopicUuid(dbCapsule.getTopicUuid());
170
                capsule.setUuid(dbCapsule.getUuid());
171
                capsule.setName(dbCapsule.getName());
172
                capsule.setDescription(dbCapsule.getDescription());
173
                capsule.setImage(dbCapsule.getImage());
174
                capsule.setPosition(dbCapsule.getPosition());
175
 
176
 
19 gabriel 177
                progress = progressDao.selectByCapsuleUuidAndUserUuid(capsule.getUuid(), userUuid);
3 gabriel 178
                if(progress != null) {
179
                    capsule.setCompleted( progress.getCompleted());
180
                    capsule.setViewSlides(progress.getViewSlides());
181
                    capsule.setTotalSlides( progress.getTotalSlides());
182
                    capsule.setProgress(progress.getProgress());
183
                }
184
 
185
                mCapsuleViewModel.getCapsuleArrayList().add(capsule);
1 gabriel 186
            }
187
 
3 gabriel 188
        mCapsuleViewModel.getCapsuleMutableLiveData().setValue(mCapsuleViewModel.getCapsuleArrayList());
2 gabriel 189
 
1 gabriel 190
        }
191
 
192
 
3 gabriel 193
    }
1 gabriel 194
 
195