2024-08-11 00:53:57 +02:00
|
|
|
// Made by Lumaa
|
2024-08-10 18:26:58 +02:00
|
|
|
|
|
|
|
import SwiftUI
|
2024-08-11 00:53:57 +02:00
|
|
|
import WidgetKit
|
2024-08-10 18:26:58 +02:00
|
|
|
|
2024-08-11 00:53:57 +02:00
|
|
|
struct CreatePostWidget: Widget {
|
|
|
|
let kind: String = "CreatePostWidget"
|
|
|
|
|
|
|
|
var body: some WidgetConfiguration {
|
|
|
|
StaticConfiguration(
|
|
|
|
kind: kind,
|
|
|
|
provider: Provider()
|
|
|
|
) { entry in
|
|
|
|
CreatePostWidget.WidgetView()
|
|
|
|
}
|
|
|
|
.configurationDisplayName("widget.open.composer")
|
|
|
|
.description("widget.open.composer")
|
|
|
|
.supportedFamilies([.systemSmall])
|
2024-08-15 04:30:05 +02:00
|
|
|
.disfavoredLocations(
|
|
|
|
[.standBy, .iPhoneWidgetsOnMac],
|
|
|
|
for: [.systemSmall]
|
|
|
|
)
|
2024-08-11 00:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct WidgetView: View {
|
|
|
|
@Environment(\.widgetFamily) private var family: WidgetFamily
|
|
|
|
@Environment(\.colorScheme) private var colorScheme: ColorScheme
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
|
|
|
if family == .systemSmall {
|
|
|
|
VStack {
|
|
|
|
Button(intent: OpenComposerIntent()) {
|
|
|
|
Image(systemName: "square.and.pencil")
|
|
|
|
.resizable()
|
|
|
|
.scaledToFit()
|
|
|
|
.frame(width: 80, height: 80)
|
|
|
|
}
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
|
|
|
|
Text("widget.composer")
|
|
|
|
.font(.caption)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.containerBackground(Color.appBackground, for: .widget)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Provider: TimelineProvider {
|
|
|
|
func getSnapshot(
|
|
|
|
in context: Context,
|
|
|
|
completion: @escaping @Sendable (CreatePostWidget.Entry) -> Void
|
|
|
|
) {
|
|
|
|
completion(.init(date: .now))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTimeline(
|
|
|
|
in context: Context,
|
|
|
|
completion: @escaping (Timeline<CreatePostWidget.Entry>) -> Void
|
|
|
|
) {
|
|
|
|
var entries: [CreatePostWidget.Entry] = []
|
|
|
|
|
|
|
|
// Generate a timeline consisting of two entries an hour apart, starting from the current date.
|
|
|
|
let currentDate = Date()
|
|
|
|
for hourOffset in 0 ..< 2 {
|
|
|
|
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
|
|
|
|
let entry = CreatePostWidget.Entry(date: entryDate)
|
|
|
|
entries.append(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
completion(Timeline(entries: entries, policy: .atEnd))
|
|
|
|
}
|
|
|
|
|
|
|
|
func placeholder(in context: Context) -> CreatePostWidget.Entry {
|
|
|
|
return .init(date: .now)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Entry: TimelineEntry {
|
|
|
|
let date: Date
|
2024-08-10 18:26:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-11 00:53:57 +02:00
|
|
|
#Preview(as: .systemSmall) {
|
2024-08-10 18:26:58 +02:00
|
|
|
CreatePostWidget()
|
2024-08-11 00:53:57 +02:00
|
|
|
} timeline: {
|
|
|
|
CreatePostWidget.Entry(date: .now)
|
2024-08-10 18:26:58 +02:00
|
|
|
}
|