Rev 10 | Autoría | Ultima modificación | Ver Log |
package com.cesams.twogetskills.fragment;import android.os.Bundle;import androidx.annotation.NonNull;import androidx.fragment.app.Fragment;import androidx.lifecycle.LifecycleOwner;import androidx.recyclerview.widget.GridLayoutManager;import androidx.recyclerview.widget.RecyclerView;import android.util.Log;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.ProgressBar;import android.widget.TextView;import com.bumptech.glide.Glide;import com.bumptech.glide.load.engine.DiskCacheStrategy;import com.bumptech.glide.load.model.GlideUrl;import com.bumptech.glide.load.model.LazyHeaders;import com.bumptech.glide.request.RequestOptions;import com.cesams.twogetskills.Constants;import com.cesams.twogetskills.R;import com.cesams.twogetskills.adapter.CapsuleListViewAdapter;import com.cesams.twogetskills.adapter.TabsCapsulesAdapter;import com.cesams.twogetskills.dao.CapsuleDao;import com.cesams.twogetskills.dao.ProgressDao;import com.cesams.twogetskills.dao.TopicDao;import com.cesams.twogetskills.entity.Capsule;import com.cesams.twogetskills.entity.Progress;import com.cesams.twogetskills.entity.Topic;import com.cesams.twogetskills.library.MD5;import com.cesams.twogetskills.skeleton.ITwoGetSkills;import java.util.ArrayList;import java.util.Calendar;import java.util.HashMap;import java.util.List;import java.util.Random;import java.util.TimeZone;/*** A simple {@link Fragment} subclass.* Use the {@link WelcomeFragment#newInstance} factory method to* create an instance of this fragment.*/public class WelcomeFragment extends Fragment implements LifecycleOwner {TextView username, titulotarjeta, progresoporcentaje, empezar; ProgressBar progresotarjeta;private ITwoGetSkills iTwoGetSkills; View card;ArrayList<HashMap<String, String>> capsuleList;ImageView imagetarjeta;TabsCapsulesAdapter adapter2;RecyclerView categorizados;EditText busqueda; TextView textowelcome; Button continuar;public WelcomeFragment() {// Required empty public constructor}public static WelcomeFragment newInstance() {WelcomeFragment fragment = new WelcomeFragment();return fragment;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {iTwoGetSkills = (ITwoGetSkills) getActivity();iTwoGetSkills.showNavigationAndToolbar();View view= inflater.inflate(R.layout.fragment_welcome, container, false);username=view.findViewById(R.id.textView14);textowelcome= view.findViewById(R.id.textView15);busqueda=view.findViewById(R.id.busqueda);card= view.findViewById(R.id.include); empezar= view.findViewById(R.id.titlenotifi);capsuleList = new ArrayList<>();//Tarjeta Principaltitulotarjeta = view.findViewById(R.id.description); progresotarjeta= view.findViewById(R.id.progressBar2); imagetarjeta= view.findViewById(R.id.imagelist);continuar = view.findViewById(R.id.button); progresoporcentaje = view.findViewById(R.id.textView16);username.setText("¡Hola, "+(iTwoGetSkills.getPreference().getFirstName() +"!"));loadData();busqueda.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if(hasFocus){card.setVisibility(View.GONE);username.setVisibility(View.GONE);textowelcome.setVisibility(View.GONE);}}});adapter2 = new TabsCapsulesAdapter(getContext(),capsuleList);categorizados = view.findViewById(R.id.categorizados);GridLayoutManager layoutcategoria = new GridLayoutManager(getContext(),1,GridLayoutManager.VERTICAL, false);categorizados.setLayoutManager(layoutcategoria);categorizados.setAdapter(adapter2);categorizados.setOnFlingListener(new RecyclerView.OnFlingListener() {@Overridepublic boolean onFling(int velocityX, int velocityY) {if (categorizados.canScrollVertically(-1) ) {Log.e("Se activa", "On fling");card.setVisibility(View.GONE);username.setVisibility(View.GONE);textowelcome.setVisibility(View.GONE);}else{card.setVisibility(View.VISIBLE);username.setVisibility(View.VISIBLE);textowelcome.setVisibility(View.VISIBLE);}return false;}});/* categorizados.setOnScrollChangeListener(new View.OnScrollChangeListener() {@Overridepublic void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {if (!categorizados.canScrollVertically(-1)) {// Toast.makeText(getActivity(), "Last", Toast.LENGTH_LONG).show();capsulas.setVisibility(View.VISIBLE);username.setVisibility(View.VISIBLE);textowelcome.setVisibility(View.VISIBLE);Log.e("On scroll"," se activa");}}});*/return view;}@Overridepublic void onResume() {super.onResume();loadContinuecard();}@Overridepublic void onHiddenChanged(boolean hidden) {super.onHiddenChanged(hidden);Log.e("TAG", "onHiddenChanged : " + (hidden ? "true" : "false"));if(!hidden) {loadContinuecard();}}private void loadContinuecard(){Log.e("Cargo datos"," en tarjeta principal");//Capsula de primera tarjetaCapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();Progress progress;ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();List<Capsule> dbCapsules= capsuleDao.selectAll();for(Capsule dbCapsule : dbCapsules){progress = progressDao.selectByCapsuleUuid(dbCapsule.getUuid());if (progress != null) {if (progress.getProgress() < 100) {Log.e("Es menor"," a 100");titulotarjeta.setText(dbCapsule.getName());progresotarjeta.setVisibility(View.VISIBLE);progresoporcentaje.setVisibility(View.VISIBLE);progresotarjeta.setProgress((int) progress.getProgress());progresoporcentaje.setText("" + progress.getProgress() + " %");empezar.setText("En curso");iTwoGetSkills.getPreference().setCapsuleUuidActive(dbCapsule.getUuid());iTwoGetSkills.getPreference().setTopicUuidActive(dbCapsule.getTopicUuid());if (dbCapsule.getImage() != null && !dbCapsule.getImage().equals("")) {TimeZone timeZone = TimeZone.getTimeZone("UTC");Calendar calendar = Calendar.getInstance(timeZone);TimeZone tz = calendar.getTimeZone();int created = (int) (calendar.getTimeInMillis() / 1000);Random random = new Random(created);int rand = 1000 + random.nextInt(8999);String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();String password = iTwoGetSkills.getPreference().getPassword();String secret = MD5.generar(password + ':' + created + ':' + rand);GlideUrl url = new GlideUrl(dbCapsule.getImage(), new LazyHeaders.Builder().addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE).addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid).addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret).addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created)).addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand)).build());RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL);Glide.with(getContext()).load(url).thumbnail().apply(options).into(imagetarjeta);}}}}if(titulotarjeta.getText().equals("")){Log.e("Nombre","vacio");//Despues del recorrido no se encontro progreso en ninguna capsula, por lo cual la tarjeta queda vacia// vamos a llenarla con la ultima capsula, que debe ser nueva al no tener progresofor(Capsule dbCapsule : dbCapsules){progress = progressDao.selectByCapsuleUuid(dbCapsule.getUuid());if (progress == null) {Log.e("Es nueva","");titulotarjeta.setText(dbCapsule.getName());progresotarjeta.setVisibility(View.INVISIBLE);progresoporcentaje.setVisibility(View.INVISIBLE);empezar.setText("Nueva");continuar.setText("Ver cápsula");iTwoGetSkills.getPreference().setCapsuleUuidActive(dbCapsule.getUuid());iTwoGetSkills.getPreference().setTopicUuidActive(dbCapsule.getTopicUuid());if (dbCapsule.getImage() != null && !dbCapsule.getImage().equals("")) {TimeZone timeZone = TimeZone.getTimeZone("UTC");Calendar calendar = Calendar.getInstance(timeZone);TimeZone tz = calendar.getTimeZone();int created = (int) (calendar.getTimeInMillis() / 1000);Random random = new Random(created);int rand = 1000 + random.nextInt(8999);String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();String password = iTwoGetSkills.getPreference().getPassword();String secret = MD5.generar(password + ':' + created + ':' + rand);GlideUrl url = new GlideUrl(dbCapsule.getImage(), new LazyHeaders.Builder().addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE).addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid).addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret).addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created)).addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand)).build());RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL);Glide.with(getContext()).load(url).thumbnail().apply(options).into(imagetarjeta);}}/*if (progress != null) {double progre = progress.getProgress();if (progre >= 100) {Log.e("Es mayor"," a 100");} else {Log.e("Es nueva","");titulotarjeta.setText(dbCapsule.getName());progresotarjeta.setVisibility(View.INVISIBLE);progresoporcentaje.setVisibility(View.INVISIBLE);empezar.setText("Nueva");continuar.setText("Ver cápsula");iTwoGetSkills.getPreference().setCapsuleUuidActive(dbCapsule.getUuid());iTwoGetSkills.getPreference().setTopicUuidActive(dbCapsule.getTopicUuid());if (dbCapsule.getImage() != null && !dbCapsule.getImage().equals("")) {TimeZone timeZone = TimeZone.getTimeZone("UTC");Calendar calendar = Calendar.getInstance(timeZone);TimeZone tz = calendar.getTimeZone();int created = (int) (calendar.getTimeInMillis() / 1000);Random random = new Random(created);int rand = 1000 + random.nextInt(8999);String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();String password = iTwoGetSkills.getPreference().getPassword();String secret = MD5.generar(password + ':' + created + ':' + rand);GlideUrl url = new GlideUrl(dbCapsule.getImage(), new LazyHeaders.Builder().addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE).addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid).addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret).addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created)).addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand)).build());RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL);Glide.with(getContext()).load(url).thumbnail().apply(options).into(imagetarjeta);}}}*/}}continuar.setOnClickListener(v -> {iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_SLIDES);});//Fin de primer tarjeta}private void loadData(){TopicDao topicDao = iTwoGetSkills.getDatabase().getTopicDao();ArrayList<Topic> dbTopics = (ArrayList<Topic>) topicDao.selectAll();List<Capsule> dbCapsules;CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();for (Topic dbTopic : dbTopics) {dbCapsules = capsuleDao.selectAllByTopicUuid(dbTopic.getUuid());Capsule capsule;Progress progress;ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();HashMap<String, String> m_li;for(Capsule dbCapsule : dbCapsules){capsule = new Capsule();capsule.setTopicUuid(dbCapsule.getTopicUuid());capsule.setUuid(dbCapsule.getUuid());capsule.setName(dbCapsule.getName());capsule.setDescription(dbCapsule.getDescription());capsule.setImage(dbCapsule.getImage());capsule.setPosition(dbCapsule.getPosition());m_li = new HashMap<>();//m_li.put("copyright",copy);progress = progressDao.selectByCapsuleUuid(capsule.getUuid());if(progress != null) {capsule.setCompleted( progress.getCompleted());capsule.setViewSlides(progress.getViewSlides());capsule.setTotalSlides( progress.getTotalSlides());capsule.setProgress(progress.getProgress());m_li.put("view", String.valueOf(progress.getViewSlides()));m_li.put("total", String.valueOf(progress.getTotalSlides()));m_li.put("progress", String.valueOf(progress.getProgress()));}m_li.put("imagen",capsule.getImage());m_li.put("nombre",capsule.getName());m_li.put("uuid",capsule.getUuid());m_li.put("description",capsule.getDescription());capsuleList.add(m_li);}}}}