From 90ca8b34d823eed19d26f6611716ca231d60424c Mon Sep 17 00:00:00 2001 From: Renard Date: Tue, 5 Apr 2016 01:52:13 -0300 Subject: Initial commit --- .../baiparser/RecentPostAdapter.java | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 app/src/main/java/org/bienvenidoainternet/baiparser/RecentPostAdapter.java (limited to 'app/src/main/java/org/bienvenidoainternet/baiparser/RecentPostAdapter.java') diff --git a/app/src/main/java/org/bienvenidoainternet/baiparser/RecentPostAdapter.java b/app/src/main/java/org/bienvenidoainternet/baiparser/RecentPostAdapter.java new file mode 100644 index 0000000..e67f276 --- /dev/null +++ b/app/src/main/java/org/bienvenidoainternet/baiparser/RecentPostAdapter.java @@ -0,0 +1,90 @@ +package org.bienvenidoainternet.baiparser; + +import android.content.Context; +import android.text.Html; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import org.bienvenidoainternet.baiparser.structure.BoardItem; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.EnumSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * Created by Renard on 21-03-2016. + */ +public class RecentPostAdapter extends ArrayAdapter { + + public RecentPostAdapter(Context context, List objects) { + super(context, 0, objects); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + LayoutInflater inflater = (LayoutInflater)getContext() + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View listItemView = convertView; + if (null == convertView) { + listItemView = inflater.inflate( + R.layout.recentpost_item, + parent, + false); + } + final BoardItem postItem = getItem(position); + TextView rp_message = (TextView) listItemView.findViewById(R.id.rp_message); + TextView rp_title = (TextView) listItemView.findViewById(R.id.rp_title); + TextView rp_timediff = (TextView) listItemView.findViewById(R.id.rp_timediff); + if (postItem.getParentBoard() != null){ + rp_title.setText(postItem.getParentBoard().getBoardName() + ": " + postItem.getSubject()); + }else{ + rp_title.setText(postItem.getSubject()); + } + rp_message.setText(Html.fromHtml(postItem.getMessage())); + Map timeDiff = computeDiff(new Date(postItem.getTimeStamp() * 1000L), new Date(System.currentTimeMillis())); + String strTimeDiff = ""; + if (timeDiff.get(TimeUnit.SECONDS) != 0){ + strTimeDiff = "Hace " + timeDiff.get(TimeUnit.SECONDS) + (timeDiff.get(TimeUnit.SECONDS) == 1 ? " segundo" : " segundos"); + } + + if (timeDiff.get(TimeUnit.MINUTES) != 0){ + strTimeDiff = "Hace " + timeDiff.get(TimeUnit.MINUTES) + (timeDiff.get(TimeUnit.MINUTES) == 1 ? " minuto" : " minutos"); + } + + if (timeDiff.get(TimeUnit.HOURS) != 0){ + strTimeDiff = "Hace " + timeDiff.get(TimeUnit.HOURS) + (timeDiff.get(TimeUnit.HOURS) == 1 ? " hora" : " horas"); + } + + if (timeDiff.get(TimeUnit.DAYS) != 0){ + strTimeDiff = "Hace " + timeDiff.get(TimeUnit.DAYS) + (timeDiff.get(TimeUnit.DAYS) == 1 ? " día" : " días"); + } + rp_timediff.setText(strTimeDiff); + + + return listItemView; + } + + + public static Map computeDiff(Date date1, Date date2) { + long diffInMillies = date2.getTime() - date1.getTime(); + List units = new ArrayList(EnumSet.allOf(TimeUnit.class)); + Collections.reverse(units); + Map result = new LinkedHashMap(); + long milliesRest = diffInMillies; + for ( TimeUnit unit : units ) { + long diff = unit.convert(milliesRest,TimeUnit.MILLISECONDS); + long diffInMilliesForUnit = unit.toMillis(diff); + milliesRest = milliesRest - diffInMilliesForUnit; + result.put(unit,diff); + } + return result; + } +} -- cgit v1.2.1-18-gbd029