Skip to content

Commit afa6973

Browse files
committed
Remove iteration over character set when generating headers
This was unnecessary and also previously caused repositories starting with numbers to be dropped from the headers. Closes pockethub#282
1 parent ecb86f2 commit afa6973

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

app/src/main/java/com/github/mobile/ui/repo/RepositoryListFragment.java

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -153,39 +153,57 @@ private void updateHeaders(final List<Repository> repos) {
153153
.getWrappedAdapter();
154154
adapter.clearHeaders();
155155

156-
char start = 'a';
157-
Repository previous = null;
158-
for (int i = 0; i < repos.size(); i++) {
159-
Repository repository = repos.get(i);
156+
if (repos.isEmpty())
157+
return;
160158

161-
if (recentRepos.contains(repository.getId())) {
162-
previous = repository;
163-
continue;
164-
}
159+
// Add recent header if at least one recent repository
160+
Repository first = repos.get(0);
161+
if (recentRepos.contains(first))
162+
adapter.registerHeader(first, getString(string.recently_viewed));
163+
164+
// Advance past all recent repositories
165+
int index;
166+
Repository current = null;
167+
for (index = 0; index < repos.size(); index++) {
168+
Repository repository = repos.get(index);
169+
if (recentRepos.contains(repository.getId()))
170+
current = repository;
171+
else
172+
break;
173+
}
174+
175+
if (index >= repos.size())
176+
return;
165177

166-
char repoStart = Character.toLowerCase(repository.getName().charAt(
167-
0));
168-
if (repoStart < start) {
169-
previous = repository;
178+
if (current != null)
179+
adapter.registerNoSeparator(current);
180+
181+
// Register header for first character
182+
current = repos.get(index);
183+
char start = Character.toLowerCase(current.getName().charAt(0));
184+
adapter.registerHeader(current,
185+
Character.toString(start).toUpperCase(US));
186+
187+
char previousHeader = start;
188+
for (index = index + 1; index < repos.size(); index++) {
189+
current = repos.get(index);
190+
char repoStart = Character.toLowerCase(current.getName().charAt(0));
191+
if (repoStart <= start)
170192
continue;
171-
}
172193

173-
adapter.registerHeader(repository, Character.toString(repoStart)
194+
// Don't include separator for the last element of the previous
195+
// character
196+
if (previousHeader != repoStart)
197+
adapter.registerNoSeparator(repos.get(index - 1));
198+
199+
adapter.registerHeader(current, Character.toString(repoStart)
174200
.toUpperCase(US));
175-
if (previous != null)
176-
adapter.registerNoSeparator(previous);
177-
start = repoStart;
178-
if (start == 'z')
179-
break;
180-
start++;
181-
previous = repository;
201+
previousHeader = repoStart;
202+
start = repoStart++;
182203
}
183204

184-
if (!repos.isEmpty()) {
185-
Repository first = repos.get(0);
186-
if (recentRepos.contains(first))
187-
adapter.registerHeader(first, getString(string.recently_viewed));
188-
}
205+
// Don't include separator for last element
206+
adapter.registerNoSeparator(repos.get(repos.size() - 1));
189207
}
190208

191209
@Override

0 commit comments

Comments
 (0)