🐛 Bug fixes on iOS widget

This commit is contained in:
LittleSheep 2024-12-21 22:38:22 +08:00
parent 7182336a0d
commit e9e3a4c474
2 changed files with 14 additions and 18 deletions

View File

@ -10,7 +10,7 @@ import SwiftUI
struct CheckInProvider: TimelineProvider { struct CheckInProvider: TimelineProvider {
func placeholder(in context: Context) -> CheckInEntry { func placeholder(in context: Context) -> CheckInEntry {
CheckInEntry(date: Date(), user: nil, checkIn: nil) CheckInEntry(date: Date(), checkIn: nil)
} }
func getSnapshot(in context: Context, completion: @escaping (CheckInEntry) -> ()) { func getSnapshot(in context: Context, completion: @escaping (CheckInEntry) -> ()) {
@ -23,24 +23,17 @@ struct CheckInProvider: TimelineProvider {
jsonDecoder.dateDecodingStrategy = .formatted(dateFormatter) jsonDecoder.dateDecodingStrategy = .formatted(dateFormatter)
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
let userRaw = prefs?.string(forKey: "user")
var user: SolarUser?
if let userRaw = userRaw {
user = try! jsonDecoder.decode(SolarUser.self, from: userRaw.data(using: .utf8)!)
}
let checkInRaw = prefs?.string(forKey: "pas_check_in_record") let checkInRaw = prefs?.string(forKey: "pas_check_in_record")
var checkIn: SolarCheckInRecord? var checkIn: SolarCheckInRecord?
if let checkInRaw = checkInRaw { if let checkInRaw = checkInRaw {
checkIn = try! jsonDecoder.decode(SolarCheckInRecord.self, from: checkInRaw.data(using: .utf8)!) checkIn = try! jsonDecoder.decode(SolarCheckInRecord.self, from: checkInRaw.data(using: .utf8)!)
if checkIn != nil && Calendar.current.isDate(checkIn!.createdAt, inSameDayAs: Date()) { if checkIn != nil && !Calendar.current.isDate(checkIn!.createdAt, inSameDayAs: Date()) {
checkIn = nil checkIn = nil
} }
} }
let entry = CheckInEntry( let entry = CheckInEntry(
date: Date(), date: Date(),
user: user,
checkIn: checkIn checkIn: checkIn
) )
completion(entry) completion(entry)
@ -56,7 +49,6 @@ struct CheckInProvider: TimelineProvider {
struct CheckInEntry: TimelineEntry { struct CheckInEntry: TimelineEntry {
let date: Date let date: Date
let user: SolarUser?
let checkIn: SolarCheckInRecord? let checkIn: SolarCheckInRecord?
} }
@ -135,10 +127,9 @@ struct CheckInWidget: Widget {
#Preview(as: .systemSmall) { #Preview(as: .systemSmall) {
CheckInWidget() CheckInWidget()
} timeline: { } timeline: {
CheckInEntry(date: .now, user: nil, checkIn: nil) CheckInEntry(date: .now, checkIn: nil)
CheckInEntry( CheckInEntry(
date: .now, date: .now,
user: SolarUser(id: 1, name: "demo", nick: "Deemo"),
checkIn: SolarCheckInRecord(id: 1, resultTier: 1, resultExperience: 100, createdAt: Date.now) checkIn: SolarCheckInRecord(id: 1, resultTier: 1, resultExperience: 100, createdAt: Date.now)
) )
} }

View File

@ -13,7 +13,7 @@ struct RandomPostProvider: TimelineProvider {
func placeholder(in context: Context) -> RandomPostEntry { func placeholder(in context: Context) -> RandomPostEntry {
RandomPostEntry(date: Date(), user: nil, randomPost: nil, family: .systemMedium) RandomPostEntry(date: Date(), user: nil, randomPost: nil, family: .systemMedium)
} }
func getSnapshot(in context: Context, completion: @escaping (RandomPostEntry) -> ()) { func getSnapshot(in context: Context, completion: @escaping (RandomPostEntry) -> ()) {
let prefs = UserDefaults(suiteName: "group.solsynth.solian") let prefs = UserDefaults(suiteName: "group.solsynth.solian")
@ -45,7 +45,7 @@ struct RandomPostProvider: TimelineProvider {
) )
completion(entry) completion(entry)
} }
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
getSnapshot(in: context) { (entry) in getSnapshot(in: context) { (entry) in
let timeline = Timeline(entries: [entry], policy: .atEnd) let timeline = Timeline(entries: [entry], policy: .atEnd)
@ -64,7 +64,7 @@ struct RandomPostEntry: TimelineEntry {
struct RandomPostWidgetEntryView : View { struct RandomPostWidgetEntryView : View {
var entry: RandomPostProvider.Entry var entry: RandomPostProvider.Entry
var body: some View { var body: some View {
VStack(alignment: .leading, spacing: 0) { VStack(alignment: .leading, spacing: 0) {
if let randomPost = entry.randomPost { if let randomPost = entry.randomPost {
@ -73,15 +73,20 @@ struct RandomPostWidgetEntryView : View {
if let avatar = randomPost.publisher.avatar { if let avatar = randomPost.publisher.avatar {
let avatarUrl = getAttachmentUrl(for: avatar) let avatarUrl = getAttachmentUrl(for: avatar)
let size: CGFloat = 28 let size: CGFloat = 28
let scaleProcessor = ResizingImageProcessor(referenceSize: CGSize(width: size, height: size), mode: .aspectFit) let scaleProcessor = ResizingImageProcessor(referenceSize: CGSize(width: size, height: size), mode: .aspectFill)
KFImage.url(URL(string: avatarUrl)) KFImage.url(URL(string: avatarUrl))
.resizable() .resizable()
.setProcessor(scaleProcessor) .setProcessor(scaleProcessor)
.fade(duration: 0.25) .fade(duration: 0.25)
.aspectRatio(contentMode: .fit) .placeholder{
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
}
.aspectRatio(contentMode: .fill)
.frame(width: size, height: size) .frame(width: size, height: size)
.cornerRadius(size / 2) .cornerRadius(size / 2)
.frame(width: size, height: size, alignment: .center) .frame(width: size, height: size, alignment: .center)
} }
@ -158,7 +163,7 @@ struct RandomPostWidgetEntryView : View {
struct RandomPostWidget: Widget { struct RandomPostWidget: Widget {
let kind: String = "SolarRandomPostWidget" let kind: String = "SolarRandomPostWidget"
var body: some WidgetConfiguration { var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: RandomPostProvider()) { entry in StaticConfiguration(kind: kind, provider: RandomPostProvider()) { entry in
if #available(iOS 17.0, *) { if #available(iOS 17.0, *) {