Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
xx messenger iOS
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mobile
iOS
xx messenger iOS
Commits
0b7f47e3
Commit
0b7f47e3
authored
2 years ago
by
Dariusz Rybicki
Browse files
Options
Downloads
Patches
Plain Diff
Update collation in ContactListTableController
parent
32aacdad
No related branches found
No related tags found
1 merge request
!93
Fix connections list index
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Sources/ContactListFeature/ContactListTableController.swift
+57
-28
57 additions, 28 deletions
Sources/ContactListFeature/ContactListTableController.swift
with
57 additions
and
28 deletions
Sources/ContactListFeature/ContactListTableController.swift
+
57
−
28
View file @
0b7f47e3
...
...
@@ -5,11 +5,24 @@ import XXModels
import
AppResources
final
class
ContactListTableController
:
UITableViewController
{
private
var
collation
=
UILocalizedIndexedCollation
.
current
()
private
var
sections
:
[[
Contact
]]
=
[]
{
didSet
{
self
.
tableView
.
reloadData
()
}
private
final
class
Row
:
NSObject
{
init
(
contact
:
XXModels
.
Contact
)
{
self
.
contact
=
contact
self
.
title
=
contact
.
nickname
??
contact
.
username
??
""
}
let
contact
:
XXModels
.
Contact
@objc
let
title
:
String
}
private
struct
Section
{
var
title
:
String
var
rows
:
[
Row
]
}
private
var
collation
=
UILocalizedIndexedCollation
.
current
()
private
var
sections
:
[
Section
]
=
[]
private
let
viewModel
:
ContactListViewModel
private
var
cancellables
=
Set
<
AnyCancellable
>
()
private
let
tapRelay
=
PassthroughSubject
<
Contact
,
Never
>
()
...
...
@@ -37,18 +50,29 @@ final class ContactListTableController: UITableViewController {
viewModel
.
contacts
.
receive
(
on
:
DispatchQueue
.
main
)
.
sink
{
[
unowned
self
]
in
let
results
=
IndexedListCollator
()
.
sectioned
(
items
:
$0
)
self
.
collation
=
results
.
collation
self
.
sections
=
results
.
sections
.
sink
{
[
unowned
self
]
contacts
in
sections
=
makeSections
(
from
:
contacts
)
tableView
.
reloadData
()
}
.
store
(
in
:
&
cancellables
)
}
private
func
makeSections
(
from
contacts
:
[
XXModels
.
Contact
])
->
[
Section
]
{
let
rows
=
contacts
.
map
(
Row
.
init
(
contact
:))
let
selector
:
Selector
=
#selector
(
getter
:
Row
.
title
)
let
sortedRows
=
collation
.
sortedArray
(
from
:
rows
,
collationStringSelector
:
selector
)
as!
[
Row
]
var
sections
=
collation
.
sectionTitles
.
map
{
Section
(
title
:
$0
,
rows
:
[])
}
sortedRows
.
forEach
{
row
in
let
sectionNumber
=
collation
.
section
(
for
:
row
,
collationStringSelector
:
selector
)
sections
[
sectionNumber
]
.
rows
.
append
(
row
)
}
sections
.
removeAll
(
where
:
\
.
rows
.
isEmpty
)
return
sections
}
override
func
tableView
(
_
tableView
:
UITableView
,
cellForRowAt
indexPath
:
IndexPath
)
->
UITableViewCell
{
let
cell
:
AvatarCell
=
tableView
.
dequeueReusableCell
(
forIndexPath
:
indexPath
)
let
contact
=
sections
[
indexPath
.
section
][
indexPath
.
row
]
let
contact
=
sections
[
indexPath
.
section
]
.
rows
[
indexPath
.
row
]
.
contact
let
name
=
(
contact
.
nickname
??
contact
.
username
)
??
"Fetching username..."
cell
.
setup
(
title
:
name
,
image
:
contact
.
photo
)
return
cell
}
...
...
@@ -58,11 +82,12 @@ final class ContactListTableController: UITableViewController {
}
override
func
tableView
(
_
:
UITableView
,
numberOfRowsInSection
section
:
Int
)
->
Int
{
sections
[
section
]
.
count
sections
[
section
]
.
rows
.
count
}
override
func
tableView
(
_
:
UITableView
,
didSelectRowAt
indexPath
:
IndexPath
)
{
tapRelay
.
send
(
sections
[
indexPath
.
section
][
indexPath
.
row
])
let
contact
=
sections
[
indexPath
.
section
]
.
rows
[
indexPath
.
row
]
.
contact
tapRelay
.
send
(
contact
)
}
override
func
sectionIndexTitles
(
for
:
UITableView
)
->
[
String
]?
{
...
...
@@ -70,11 +95,15 @@ final class ContactListTableController: UITableViewController {
}
override
func
tableView
(
_
:
UITableView
,
titleForHeaderInSection
section
:
Int
)
->
String
?
{
collation
.
section
Title
s
[
section
]
sections
[
section
]
.
title
}
override
func
tableView
(
_
:
UITableView
,
sectionForSectionIndexTitle
:
String
,
at
index
:
Int
)
->
Int
{
collation
.
section
(
forSectionIndexTitle
:
index
)
if
let
index
=
sections
.
lastIndex
(
where
:
{
$0
.
title
<=
sectionForSectionIndexTitle
})
{
return
index
}
else
{
return
sections
.
index
(
before
:
sections
.
endIndex
)
}
}
override
func
tableView
(
_
:
UITableView
,
heightForRowAt
:
IndexPath
)
->
CGFloat
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment