Skip to content

Commit

Permalink
refactor: Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jun 1, 2023
1 parent a3edab1 commit ccea903
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
31 changes: 16 additions & 15 deletions Mail/Components/ThreadCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ struct ThreadCellDataHolder {

struct ThreadCell: View {
@EnvironmentObject private var mailboxManager: MailboxManager

/// With normal or compact density, the checkbox should appear and disappear at different times of the cell offset.
@State private var shouldDisplayCheckbox = false

let thread: Thread

let dataHolder: ThreadCellDataHolder
Expand All @@ -82,8 +86,6 @@ struct ThreadCell: View {
let isMultipleSelectionEnabled: Bool
let isSelected: Bool

@State private var shouldDisplayCheckbox = false

private var checkboxSize: CGFloat {
density == .large ? UIConstants.checkboxLargeSize : UIConstants.checkboxSize
}
Expand All @@ -99,12 +101,7 @@ struct ThreadCell: View {
return label
}

init(
thread: Thread,
density: ThreadDensity,
isMultipleSelectionEnabled: Bool = false,
isSelected: Bool = false
) {
init(thread: Thread, density: ThreadDensity, isMultipleSelectionEnabled: Bool = false, isSelected: Bool = false) {
self.thread = thread

dataHolder = ThreadCellDataHolder(thread: thread)
Expand Down Expand Up @@ -134,6 +131,7 @@ struct ThreadCell: View {
} else if isMultipleSelectionEnabled {
CheckboxView(isSelected: isSelected, density: density)
.opacity(shouldDisplayCheckbox ? 1 : 0)
.animation(.default.speed(1.5), value: shouldDisplayCheckbox)
}
}
.padding(.trailing, 4)
Expand All @@ -148,7 +146,7 @@ struct ThreadCell: View {
}
}
.animation(
isMultipleSelectionEnabled ? .threadListSlide : .threadListSlide.delay(0.3),
isMultipleSelectionEnabled ? .threadListSlide : .threadListSlide.delay(UIConstants.checkboxDisappearOffsetDelay),
value: isMultipleSelectionEnabled
)
}
Expand All @@ -157,15 +155,18 @@ struct ThreadCell: View {
.padding(.vertical, density.cellVerticalPadding)
.clipped()
.accessibilityElement(children: .combine)
.onChange(of: isMultipleSelectionEnabled) { newValue in
if newValue {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
withAnimation {
.onChange(of: isMultipleSelectionEnabled) { isEnabled in
guard density != .large else { return }

withAnimation {
if isEnabled {
// We should wait a bit before showing the checkbox
DispatchQueue.main.asyncAfter(deadline: .now() + UIConstants.checkboxAppearDelay) {
self.shouldDisplayCheckbox = true
}
} else {
self.shouldDisplayCheckbox = false
}
} else {
self.shouldDisplayCheckbox = false
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions MailCore/UI/UIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public enum UIConstants {
public static let checkmarkSize: CGFloat = 14
public static let checkboxLargeSize: CGFloat = 40

public static let checkboxAppearDelay = 0.2
public static let checkboxDisappearOffsetDelay = 0.35

public static let buttonsRadius: CGFloat = 16
public static let buttonsIconSize: CGFloat = 16

Expand Down

0 comments on commit ccea903

Please sign in to comment.