com.android.projectz.teamrocket.thebusapp.adapters.MainRecyclerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.android.projectz.teamrocket.thebusapp.adapters.MainRecyclerAdapter.java

Source

package com.android.projectz.teamrocket.thebusapp.adapters;
/*
 Copyright (C) 2016-2017  TeamRocket
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.android.projectz.teamrocket.thebusapp.R;
import com.android.projectz.teamrocket.thebusapp.activities.DetailViewerActivity;
import com.android.projectz.teamrocket.thebusapp.activities.MainActivity;
import com.android.projectz.teamrocket.thebusapp.local.DbAdapter;
import com.android.projectz.teamrocket.thebusapp.settings.SettingGeneralActivity;
import com.android.projectz.teamrocket.thebusapp.util.SharedPreferencesUtils;

import org.json.JSONException;

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

/**
 * MainRecyclerAdapter:
 * questa classe deve visualizzare gli oggetti che vengono visualizzati
 * dentro il contentitore, qui inoltre ci sono le informazioni
 * per la visualizzazione degli orari principale
 *
 * @author simone98dm
 * @data 28/10/2016
 */
public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.ViewHolder> {

    private static final String TAG = MainRecyclerAdapter.class.getSimpleName();
    public static MaterialDialog loading;
    Context context;
    ArrayList<MainRecyclerObjectProvider.MyObject> myObjects = new ArrayList<>();
    private Drawable image = null;

    //costruttore dove si passa il context e si crea l'oggetto myObject
    public MainRecyclerAdapter(Context context, String result) throws JSONException {
        this.context = context;
        myObjects = new MainRecyclerObjectProvider(context, result).getObjects();
    }

    @Override
    public void onViewDetachedFromWindow(ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.itemView.clearAnimation();
    }

    //si prende la view row e la ritorna
    @Override
    public MainRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.row, parent, false);
        return new ViewHolder(view);
    }

    //metodo per l'assegnazione dei valori all'interno della card
    @Override
    public void onBindViewHolder(final MainRecyclerAdapter.ViewHolder holder, final int position) {
        int lastPosition = -1;
        if (position > lastPosition) {
            Animation animation = AnimationUtils.loadAnimation(context,
                    (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
            holder.itemView.startAnimation(animation);
            lastPosition = position;
        }

        if (SharedPreferencesUtils.getSelectedTheme(context).equals("ThemeDark")) {
            holder.cardView.setBackgroundColor(ContextCompat.getColor(context, R.color.cardview_dark_background));
            holder.txtTitle.setTextColor(ContextCompat.getColor(context, android.R.color.primary_text_dark));
            holder.txtSubtitle.setTextColor(ContextCompat.getColor(context, android.R.color.secondary_text_dark));
            holder.txtOther.setTextColor(ContextCompat.getColor(context, android.R.color.secondary_text_dark));
            image = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.ic_arrow_row));
            DrawableCompat.setTint(image, context.getResources().getColor(R.color.iconDark));
        } else {
            holder.cardView.setBackgroundColor(ContextCompat.getColor(context, R.color.cardview_light_background));
            holder.txtTitle.setTextColor(ContextCompat.getColor(context, android.R.color.primary_text_light));
            holder.txtSubtitle.setTextColor(ContextCompat.getColor(context, android.R.color.secondary_text_light));
            holder.txtOther.setTextColor(ContextCompat.getColor(context, android.R.color.secondary_text_light));
            image = DrawableCompat.wrap(ContextCompat.getDrawable(context, R.drawable.ic_arrow_row));
            DrawableCompat.setTint(image, context.getResources().getColor(R.color.iconLight));
        }

        holder.txtTitle.setText(myObjects.get(position).getTextTitle());
        holder.txtSubtitle.setText(myObjects.get(position).getTextSubtitle());
        holder.txtOther.setText(myObjects.get(position).getTextOther());
        holder.imageView.setImageResource(myObjects.get(position).getImageId());
        holder.arrowRow.setImageDrawable(image);

        //ascoltatore del *tap* sulla card
        holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Loading...", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, DetailViewerActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); //senza di esso va in crash, perch bisogna creare l'intent come se fosse una nuova activity
                intent.putExtra("title", myObjects.get(position).getTextTitle());
                intent.putExtra("subtitle", myObjects.get(position).getTextSubtitle());
                intent.putExtra("other", myObjects.get(position).getTextOther());
                intent.putExtra("fermataId", myObjects.get(position).getFermataId());
                ArrayList<String> holderData = new ArrayList<String>();
                holderData.add(myObjects.get(position).getTextTitle());
                holderData.add(myObjects.get(position).getTextSubtitle());
                holderData.add(myObjects.get(position).getTextOther());
                intent.putStringArrayListExtra("holderData", holderData);
                context.startActivity(intent);
            }
        });

        //MainActivity.recyclerView.scrollToPosition(myObjects.get(position).isNear != -1 ? position : null);
    }

    //serve per dare un'idea di elementi da dare
    @Override
    public int getItemCount() {
        return myObjects.size();
    }

    //l'Holder ha il compito di prendere dal row.xml i componenti text o immagini
    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView, arrowRow;
        TextView txtTitle, txtSubtitle, txtOther;
        RelativeLayout relativeLayout;
        CardView cardView;

        public ViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.my_cardView);
            imageView = (ImageView) itemView.findViewById(R.id.icon);
            arrowRow = (ImageView) itemView.findViewById(R.id.arrowRow);
            txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
            txtSubtitle = (TextView) itemView.findViewById(R.id.txtSubtitle);
            txtOther = (TextView) itemView.findViewById(R.id.txtOther);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relative_layout);
        }
    }
}