import Foundation
import WidgetKit
import SwiftUI
struct Provider: TimelineProvider {
public typealias Entry = SimpleEntry
public func snapshot(with context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date())
completion(entry)
}
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 1 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate)
entries.append(entry)
let currentDate = Date()
let formatter = DateFormatter()
formatter.dateStyle = .short
let dateTimeString = formatter.string(from: currentDate)
print("\(dateTimeString)")
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
public let date: Date
// future code.
}
struct PlaceholderView : View {
var body: some View {
ZStack {
Color.black
.edgesIgnoringSafeArea(.all)
Text("----- --:")
.background(Color.black)
.foregroundColor(Color.white)
.font(.custom("DIN Alternate-Bold", size: 25))
.multilineTextAlignment(.center)
Text("--------- -- ----")
.background(Color.black)
.foregroundColor(Color.white)
.font(.custom("DIN Alternate-Bold", size: 25))
.multilineTextAlignment(.center)
}
}
struct CPClockWidgetEntryView : View {
static var dateformatter: DateFormatter =
{
let dateformatter = DateFormatter()
dateformatter.dateStyle = .medium
return dateformatter
}()
var currentDate = Date()
var entry: Provider.Entry
var body: some View {
ZStack {
Color.black
.edgesIgnoringSafeArea(.all)
VStack {
Text("Today Is:")
.background(Color.black)
.foregroundColor(Color.white)
.font(.custom("DIN Alternate-Bold", size: 25))
.multilineTextAlignment(.center)
let dateTimeString = PlaceholderView.CPClockWidgetEntryView.dateformatter.string(from: currentDate)
Text("\(dateTimeString)")
.background(Color.black)
.foregroundColor(Color.white)
.font(.custom("DIN Alternate-Bold", size: 30))
.multilineTextAlignment(.center)
}
}
}
// <--- Multiple Widgets Code --->
// struct CPClockWidget: View {
// @Environment(\.widgetFamily) var family: WidgetFamily
//
// @ViewBuilder
//
// var body: some View {
//
// switch family {
// case: .systemSmall(CPClockWidget)
//
// }
// }
// }
//
//}
}
@main
struct CPClockWidget: Widget {
private let kind: String = "CPClockWidget"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider(), placeholder: PlaceholderView()) { entry in
CPClockWidgetEntryView(entry: entry)
}
.configurationDisplayName("CP Clock")
.description("Widget for CPClock.")
.supportedFamilies([.systemSmall, .systemMedium])
}
}
}
struct CPClockWidget_Previews: PreviewProvider {
static var previews: some View {
ZStack {
Color.black
.edgesIgnoringSafeArea(.all)
VStack {
Text("Today Is:")
.background(Color.black)
.foregroundColor(Color.orange)
.font(.custom("Arial-Bold", size: 25))
Text("\(Date(), style: .date)")
.background(Color.black)
.foregroundColor(Color.orange)
.font(.custom("Arial-Bold", size: 30))
.multilineTextAlignment(.center)
}
}
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
func getinfo()
{
}