ir.isilearning.lmsapp.adapter.CategoryAdapter.java Source code

Java tutorial

Introduction

Here is the source code for ir.isilearning.lmsapp.adapter.CategoryAdapter.java

Source

/*
 * Copyright 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ir.isilearning.lmsapp.adapter;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import ir.isilearning.lmsapp.R;
import ir.isilearning.lmsapp.helper.ApiLevelHelper;
import ir.isilearning.lmsapp.helper.DateHelper;
import ir.isilearning.lmsapp.helper.JalaliCalendar;
import ir.isilearning.lmsapp.helper.JsonHelper;
import ir.isilearning.lmsapp.helper.PersianTextHelper;
import ir.isilearning.lmsapp.model.Category;
import ir.isilearning.lmsapp.model.ExamDetail.UserSelectedExam;
import ir.isilearning.lmsapp.model.Theme;
import ir.isilearning.lmsapp.persistence.LMSAppDatabaseHelper;
import ir.isilearning.lmsapp.webservice.gmodel.gUserExamDetail;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {

    public static final String DRAWABLE = "drawable";
    private static final String ICON_CATEGORY = "icon_category_";
    private final Resources mResources;
    private final String mPackageName;
    private final LayoutInflater mLayoutInflater;
    private final Activity mActivity;
    private List<gUserExamDetail> mExamDetailList;
    private List<Category> mCategories;

    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onClick(View view, int position);
    }

    public CategoryAdapter(Activity activity) {
        mActivity = activity;
        mResources = mActivity.getResources();
        mPackageName = mActivity.getPackageName();
        mLayoutInflater = LayoutInflater.from(activity.getApplicationContext());
        updateCategories(activity);
    }

    public CategoryAdapter(Activity activity, List<gUserExamDetail> userExamDetailList) {
        mActivity = activity;
        mResources = mActivity.getResources();
        mPackageName = mActivity.getPackageName();
        mLayoutInflater = LayoutInflater.from(activity.getApplicationContext());
        mExamDetailList = userExamDetailList;
        updateCategories(activity);
    }

    public void UpdateQuizesForCategory(String userselectedexamJson, Activity activity) {
        UserSelectedExam str = JsonHelper.GenerateUserSelectedExamObject(userselectedexamJson);
        Category selectedcategory = (JsonHelper.ConvertUserSelectedExamTOCategory(str));
        LMSAppDatabaseHelper.removequizezandinsertnewones(activity, selectedcategory);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.item_category, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Category category = mCategories.get(position);
        Theme theme = category.getTheme();
        setCategoryIcon(category, holder.catIcon);
        holder.itemView.setBackgroundColor(getColor(theme.getWindowBackgroundColor()));
        holder.catTitle.setText(category.getName());
        holder.catTitle.setTextColor(getColor(theme.getTextPrimaryColor()));
        holder.catTitle.setBackgroundColor(getColor(theme.getPrimaryColor()));
        holder.catStatus.setText(PersianTextHelper.GetPersianCategoryStatus(category.getCategoryStatus()));
        holder.catStatus.setTextColor(getColor(theme.getTextPrimaryColor()));
        //        holder.catStartDate.setText((DateHelper.ConvertTojalaliDate(category.getCategorystartDate())));
        //        holder.catStartDate.setTextColor(getColor(theme.getTextPrimaryColor()));
        //        holder.catStartDate.setBackgroundColor(getColor(theme.getPrimaryColor()));
        //        holder.catEndDate.setText(DateHelper.ConvertTojalaliDate(category.getCateoryendDate()));
        //        holder.catEndDate.setTextColor(getColor(theme.getTextPrimaryColor()));
        //        holder.catEndDate.setBackgroundColor(getColor(theme.getPrimaryColor()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onClick(v, position);
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return mCategories.get(position).getId().hashCode();
    }

    @Override
    public int getItemCount() {
        return mCategories.size();
    }

    public Category getItem(int position) {
        return mCategories.get(position);
    }

    /**
     * @param id Id of changed category.
     * @see android.support.v7.widget.RecyclerView.Adapter#notifyItemChanged(int)
     */
    public final void notifyItemChanged(String id) {
        updateCategories(mActivity);
        notifyItemChanged(getItemPositionById(id));
    }

    private int getItemPositionById(String id) {
        for (int i = 0; i < mCategories.size(); i++) {
            if (mCategories.get(i).getId().equals(id)) {
                return i;
            }

        }
        return -1;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    private void setCategoryIcon(Category category, ImageView icon) {
        final int categoryImageResource = mResources.getIdentifier(ICON_CATEGORY + "knowledge", DRAWABLE,
                mPackageName);
        //        final int categoryImageResource = mResources.getIdentifier(
        //                ICON_CATEGORY + category.getId(), DRAWABLE, mPackageName);
        final boolean solved = category.isSolved();
        if (solved) {
            Drawable solvedIcon = loadSolvedIcon(category, categoryImageResource);
            icon.setImageDrawable(solvedIcon);
        } else {
            icon.setImageResource(categoryImageResource);
        }
    }

    private void updateCategories(Activity activity) { // the category being loaded
        Gson gson = new Gson();
        List<Category> catrgoylist = JsonHelper.GetUserExamsAndConvertToCategory(mExamDetailList);
        mCategories = LMSAppDatabaseHelper.getCategories(activity, true, gson.toJson(catrgoylist));

    }

    /**
     * Loads an catIcon that indicates that a category has already been solved.
     *
     * @param category              The solved category to display.
     * @param categoryImageResource The category's identifying image.
     * @return The catIcon indicating that the category has been solved.
     */
    private Drawable loadSolvedIcon(Category category, int categoryImageResource) {
        if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
            return loadSolvedIconLollipop(category, categoryImageResource);
        }
        return loadSolvedIconPreLollipop(category, categoryImageResource);
    }

    @NonNull
    private LayerDrawable loadSolvedIconLollipop(Category category, int categoryImageResource) {
        final Drawable done = loadTintedDoneDrawable();
        final Drawable categoryIcon = loadTintedCategoryDrawable(category, categoryImageResource);
        Drawable[] layers = new Drawable[] { categoryIcon, done }; // ordering is back to front
        return new LayerDrawable(layers);
    }

    private Drawable loadSolvedIconPreLollipop(Category category, int categoryImageResource) {
        return loadTintedCategoryDrawable(category, categoryImageResource);
    }

    /**
     * Loads and tints a drawable.
     *
     * @param category              The category providing the tint color
     * @param categoryImageResource The image resource to tint
     * @return The tinted resource
     */
    private Drawable loadTintedCategoryDrawable(Category category, int categoryImageResource) {
        final Drawable categoryIcon = ContextCompat.getDrawable(mActivity, categoryImageResource).mutate();
        return wrapAndTint(categoryIcon, category.getTheme().getPrimaryColor());
    }

    /**
     * Loads and tints a check mark.
     *
     * @return The tinted check mark
     */
    private Drawable loadTintedDoneDrawable() {
        final Drawable done = ContextCompat.getDrawable(mActivity, R.drawable.ic_tick);
        return wrapAndTint(done, android.R.color.white);
    }

    private Drawable wrapAndTint(Drawable done, @ColorRes int color) {
        Drawable compatDrawable = DrawableCompat.wrap(done);
        DrawableCompat.setTint(compatDrawable, getColor(color));
        return compatDrawable;
    }

    /**
     * Convenience method for color loading.
     *
     * @param colorRes The resource id of the color to load.
     * @return The loaded color.
     */
    private int getColor(@ColorRes int colorRes) {
        return ContextCompat.getColor(mActivity, colorRes);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        final ImageView catIcon;
        final TextView catTitle;
        final TextView catStartDate;
        final TextView catEndDate;
        final TextView catStatus;

        public ViewHolder(View container) {
            super(container);
            catIcon = (ImageView) container.findViewById(R.id.category_icon);
            catTitle = (TextView) container.findViewById(R.id.category_title);
            catStartDate = (TextView) container.findViewById(R.id.category_StartDate);
            catEndDate = (TextView) container.findViewById(R.id.category_EndDate);
            catStatus = (TextView) container.findViewById(R.id.category_Status);
        }
    }
}