Skip to content

Instantly share code, notes, and snippets.

View dimebt's full-sized avatar
🎯
Focusing

Dimitar Stefanovski dimebt

🎯
Focusing
View GitHub Profile
private func fetchJSON() -> Promise<[Photo]> {
return Promise { seal in
Alamofire.request(photosURL, method: .get).validate().responseData { (data) in
guard let data = data.result.value else {
seal.reject(PhotoError.ConvertToData)
return
}
guard let photos = try? JSONDecoder().decode([Photo].self, from: data) else {
seal.reject(PhotoError.PhotoDecoding)
return
private func downloadPhotos(photos: [Photo]) -> Promise<[UIImage]> {
return Promise { seal in
var count = 0
for photo in photos {
guard let photoUrl = URL(string: photo.url) else {
seal.reject(PhotoError.downloadPhotoUrl)
return
}
guard let photoData = try? Data(contentsOf: photoUrl) else {
seal.reject(PhotoError.downloadPhotoConvertToData)
let backgroundQueue = DispatchQueue.global(qos: .background)
firstly {
showLoader()
}.then(on: backgroundQueue) {
self.fetchJSON()
}.then(on: backgroundQueue) { (photos) in
self.downloadPhotos(photos: Array(photos.prefix(40)))
}.done(on: DispatchQueue.main, flags: nil) { _ in
self.hideLoader()
@main
struct WalletWidget: Widget {
private let kind: String = "WalletWidget"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
WalletWidgetEntryView(entry: entry)
}
.configurationDisplayName("Wallet Widget")
.description("Description for the Wallet app widget.")
struct WalletWidgetEntryView : View {
var entry: Provider.Entry
@Environment(\.widgetFamily) var family
@ViewBuilder
var body: some View {
//Text(entry.date, style: .time)
switch family {
case .systemSmall:
public func snapshot(with context: Context, completion: @escaping (WalletWidgetEntry) -> ()) {
let entry = WalletWidgetEntry(date: Date())
completion(entry)
}
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
// Generate a timeline consisting of one entry with reload policy after 1 min.
let currentDate = Date()
let nextUpdateDate = Calendar.current.date(byAdding: .minute,
value: 1,
to: currentDate)!
let entry = WalletWidgetEntry(date: currentDate)
let timeline = Timeline(entries: [entry], policy: .after(nextUpdateDate))
completion(timeline)
struct Provider: TimelineProvider {
public typealias Entry = WalletWidgetEntry
public func placeholder(in context: Context) -> Entry {
let entry = WalletWidgetEntry(date: Date())
return entry
}
}
ForEach(self.transactions, id: \.self) { transaction in
Link(destination: URL(string: transaction.stringId)!) {
VStack {
HStack {
struct ContentView: View {
@State var transactionId: String = "Not set yet!"
var body: some View {
NavigationView {
WWidgetPreview(transaction: Helper.getTransaction(from: transactionId))
.onOpenURL(perform: { (transactionId) in
self.transactionId = transactionId.absoluteString
})