/* * Copyright (c) 2017 m2049r * * 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 com.m2049r.xmrwallet.layout; import android.graphics.Color; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.m2049r.xmrwallet.R; import com.m2049r.xmrwallet.model.TransactionInfo; import com.m2049r.xmrwallet.model.Wallet; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.TimeZone; public class TransactionInfoAdapter extends RecyclerView.Adapter { private static final String TAG = "TransactionInfoAdapter"; private final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd"); private final SimpleDateFormat TIME_FORMATTER = new SimpleDateFormat("HH:mm:ss"); static final int TX_RED = Color.rgb(255, 79, 65); static final int TX_GREEN = Color.rgb(54, 176, 91); static final int TX_PENDING = Color.rgb(72, 53, 176); static final int TX_FAILED = Color.rgb(208, 0, 255); public interface OnInteractionListener { void onInteraction(View view, TransactionInfo item); } private final List infoItems; private final OnInteractionListener listener; public TransactionInfoAdapter(OnInteractionListener listener) { this.infoItems = new ArrayList<>(); this.listener = listener; Calendar cal = Calendar.getInstance(); TimeZone tz = cal.getTimeZone(); //get the local time zone. DATE_FORMATTER.setTimeZone(tz); TIME_FORMATTER.setTimeZone(tz); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.transaction_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, int position) { holder.bind(position); } @Override public int getItemCount() { return infoItems.size(); } public void setInfos(List data) { // TODO do stuff with data so we can really recycle elements (i.e. add only new tx) // as the TransactionInfo items are always recreated, we cannot recycle this.infoItems.clear(); if (data != null) { Log.d(TAG, "setInfos " + data.size()); // sort by block height Collections.sort(data, new Comparator() { @Override public int compare(TransactionInfo o1, TransactionInfo o2) { long b1 = o1.timestamp; long b2 = o2.timestamp; if (b1>b2) { return -1; } else if (b1