aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/bienvenidoainternet/baiparser/RecentPostAdapter.java
blob: e67f276eee668749d5aa54f747427f88cc713c46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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<BoardItem> {

    public RecentPostAdapter(Context context, List<BoardItem> 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<TimeUnit,Long> 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<TimeUnit,Long> computeDiff(Date date1, Date date2) {
        long diffInMillies = date2.getTime() - date1.getTime();
        List<TimeUnit> units = new ArrayList<TimeUnit>(EnumSet.allOf(TimeUnit.class));
        Collections.reverse(units);
        Map<TimeUnit,Long> result = new LinkedHashMap<TimeUnit,Long>();
        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;
    }
}