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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
package cgeo.geocaching.ui.logs;
import cgeo.geocaching.CacheDetailActivity;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.LogEntry;
import cgeo.geocaching.R;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.ui.UserActionsClickListener;
import org.apache.commons.lang3.StringUtils;
import android.content.res.Resources;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class CacheLogsViewCreator extends LogsViewCreator {
private final boolean allLogs;
private final Resources res = CgeoApplication.getInstance().getResources();
private final CacheDetailActivity cacheDetailActivity;
public CacheLogsViewCreator(final CacheDetailActivity cacheDetailActivity, final boolean allLogs) {
super(cacheDetailActivity);
this.cacheDetailActivity = cacheDetailActivity;
this.allLogs = allLogs;
}
private Geocache getCache() {
return cacheDetailActivity.getCache();
}
@Override
protected List<LogEntry> getLogs() {
final Geocache cache = getCache();
final List<LogEntry> logs = allLogs ? cache.getLogs() : cache.getFriendsLogs();
return addOwnOfflineLog(cache, logs);
}
private List<LogEntry> addOwnOfflineLog(final Geocache cache, final List<LogEntry> logsIn) {
final LogEntry log = DataStore.loadLogOffline(cache.getGeocode());
final ArrayList<LogEntry> logs = new ArrayList<>(logsIn);
if (log != null) {
log.author = res.getString(R.string.log_your_saved_log);
logs.add(0, log);
}
return logs;
}
@Override
protected void addHeaderView() {
// adds the log counts
final Map<LogType, Integer> logCounts = getCache().getLogCounts();
if (logCounts != null) {
final List<Entry<LogType, Integer>> sortedLogCounts = new ArrayList<>(logCounts.size());
for (final Entry<LogType, Integer> entry : logCounts.entrySet()) {
// it may happen that the label is unknown -> then avoid any output for this type
if (entry.getKey() != LogType.PUBLISH_LISTING && entry.getValue() != 0) {
sortedLogCounts.add(entry);
}
}
if (!sortedLogCounts.isEmpty()) {
// sort the log counts by type id ascending. that way the FOUND, DNF log types are the first and most visible ones
Collections.sort(sortedLogCounts, new Comparator<Entry<LogType, Integer>>() {
@Override
public int compare(final Entry<LogType, Integer> logCountItem1, final Entry<LogType, Integer> logCountItem2) {
return logCountItem1.getKey().compareTo(logCountItem2.getKey());
}
});
final ArrayList<String> labels = new ArrayList<>(sortedLogCounts.size());
for (final Entry<LogType, Integer> pair : sortedLogCounts) {
labels.add(pair.getValue() + "× " + pair.getKey().getL10n());
}
final TextView countView = new TextView(activity);
countView.setText(res.getString(R.string.cache_log_types) + ": " + StringUtils.join(labels, ", "));
view.addHeaderView(countView, null, false);
}
}
}
@Override
protected void fillCountOrLocation(final LogViewHolder holder, final LogEntry log) {
// finds count
if (log.found == -1) {
holder.countOrLocation.setVisibility(View.GONE);
} else {
holder.countOrLocation.setVisibility(View.VISIBLE);
holder.countOrLocation.setText(res.getQuantityString(R.plurals.cache_counts, log.found, log.found));
}
}
@Override
protected void fillViewHolder(final View convertView, final LogViewHolder holder, final LogEntry log) {
super.fillViewHolder(convertView, holder, log);
if (null == convertView) {
if (isOfflineLog(log)) {
holder.author.setOnClickListener(new EditOfflineLogListener(getCache(), cacheDetailActivity));
holder.text.setOnClickListener(new EditOfflineLogListener(getCache(), cacheDetailActivity));
}
}
}
private boolean isOfflineLog(final LogEntry log) {
return log.author.equals(activity.getString(R.string.log_your_saved_log));
}
@Override
protected boolean isValid() {
return getCache() != null;
}
@Override
protected String getGeocode() {
return getCache().getGeocode();
}
@Override
protected UserActionsClickListener createUserActionsListener() {
return new UserActionsClickListener(getCache());
}
}
|