Skip to content

Commit

Permalink
Fix setting wrong bookmark color when picking teal, green, or purple (#…
Browse files Browse the repository at this point in the history
…1183)

* Use secure value transformer to pacify Core Data model compiler

* Fix wrong bookmark color getting applied for cyan, green, and purple

Also change bookmark color picker to a sheet presentation and fix its background drawing.
  • Loading branch information
nolanw committed Mar 3, 2024
1 parent 7b680a7 commit f947e22
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 56 deletions.
2 changes: 1 addition & 1 deletion App/Data Sources/ThreadListDataSource.swift
Expand Up @@ -218,7 +218,7 @@ extension ThreadListDataSource: UITableViewDataSource {
case .orange: color = theme["unreadBadgeOrangeColor"]!
case .red: color = theme["unreadBadgeRedColor"]!
case .yellow: color = theme["unreadBadgeYellowColor"]!
case .teal: color = theme["unreadBadgeTealColor"]!
case .cyan: color = theme["unreadBadgeCyanColor"]!
case .green: color = theme["unreadBadgeGreenColor"]!
case .purple: color = theme["unreadBadgePurpleColor"]!
case .none: color = theme["unreadBadgeBlueColor"]!
Expand Down
Expand Up @@ -61,6 +61,10 @@ extension UIContextMenuConfiguration {
setBookmarkColor: ForumsClient.shared.setBookmarkColor(_:as:),
thread: thread
))
profile.modalPresentationStyle = .pageSheet
if let sheet = profile.sheetPresentationController {
sheet.detents = [.medium()]
}
presenter.present(profile, animated: true)
}
)
Expand Down
61 changes: 24 additions & 37 deletions App/Views/BookmarkColorPicker.swift
Expand Up @@ -10,23 +10,13 @@ import SwiftUI

private let Log = Logger.get()

private struct ThemeKey: EnvironmentKey {
static var defaultValue: Theme { Theme.defaultTheme() }
}
extension EnvironmentValues {
var theme: Theme {
get { self[ThemeKey.self] }
set { self[ThemeKey.self] = newValue }
}
}

private extension StarCategory {
var themeKey: String {
switch self {
case .orange: return "unreadBadgeOrangeColor"
case .red: return "unreadBadgeRedColor"
case .yellow: return "unreadBadgeYellowColor"
case .teal: return "unreadBadgeTealColor"
case .cyan: return "unreadBadgeCyanColor"
case .green: return "unreadBadgeGreenColor"
case .purple: return "unreadBadgePurpleColor"
case .none: return "unreadBadgeBlueColor"
Expand Down Expand Up @@ -57,52 +47,49 @@ private struct BookmarkColor: View {
}

struct BookmarkColorPicker: View {
@SwiftUI.Environment(\.presentationMode) var presentationMode
@SwiftUI.Environment(\.dismiss) var dismiss
let setBookmarkColor: (AwfulThread, StarCategory) async throws -> Void
let starCategories = Array(StarCategory.allCases.filter { $0 != .none })
@SwiftUI.Environment(\.theme) var theme
@ObservedObject var thread: AwfulThread

private func didTap(_ starCategory: StarCategory) {
let oldSelection = thread.starCategory
thread.starCategory = starCategory
try! thread.managedObjectContext?.save()

Task {
do {
try await setBookmarkColor(thread, starCategory)
presentationMode.wrappedValue.dismiss()
dismiss()
} catch {
Log.e("Could not set thread \(thread.threadID) category to \(starCategory.rawValue)")
thread.starCategory = oldSelection
try! thread.managedObjectContext?.save()
}
}
}

var body: some View {
return VStack {
Text(thread.title ?? "")
.foregroundColor(theme[color: "sheetTitleColor"]!)
.font(.system(size: 16.0, weight: .regular, design: .rounded))
.padding()

HStack {
ForEach(StarCategory.allCases, id: \.rawValue) { starCategory in
Button(action: { didTap(starCategory) }) {
BookmarkColor(
selection: $thread.starCategory,
starCategory: starCategory
)
ZStack {
theme[color: "sheetBackgroundColor"]!
.edgesIgnoringSafeArea(.all)

VStack {
Text(thread.title ?? "")
.foregroundColor(theme[color: "sheetTitleColor"]!)
.font(.system(size: 16.0, weight: .regular, design: .rounded))
.padding()

HStack {
ForEach(starCategories, id: \.rawValue) { starCategory in
Button(action: { didTap(starCategory) }) {
BookmarkColor(
selection: $thread.starCategory,
starCategory: starCategory
)
}
}
}
}

Spacer()
Spacer()
}
}
.background(theme[color: "sheetBackgroundColor"]!)
.edgesIgnoringSafeArea(.all)
}

}

struct BookmarkColorPicker_Previews: PreviewProvider {
Expand Down
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21279" systemVersion="22A380" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22522" systemVersion="23D60" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Announcement" representedClassName="Announcement" syncable="YES">
<attribute name="authorCustomTitleHTML" attributeType="String"/>
<attribute name="authorRegdate" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
Expand Down Expand Up @@ -91,7 +91,7 @@
<attribute name="aboutMe" optional="YES" attributeType="String"/>
<attribute name="aimName" optional="YES" attributeType="String"/>
<attribute name="gender" optional="YES" attributeType="String"/>
<attribute name="homepageURL" optional="YES" attributeType="Transformable"/>
<attribute name="homepageURL" optional="YES" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromDataTransformer"/>
<attribute name="icqName" optional="YES" attributeType="String"/>
<attribute name="interests" optional="YES" attributeType="String"/>
<attribute name="lastModifiedDate" attributeType="Date" usesScalarValueType="NO"/>
Expand All @@ -101,7 +101,7 @@
<attribute name="occupation" optional="YES" attributeType="String"/>
<attribute name="postCount" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="NO"/>
<attribute name="postRate" optional="YES" attributeType="String"/>
<attribute name="profilePictureURL" optional="YES" attributeType="Transformable"/>
<attribute name="profilePictureURL" optional="YES" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromDataTransformer"/>
<attribute name="yahooName" optional="YES" attributeType="String"/>
<relationship name="user" maxCount="1" deletionRule="Nullify" destinationEntity="User" inverseName="profile" inverseEntity="User"/>
</entity>
Expand Down
11 changes: 5 additions & 6 deletions AwfulCore/Sources/AwfulCore/Model/Thread.swift
Expand Up @@ -40,15 +40,14 @@ public class AwfulThread: AwfulManagedObject, Managed {
}

@objc public enum StarCategory: Int16, CaseIterable {
case none = -1 // probably should've been 0, oh well

case orange = 0
case red = 1
case yellow = 2

case none = 3 // probably should've been 0, oh well

case teal = 4
case green = 5
case purple = 6
case cyan = 3
case green = 4
case purple = 5
}

extension AwfulThread {
Expand Down
3 changes: 3 additions & 0 deletions AwfulCore/Sources/AwfulCore/Networking/ForumsClient.swift
Expand Up @@ -354,6 +354,9 @@ public final class ForumsClient {
if thread.bookmarkListPage <= 0 {
thread.bookmarkListPage = 1
}
if thread.starCategory != category {
thread.starCategory = category
}
try mainContext.save()
}
}
Expand Down
Expand Up @@ -97,7 +97,7 @@ internal extension ThreadListScrapeResult.Thread {
func update(_ thread: AwfulThread) {
let isBookmarked: Bool = {
switch bookmark {
case .orange, .red, .yellow, .teal, .green, .purple:
case .orange, .red, .yellow, .cyan, .green, .purple:
return true
case .none:
return false
Expand All @@ -123,7 +123,7 @@ internal extension ThreadListScrapeResult.Thread {
case .orange: return .orange
case .red: return .red
case .yellow: return .yellow
case .teal: return .teal
case .cyan: return .cyan
case .green: return .green
case .purple: return .purple
case .none: return .none
Expand Down
Expand Up @@ -53,7 +53,7 @@ public struct ThreadListScrapeResult: ScrapeResult {

public enum Bookmark: Equatable {
case none
case orange, red, yellow, teal, green, purple
case orange, red, yellow, cyan, green, purple
}
}

Expand Down Expand Up @@ -263,7 +263,7 @@ private extension ThreadListScrapeResult.Thread.Bookmark {
case 0: self = .orange
case 1: self = .red
case 2: self = .yellow
case 3: self = .teal
case 3: self = .cyan
case 4: self = .green
case 5: self = .purple
case .some: self = .orange
Expand Down
10 changes: 5 additions & 5 deletions AwfulTheming/Sources/AwfulTheming/Themes.plist
Expand Up @@ -201,7 +201,7 @@
<string>#f3f135</string>
<key>unreadBadgeGreenColor</key>
<string>#7ec071</string>
<key>unreadBadgeTealColor</key>
<key>unreadBadgeCyanColor</key>
<string>#6cecf0</string>
<key>unreadBadgePurpleColor</key>
<string>#9437ff</string>
Expand Down Expand Up @@ -309,7 +309,7 @@
<string>#83dafd</string>
<key>unreadBadgeGrayColor</key>
<string>#bebebe</string>
<key>unreadBadgeTealColor</key>
<key>unreadBadgeCyanColor</key>
<string>#75f8fd</string>
<key>unreadBadgeGreenColor</key>
<string>#73fa79</string>
Expand Down Expand Up @@ -1054,7 +1054,7 @@
<string>#5AA000</string>
<key>unreadBadgePurpleColor</key>
<string>#9337FD</string>
<key>unreadBadgeTealColor</key>
<key>unreadBadgeCyanColor</key>
<string>#0066FF</string>
</dict>
</dict>
Expand Down Expand Up @@ -1249,7 +1249,7 @@
<string>#5AA000</string>
<key>unreadBadgePurpleColor</key>
<string>#9337FD</string>
<key>unreadBadgeTealColor</key>
<key>unreadBadgeCyanColor</key>
<string>#0066FF</string>
</dict>
</dict>
Expand Down Expand Up @@ -1287,7 +1287,7 @@
<string>#ff0000</string>
<key>unreadBadgeYellowColor</key>
<string>#ffff00</string>
<key>unreadBadgeTealColor</key>
<key>unreadBadgeCyanColor</key>
<string>#008080</string>
<key>unreadBadgeGreenColor</key>
<string>#00ff00</string>
Expand Down

0 comments on commit f947e22

Please sign in to comment.