Proyectos de Subversion Android Microlearning

Rev

Rev 39 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

package com.cesams.twogetskills.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

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.dao.DatabaseHelper;
import com.cesams.twogetskills.dao.ProgressDao;
import com.cesams.twogetskills.dao.SyncDao;
import com.cesams.twogetskills.dao.UserLogDao;
import com.cesams.twogetskills.entity.Progress;
import com.cesams.twogetskills.entity.Slide;
import com.cesams.twogetskills.entity.Sync;
import com.cesams.twogetskills.entity.UserLog;
import com.cesams.twogetskills.library.MD5;
import com.cesams.twogetskills.skeleton.ITwoGetSkills;


import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.TimeZone;

public class GalleryViewPageAdapter extends RecyclerView.Adapter<GalleryViewPageAdapter.ViewHolder>  {
    private final static String TAG = "C2GS - GalleryAdapter";
    private List<Slide> mData;
    private LayoutInflater mInflater;
    private ITwoGetSkills iTwoGetSkills;
    private Context mContext;
    private Slide mItemCurrent;



    // data is passed into the constructor
    public  GalleryViewPageAdapter(FragmentActivity context, List<Slide> data) {
        this.mContext = context;
        this.iTwoGetSkills = (ITwoGetSkills) context;
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.fragment_gallery_viewpager_item, parent, false);
        return new ViewHolder(view);
    }

    // binds the data to the TextView in each row
    @SuppressLint("NewApi")
    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        holder.mCheckReady.setVisibility(View.GONE);
        holder.mButtonFinish.setVisibility(View.GONE);
        holder.mButtonViewer.setVisibility(View.GONE);

        mItemCurrent = mData.get(position);

        if(mItemCurrent.isShowFinish()) {
            holder.mButtonFinish.setVisibility(View.VISIBLE);
        }
        if(mItemCurrent.getCompleted()== 1) {
            holder.mCheckReady.setVisibility(View.VISIBLE);
        }

        boolean setImageViewerControl = false;
        switch(mItemCurrent.getType()) {

            case Constants.SLIDE_TYPE_TEXT :

                holder.mButtonViewer.setVisibility(View.VISIBLE);
                holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_text));
                break;


            case Constants.SLIDE_TYPE_AUDIO :

                holder.mButtonViewer.setVisibility(View.VISIBLE);
                holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_audio));
                break;

            case Constants.SLIDE_TYPE_DOCUMENT :

                holder.mButtonViewer.setVisibility(View.VISIBLE);
                holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_document));
                break;

            case Constants.SLIDE_TYPE_VIDEO :

                holder.mButtonViewer.setVisibility(View.VISIBLE);
                holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_video));


                break;


            case Constants.SLIDE_TYPE_QUIZ :
                if(mItemCurrent.getCompleted()  == 0) {

                    holder.mButtonViewer.setVisibility(View.VISIBLE);
                    holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_quiz));
                } else {
                    holder.mButtonViewer.setVisibility(View.INVISIBLE);
                }
                break;

            default :
                holder.mButtonViewer.setVisibility(View.INVISIBLE);
                break;

        }

        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();

        Log.d(TAG, "token = " + deviceUuid);
        Log.d(TAG, "created = " + created);
        Log.d(TAG, "rand = " + rand);
        Log.d(TAG, "calc = " + password + ':' +  created + ':' + rand);

        String image = TextUtils.isEmpty(mItemCurrent.getBackground()) ?mItemCurrent.getFile() : mItemCurrent.getBackground();
        Log.d(TAG, "gallery slide image = " + image);

        String secret = MD5.generar(password + ':' +  created + ':' + rand);

        GlideUrl url = new GlideUrl(image, 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(mContext).load(url)
                .thumbnail()
                .apply(options)
                .into(holder.mImage);

        holder.mButtonViewer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              //  Toast.makeText(view.getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
                launchViewer();
            }
        });

    }




    // total number of rows
    @Override
    public int getItemCount() {
        return mData.size();
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mImage;
        ImageView mCheckReady;
        Button mButtonViewer;
        Button mButtonFinish;


        ViewHolder(View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.fragment_gallery_viewpage_item_image);
            mCheckReady = itemView.findViewById(R.id.fragment_gallery_viewpage_item_check_ready);
            mButtonViewer = itemView.findViewById(R.id.fragment_gallery_viewpage_item_button_viewer);
            mButtonViewer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    launchViewer();
                }
            });



            mButtonFinish = itemView.findViewById(R.id.fragment_gallery_viewpage_item_button_finish);
            mButtonFinish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finishCapsuleAndOrTopic();
                }
            });
        }

    }

    public void launchViewer()
    {

        switch (mItemCurrent.getType()) {
            case Constants.SLIDE_TYPE_TEXT:

                iTwoGetSkills.launchTextViewer(mItemCurrent.getDescription());
                break;

            case Constants.SLIDE_TYPE_AUDIO:

                iTwoGetSkills.launchAudioViewer(mItemCurrent.getFile());
                break;

            case Constants.SLIDE_TYPE_DOCUMENT:
                iTwoGetSkills.launchDocumentViewer(mItemCurrent.getFile());

                break;

            case Constants.SLIDE_TYPE_VIDEO:

                iTwoGetSkills.launchVideoViewer(mItemCurrent.getFile());
                break;


            case Constants.SLIDE_TYPE_QUIZ:
                iTwoGetSkills.launchQuizViewer(mItemCurrent.getQuizUuid());

                break;
        }
    }

    public void finishCapsuleAndOrTopic()
    {
        String userUuid = iTwoGetSkills.getPreference().getUserUuid();
        UserLogDao userLogDao = iTwoGetSkills.getDatabase().getUserLogDao();
        SyncDao syncDao = iTwoGetSkills.getDatabase().getSyncDao();
        ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();

        Progress progressCapsule = progressDao.selectByCapsuleUuidAndUserUuid(mItemCurrent.getCapsuleUuid(), userUuid);
        Progress progressTopic = progressDao.selectByTopicUuidAndUserUuid(mItemCurrent.getTopicUuid(), userUuid);

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

        UserLog userLog;
        Sync sync;

        int toInvokeFragment = Constants.IDX_FRAGMENT_FINISH_TOPIC;

        if(progressCapsule.getProgress() >= 100 && progressCapsule.getCompleted() == 0) {
            toInvokeFragment = Constants.IDX_FRAGMENT_FINISH_CAPSULE;

            userLog = new UserLog();
            userLog.setUserUuid(iTwoGetSkills.getPreference().getUserUuid());
            userLog.setActivity(Constants.USER_LOG_ACTIVITY_COMPLETED_CAPSULE);
            userLog.setCompanyUuid(progressCapsule.getCompanyUuid());
            userLog.setTopicUuid(progressCapsule.getTopicUuid());
            userLog.setCapsuleUuid(progressCapsule.getCapsuleUuid());
            userLog.setAddedOn(dateOn);
            userLogDao.insert(userLog);

            try {
                JSONObject json = userLog.toJson();
                json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);

                sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC,json.toString());
                syncDao.insert(sync);

            } catch (JSONException e) {
                Log.d(TAG, e.getMessage());
            }

            progressCapsule.setCompleted(1);
            progressDao.update(progressCapsule);

            try {
                JSONObject json = progressCapsule.toJson();
                json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS);

                sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
                syncDao.insert(sync);

            } catch (JSONException e) {
                Log.d(TAG, e.getMessage());
            }
        }


        if (progressTopic.getProgress() >= 100 && progressTopic.getCompleted() == 0) {
            toInvokeFragment = Constants.IDX_FRAGMENT_FINISH_TOPIC;

            userLog = new UserLog();
            userLog.setUserUuid(iTwoGetSkills.getPreference().getUserUuid());
            userLog.setActivity(Constants.USER_LOG_ACTIVITY_COMPLETED_TOPIC);
            userLog.setCompanyUuid(progressTopic.getCompanyUuid());
            userLog.setTopicUuid(progressTopic.getTopicUuid());
            userLog.setAddedOn(dateOn);
            userLogDao.insert(userLog);

            try {
                JSONObject json = userLog.toJson();
                json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);

                sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
                syncDao.insert(sync);

            } catch (JSONException e) {
                Log.d(TAG, e.getMessage());
            }

            progressTopic.setCompleted(1);
            progressDao.update(progressTopic);

            try {
                JSONObject json = progressTopic.toJson();
                json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS);

                sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC,json.toString());
                syncDao.insert(sync);
            } catch (JSONException e) {
                Log.d(TAG, e.getMessage());
            }
        }

        iTwoGetSkills.requestExecuteSyncAdapter();
        iTwoGetSkills.invokeFragment(toInvokeFragment);
    }
}