Skip to content

Commit

Permalink
Fix not being able to select custom date format (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakerwreah authored May 27, 2023
1 parent bfc361a commit 7407312
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Calendr.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.9.5;
MARKETING_VERSION = 1.9.6;
PRODUCT_BUNDLE_IDENTIFIER = br.paker.Calendr;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Calendr/Config/Calendr-Bridging-Header.h";
Expand All @@ -1268,7 +1268,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.9.5;
MARKETING_VERSION = 1.9.6;
PRODUCT_BUNDLE_IDENTIFIER = br.paker.Calendr;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Calendr/Config/Calendr-Bridging-Header.h";
Expand Down
28 changes: 13 additions & 15 deletions Calendr/Settings/GeneralSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,27 +278,25 @@ class GeneralSettingsViewController: NSViewController {

private func setUpDateFormat() {

let dateFormatStyle = dateFormatDropdown.rx.controlProperty(
getter: { (dropdown: NSPopUpButton) -> DateStyle in
DateStyle(rawValue: UInt(dropdown.indexOfSelectedItem + 1)) ?? .none
},
setter: { (dropdown: NSPopUpButton, style: DateStyle) in
dropdown.selectItem(at: (style.isCustom ? dropdown.numberOfItems : Int(style.rawValue)) - 1)
}
let dateFormatControl = dateFormatDropdown.rx.controlProperty(
getter: \.indexOfSelectedItem,
setter: { $0.selectItem(at: $1) }
)

dateFormatStyle
.skip(1)
.bind(to: viewModel.statusItemDateStyleObserver)
.disposed(by: disposeBag)
Observable.combineLatest(
viewModel.dateStyleOptions, dateFormatControl.skip(1)
)
.map { $0[$1].style }
.bind(to: viewModel.statusItemDateStyleObserver)
.disposed(by: disposeBag)

Observable.combineLatest(
viewModel.dateStyleOptions, viewModel.statusItemDateStyle
)
.bind { [dateFormatDropdown] options, dateStyle in
dateFormatDropdown.removeAllItems()
dateFormatDropdown.addItems(withTitles: options)
dateFormatStyle.onNext(dateStyle)
.bind { [dropdown = dateFormatDropdown] options, dateStyle in
dropdown.removeAllItems()
dropdown.addItems(withTitles: options.map(\.title))
dropdown.selectItem(at: dateStyle.isCustom ? dropdown.numberOfItems - 1 : options.firstIndex(where: { $0.style == dateStyle }) ?? 0)
}
.disposed(by: disposeBag)

Expand Down
19 changes: 13 additions & 6 deletions Calendr/Settings/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ extension DateStyle {
var isCustom: Bool { !Self.options.contains(self) }
}

struct DateStyleOption: Equatable {
let style: DateStyle
let title: String
}

typealias PopoverMaterial = NSVisualEffectView.Material

extension PopoverMaterial {
Expand Down Expand Up @@ -90,7 +95,7 @@ class SettingsViewModel: StatusItemSettings, NextEventSettings, CalendarSettings
let showStatusItemDate: Observable<Bool>
let showStatusItemBackground: Observable<Bool>
let statusItemDateStyle: Observable<DateStyle>
let dateStyleOptions: Observable<[String]>
let dateStyleOptions: Observable<[DateStyleOption]>
let statusItemDateFormat: Observable<String>
let dateFormatPlaceholder = "E d MMM YYYY"
let isDateFormatInputVisible: Observable<Bool>
Expand Down Expand Up @@ -194,14 +199,16 @@ class SettingsViewModel: StatusItemSettings, NextEventSettings, CalendarSettings
dateStyleOptions = calendarObservable
.map { calendar in
let dateFormatter = DateFormatter(calendar: calendar)
var options: [String] = []
var options: [DateStyleOption] = []

for i: UInt in DateStyle.options.map(\.rawValue) {
dateFormatter.dateStyle = .init(rawValue: i) ?? .none
options.append(dateFormatter.string(from: dateProvider.now))
for option in DateStyle.options {
dateFormatter.dateStyle = option
let title = dateFormatter.string(from: dateProvider.now)
guard !options.contains(where: { $0.title == title }) else { continue }
options.append(.init(style: option, title: title))
}

options.append("\(Strings.Settings.MenuBar.dateFormatCustom)...")
options.append(.init(style: .none, title: "\(Strings.Settings.MenuBar.dateFormatCustom)..."))

return options
}
Expand Down
18 changes: 9 additions & 9 deletions CalendrTests/SettingsViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,38 +183,38 @@ class SettingsViewModelTests: XCTestCase {

dateProvider.m_calendar.locale = Locale(identifier: "en_US")

var options: [String]?
var options: [DateStyleOption]?

viewModel.dateStyleOptions
.bind { options = $0 }
.disposed(by: disposeBag)

XCTAssertEqual(options, [
"1/1/21",
"Jan 1, 2021",
"January 1, 2021",
"Friday, January 1, 2021",
"Custom..."
.init(style: .short, title: "1/1/21"),
.init(style: .medium, title: "Jan 1, 2021"),
.init(style: .long, title: "January 1, 2021"),
.init(style: .full, title: "Friday, January 1, 2021"),
.init(style: .none, title: "Custom...")
])
}

func testDateStyleOptions_withLocaleChange() {

dateProvider.m_calendar.locale = Locale(identifier: "en_US")

var options: [String]?
var options: [DateStyleOption]?

viewModel.dateStyleOptions
.bind { options = $0 }
.disposed(by: disposeBag)

XCTAssertEqual(options?.first, "1/1/21")
XCTAssertEqual(options?.first, .init(style: .short, title: "1/1/21"))

dateProvider.m_calendar.locale = Locale(identifier: "en_GB")

notificationCenter.post(name: NSLocale.currentLocaleDidChangeNotification, object: nil)

XCTAssertEqual(options?.first, "01/01/2021")
XCTAssertEqual(options?.first, .init(style: .short, title: "01/01/2021"))
}

func testDateStyleSelected() {
Expand Down

0 comments on commit 7407312

Please sign in to comment.