Skip to content

Commit

Permalink
Use colorScheme-based adaptive color utility (#3223)
Browse files Browse the repository at this point in the history
Co-authored-by: crow <david.crow@airship.com>
  • Loading branch information
crow and crow authored Oct 3, 2024
1 parent 39a4dac commit c893f64
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 35 deletions.
11 changes: 0 additions & 11 deletions Airship/AirshipMessageCenter/Source/Model/MessageCenterUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,3 @@ extension ProgressView {
}
}
}

extension Color {
func adaptiveColor(for colorScheme: ColorScheme, darkVariation: Color?) -> Color? {
if colorScheme == .light {
return self
} else {
/// If user doesn't provide a dark variation, fall back to the named color variation if it exists
return darkVariation ?? self
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private struct MessageCenterListContentView: View {

@ViewBuilder
func makeUnreadIndicator() -> some View {
let foregroundColor = theme.unreadIndicatorColor?.adaptiveColor(for: colorScheme, darkVariation: theme.unreadIndicatorColorDark) ?? theme.cellTintColor?.adaptiveColor(for: colorScheme, darkVariation: theme.cellTintColorDark)
let foregroundColor = colorScheme.resolveColor(light: theme.unreadIndicatorColor, dark: theme.unreadIndicatorColorDark) ?? colorScheme.resolveColor(light: theme.cellTintColor, dark: theme.cellTintColorDark)

if self.message.unread {
Image(systemName: MessageCenterListContentView.unreadIndicatorImageName)
Expand All @@ -158,7 +158,7 @@ private struct MessageCenterListContentView: View {
VStack(alignment: .leading, spacing: 5) {
Text(self.message.title)
.font(theme.cellTitleFont)
.foregroundColor(theme.cellTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.cellTitleColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.cellTitleColor, dark: theme.cellTitleColorDark))
.accessibilityHidden(true)

if let subtitle = self.message.subtitle {
Expand All @@ -170,7 +170,7 @@ private struct MessageCenterListContentView: View {

Text(self.message.sentDate, style: .date)
.font(theme.cellDateFont)
.foregroundColor(theme.cellDateColor?.adaptiveColor(for: colorScheme, darkVariation: theme.cellDateColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.cellDateColor, dark: theme.cellDateColorDark))
.accessibilityHidden(true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ public struct MessageCenterListView: View {
)

if #available(iOS 15.0, *) {
cell.listRowBackground(theme.cellColor?.adaptiveColor(for: colorScheme, darkVariation: theme.cellColorDark))
cell.listRowBackground(colorScheme.resolveColor(light: theme.cellColor, dark: theme.cellColorDark))
.listRowSeparator(
(theme.cellSeparatorStyle == SeparatorStyle.none)
? .hidden : .automatic
)
.listRowSeparatorTint(theme.cellSeparatorColor?.adaptiveColor(for: colorScheme, darkVariation: theme.cellSeparatorColorDark))
.listRowSeparatorTint(colorScheme.resolveColor(light: theme.cellSeparatorColor, dark: theme.cellSeparatorColorDark))
} else {
cell.listRowBackground(theme.cellColor?.adaptiveColor(for: colorScheme, darkVariation: theme.cellColorDark))
cell.listRowBackground(colorScheme.resolveColor(light: theme.cellColor, dark: theme.cellColorDark))
}
}

Expand Down Expand Up @@ -149,12 +149,7 @@ public struct MessageCenterListView: View {

@ViewBuilder
private func makeContent() -> some View {
let listBackgroundColor = theme
.messageListBackgroundColor?
.adaptiveColor(
for: colorScheme,
darkVariation: theme.messageListBackgroundColorDark
)
let listBackgroundColor = colorScheme.resolveColor(light: theme.messageListBackgroundColor, dark: theme.messageListBackgroundColorDark)

let content = ZStack {
makeList()
Expand Down Expand Up @@ -214,10 +209,10 @@ public struct MessageCenterListView: View {
Text(
"\("ua_delete_messages".messageCenterLocalizedString) (\(self.selection.count))"
)
.foregroundColor(theme.deleteButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.deleteButtonTitleColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.deleteButtonTitleColor, dark: theme.deleteButtonTitleColorDark))
} else {
Text("ua_delete_messages".messageCenterLocalizedString)
.foregroundColor(theme.deleteButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.deleteButtonTitleColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.deleteButtonTitleColor, dark: theme.deleteButtonTitleColorDark))
}
}
)
Expand All @@ -236,10 +231,10 @@ public struct MessageCenterListView: View {
Text(
"\("ua_mark_messages_read".messageCenterLocalizedString) (\(self.selection.count))"
)
.foregroundColor(theme.markAsReadButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.markAsReadButtonTitleColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.markAsReadButtonTitleColor, dark: theme.markAsReadButtonTitleColorDark))
} else {
Text("ua_mark_messages_read".messageCenterLocalizedString)
.foregroundColor(theme.markAsReadButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.markAsReadButtonTitleColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.markAsReadButtonTitleColor, dark: theme.markAsReadButtonTitleColorDark))
}
}
)
Expand All @@ -261,7 +256,7 @@ public struct MessageCenterListView: View {
self.selection = Set(self.messageIDs)
} label: {
Text("ua_select_all_messages".messageCenterLocalizedString)
.foregroundColor(theme.selectAllButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.selectAllButtonTitleColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.selectAllButtonTitleColor, dark: theme.selectAllButtonTitleColorDark))
}
.accessibilityHint("ua_select_all_messages".messageCenterLocalizedString)
}
Expand All @@ -271,7 +266,7 @@ public struct MessageCenterListView: View {
self.selection = Set()
} label: {
Text("ua_select_none_messages".messageCenterLocalizedString)
.foregroundColor(theme.selectAllButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.selectAllButtonTitleColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.selectAllButtonTitleColor, dark: theme.selectAllButtonTitleColorDark))
}
.accessibilityHint("ua_select_none_messages".messageCenterLocalizedString)
}
Expand All @@ -294,8 +289,8 @@ public struct MessageCenterListView: View {
let isEditMode = self.editMode?.wrappedValue.isEditing ?? false
let color =
isEditMode
? theme.cancelButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.cancelButtonTitleColorDark) :
theme.editButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.editButtonTitleColorDark)
? colorScheme.resolveColor(light: theme.cancelButtonTitleColor, dark: theme.cancelButtonTitleColorDark) :
colorScheme.resolveColor(light: theme.editButtonTitleColor, dark: theme.editButtonTitleColorDark)

return EditButton()
.foregroundColor(color)
Expand All @@ -304,7 +299,7 @@ public struct MessageCenterListView: View {

@ViewBuilder
private func refreshButton() -> some View {
let refreshColor = theme.refreshTintColor?.adaptiveColor(for: colorScheme, darkVariation: theme.refreshTintColorDark)
let refreshColor = colorScheme.resolveColor(light: theme.refreshTintColor, dark: theme.refreshTintColorDark)

if isRefreshing {
ProgressView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ private struct MessageCenterMessageContentView: View {
)
}
dismiss()
}.foregroundColor(theme.deleteButtonTitleColor?.adaptiveColor(for: colorScheme, darkVariation: theme.deleteButtonTitleColorDark))
}.foregroundColor(colorScheme.resolveColor(light: theme.deleteButtonTitleColor, dark: theme.deleteButtonTitleColorDark))
}
}

Expand All @@ -473,7 +473,7 @@ private struct MessageCenterMessageContentView: View {
Image(systemName: "chevron.backward")
.scaleEffect(0.68)
.font(Font.title.weight(.medium))
.foregroundColor(theme.backButtonColor?.adaptiveColor(for: colorScheme, darkVariation: theme.backButtonColorDark))
.foregroundColor(colorScheme.resolveColor(light: theme.backButtonColor, dark: theme.backButtonColorDark))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal struct DefaultMessageCenterViewStyle: MessageCenterViewStyle {

@ViewBuilder
internal func makeBody(configuration: Configuration) -> some View {
let containerBackgroundColor: Color? = configuration.theme.messageListContainerBackgroundColor?.adaptiveColor(for: configuration.colorScheme, darkVariation: configuration.theme.messageListContainerBackgroundColorDark)
let containerBackgroundColor: Color? = configuration.colorScheme.resolveColor(light: configuration.theme.messageListContainerBackgroundColor, dark: configuration.theme.messageListContainerBackgroundColorDark)

let content = configuration.content
.applyIf(containerBackgroundColor != nil) { view in
Expand Down

0 comments on commit c893f64

Please sign in to comment.