I have created a ios app using flutter which needs a homescreen widget for showing live rate of gold and silver which is Intgerated from socket.So I created a Homescreen widget for my app using swift,but the problem is thaht the widget is showing the Expected Result.Please Help me to resolve this.Below is my Code
//
// liveratewidget.swift
// liveratewidget
//
// Created by Artifitia on 11/07/23.
//
import WidgetKit
import SwiftUI
import Intents
import SocketIO
var bidPrice: String = "Loading..."
struct Provider: IntentTimelineProvider {
let socketManager = SocketManager(socketURL: URL(string: "https://testsocket.bullionview.com/")!, config: [.log(false),.forceWebsockets(true), .compress])
// var bidPrice: String = "Loading..."
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent(), bidPrice: bidPrice)
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration, bidPrice: bidPrice)
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var _: [SimpleEntry] = []
let currentDate = Date()
// Connect to the socket and listen for "gold-rate-change" event
let socket = socketManager.defaultSocket
socket.on("gold-rate-change") {data, ack in
// guard let self = self else { return }
if let dataArray = data as? [[String: Any]],
let bidPriceData = dataArray[0]["data"] as? [String: Any],
let bid = bidPriceData["bid"] as? Double {
bidPrice = String(bid)
}
print(bidPrice)
guard let cur = data[0] as? Double else { return }
}
// socket.on("gold-rate-change") { [weak self] (data, _) in
// guard let self = self else { return }
// if let dataArray = data as? [[String: Any]],
// let bidPriceData = dataArray[0]["data"] as? [String: Any],
// let bid = bidPriceData["bid"] as? Double {
// self.bidPrice = String(bid)
// }
// let entryDate = Calendar.current.date(byAdding: .minute, value: entries.count, to: currentDate)!
// let entry = SimpleEntry(date: entryDate, configuration: configuration, bidPrice: self.bidPrice)
// entries.append(entry)
// if entries.count >= 5 {
// socket.disconnect()
// let timeline = Timeline(entries: entries, policy: .atEnd)
// completion(timeline)
// }
// }
socket.connect()
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
let bidPrice: String
}
struct liveratewidgetEntryView: View {
var entry: Provider.Entry
var body: some View {
VStack {
Text("Gold Price" )
.font(.title)
.foregroundColor(.blue)
Text(bidPrice)
.font(.largeTitle)
.fontWeight(.bold)
}
.padding()
}
}
struct liveratewidget: Widget {
let kind: String = "liveratewidget"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
liveratewidgetEntryView(entry: entry)
}
.configurationDisplayName("Gold Bid Rate")
.description("Displays the current bid price for Gold.")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
}
}
struct liveratewidget_Previews: PreviewProvider {
static var previews: some View {
liveratewidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent(), bidPrice: bidPrice))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
enter image description here this is result