Skip to content

Commit

Permalink
Fix Swiftlint warnings in DeviceManagementContentView
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Petersson authored and buggmagnet committed Sep 8, 2023
1 parent 4fc842f commit 60c25f9
Showing 1 changed file with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,7 @@ class DeviceManagementContentView: UIView {
difference.forEach { change in
switch change {
case let .insert(offset, model, _):
let view = DeviceRowView(viewModel: model)

view.isHidden = true
view.alpha = 0

view.deleteHandler = { [weak self] _ in
view.showsActivityIndicator = true

self?.handleDeviceDeletion?(view.viewModel) {
view.showsActivityIndicator = false
}
}

viewsToAdd.append((view, offset))

viewsToAdd.append((createDeviceRowView(from: model), offset))
case let .remove(offset, _, _):
viewsToRemove.append(deviceStackView.arrangedSubviews[offset])
}
Expand All @@ -226,41 +212,58 @@ class DeviceManagementContentView: UIView {
}
}

let showHideViews = {
viewsToRemove.forEach { view in
view.alpha = 0
view.isHidden = true
}

viewsToAdd.forEach { item in
item.view.alpha = 1
item.view.isHidden = false
}
}

let removeViews = {
viewsToRemove.forEach { view in
view.removeFromSuperview()
}
}

if animated {
UIView.animate(
withDuration: 0.25,
delay: 0,
options: [.curveEaseInOut],
animations: { [weak self] in
showHideViews()
self?.showHideViews(viewsToAdd: viewsToAdd, viewsToRemove: viewsToRemove)
self?.deviceStackView.layoutIfNeeded()
},
completion: { _ in
removeViews()
completion: { [weak self] _ in
self?.removeViews(viewsToRemove: viewsToRemove)
}
)
} else {
showHideViews()
removeViews()
showHideViews(viewsToAdd: viewsToAdd, viewsToRemove: viewsToRemove)
removeViews(viewsToRemove: viewsToRemove)
}
}

private func showHideViews(viewsToAdd: [(view: UIView, offset: Int)], viewsToRemove: [UIView]) {
viewsToRemove.forEach { view in
view.alpha = 0
view.isHidden = true
}

viewsToAdd.forEach { item in
item.view.alpha = 1
item.view.isHidden = false
}
}

private func removeViews(viewsToRemove: [UIView]) {
viewsToRemove.forEach { view in
view.removeFromSuperview()
}
}

private func createDeviceRowView(from model: DeviceViewModel) -> DeviceRowView {
let view = DeviceRowView(viewModel: model)

view.isHidden = true
view.alpha = 0

view.deleteHandler = { [weak self] _ in
view.showsActivityIndicator = true

self?.handleDeviceDeletion?(view.viewModel) {
view.showsActivityIndicator = false
}
}

return view
}

private func updateView() {
Expand Down

0 comments on commit 60c25f9

Please sign in to comment.