enhance(mobile): journals calendar for ios

This commit is contained in:
charlie
2025-06-04 18:21:37 +08:00
parent 9365c110e7
commit 988584b2d5

View File

@@ -9,21 +9,19 @@ import Capacitor
import Foundation
func isDarkMode() -> Bool {
if #available(iOS 12.0, *) {
return UITraitCollection.current.userInterfaceStyle == .dark
} else {
return false
}
if #available(iOS 12.0, *) {
return UITraitCollection.current.userInterfaceStyle == .dark
} else {
return false
}
}
func isOnlyDayDifferentOrSame(date1: Date, date2: Date) -> Bool {
let calendar = Calendar.current
let components1 = calendar.dateComponents([.year, .month, .day], from: date1)
let components2 = calendar.dateComponents([.year, .month, .day], from: date2)
return components1.year == components2.year &&
components1.month == components2.month &&
(components1.day != components2.day || components1.day == components2.day)
func isOnlyDayDifferentOrSame(date1: Foundation.Date, date2: Date) -> Bool {
let calendar = Calendar.current
let components1 = calendar.dateComponents([.year, .month, .day], from: date1)
let components2 = calendar.dateComponents([.year, .month, .day], from: date2)
return components1.year == components2.year && components1.month == components2.month && (components1.day != components2.day || components1.day == components2.day)
}
class DatePickerView: UIView {
@@ -31,12 +29,12 @@ class DatePickerView: UIView {
super.init(frame: frame)
isUserInteractionEnabled = true
}
required init?(coder: NSCoder) {
super.init(coder: coder)
isUserInteractionEnabled = true
}
super.init(coder: coder)
isUserInteractionEnabled = true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
}
@@ -47,44 +45,91 @@ class DatePickerDialogViewController: UIViewController {
private let dialogView = DatePickerView()
private var lastDate: Date?
private var initialMonthLabel: UILabel?
private var currentMonthText: String?
var onDateSelected: ((Date?) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
lastDate = datePicker.date
setupImplView()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.settleMonthLabel()
}
}
private func settleMonthLabel() {
initialMonthLabel = findMonthLabel(in: datePicker)
if let label = initialMonthLabel {
currentMonthText = label.text
print("Initial month label: \(currentMonthText ?? "Unknown")")
} else {
print("Month label not found")
}
}
private func findMonthLabel(in view: UIView) -> UILabel? {
for subview in view.subviews {
if let label = subview as? UILabel, (label.text?.contains(" ")) == true {
print(label.text as Any)
return label
}
if let foundLabel = findMonthLabel(in: subview) {
return foundLabel
}
}
return nil
}
private func inCalendarWheelPickerMode(in view: UIView) -> Bool? {
for subview in view.subviews {
if let label = subview as? UILabel, label.text?.contains("July") == true {
print(label.text as Any)
return true
}
let found: Bool? = inCalendarWheelPickerMode(in: subview)
if found == true {
return true
}
}
return false
}
@objc private func confirmDate() {
if (isOnlyDayDifferentOrSame(date1: lastDate!, date2: datePicker.date)) {
let label = findMonthLabel(in: datePicker)
if isOnlyDayDifferentOrSame(date1: lastDate!, date2: datePicker.date) || (label != nil && label?.text != currentMonthText && (inCalendarWheelPickerMode(in: datePicker) != true)) {
onDateSelected?(datePicker.date)
dismiss(animated: false, completion: nil)
}
lastDate = datePicker.date
}
@objc private func dismissDialog() {
onDateSelected?(nil)
dismiss(animated: false, completion: nil)
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 12.0, *) {
if traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle {
if traitCollection.userInterfaceStyle == .dark {
print("switch to dark mode")
dialogView.backgroundColor = .black
} else {
print("switch to light mode")
dialogView.backgroundColor = .white
}
}
}
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 12.0, *) {
if traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle {
if traitCollection.userInterfaceStyle == .dark {
print("switch to dark mode")
dialogView.backgroundColor = .black
} else {
print("switch to light mode")
dialogView.backgroundColor = .white
}
}
}
}
func setupImplView() {
datePicker.datePickerMode = .date
datePicker.preferredDatePickerStyle = .inline
@@ -96,13 +141,13 @@ class DatePickerDialogViewController: UIViewController {
view.backgroundColor = .black.withAlphaComponent(0.4)
view.isUserInteractionEnabled = true
if isDarkMode() {
dialogView.backgroundColor = .black
} else {
dialogView.backgroundColor = .white
}
dialogView.layer.cornerRadius = 10
dialogView.clipsToBounds = true
view.addSubview(dialogView)
@@ -137,7 +182,7 @@ class DatePickerDialogViewController: UIViewController {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let touch = touches.first {
let location = touch.location(in: view)
if !dialogView.frame.contains(location) {