[go: nahoru, domu]

Skip to content

Commit

Permalink
Separate Model & api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
gokul1630 committed Feb 9, 2024
1 parent 7cd7342 commit fd5a9a4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 41 deletions.
Binary file modified Sticker/Assets.xcassets/.DS_Store
Binary file not shown.
74 changes: 33 additions & 41 deletions Sticker/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
import Foundation

class DownloadManager{
class DownloadManager {

static func extractExtension(response: URLResponse?) -> String {
guard let response = response else {
Expand All @@ -24,44 +24,36 @@ class DownloadManager{
return "webp"
}

static func downloadFile(fileUrl url: String, completion: @escaping (URL?) -> Void) {
let fileUrl = URL(string: url)!
var destinationURL: URL? = nil

let task = URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
guard error == nil else {
debugPrint(error!)
completion(nil)
return
}

guard let location = location else {
debugPrint(error!)
completion(nil)
return
}

do {
let randomFileName = UUID().uuidString
let tempDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!

guard let response = response else {
debugPrint(error!)
completion(nil)
return
}

destinationURL = tempDirectory.appendingPathExtension("\(randomFileName).\(DownloadManager.extractExtension(response: response))")


try FileManager.default.moveItem(at: location, to: destinationURL!)
completion(destinationURL)

} catch {
debugPrint(error)
completion(nil)
}
}
task.resume()
}
static func download<T: Codable>(_ model: T.Type, url: String, completion: @escaping (T?, _ error: Error?) -> Void) -> Void {
let url = URL(string: url)
guard let url = url else {
debugPrint("Error while unwrapping url")
return
}

let urlSession = URLSession.shared
let task = urlSession.dataTask(with: url){ (data, response, error) -> Void in

if error != nil {
debugPrint("Error decoing data: ", error!)
completion(nil, error)
return
}

do{
if let data = data {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let data: T = try decoder.decode(T.self, from: data)
completion(data, nil)
return
}
completion(nil, nil)
} catch {
debugPrint("Error on api: ", error)
}
}

task.resume()
}
}
39 changes: 39 additions & 0 deletions Sticker/Model/Telegram.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Telegram.swift
// Sticker
//
// Created by Gokulprasanth on 28/01/24.
//

import Foundation

struct Thumb: Codable{
let fileId: String
}

struct Stickers: Codable {
let emoji:String
let fileId: String
let thumb: Thumb
}

struct MetaDataResult: Codable {
let isAnimated: Bool
let title: String
let name: String
let stickers: [Stickers]
}

struct StickerMetadata: Codable {
let ok: Bool
let result: MetaDataResult
}

struct StickerDataResult: Codable{
let filePath: String
}

struct StickerData: Codable {
let ok: Bool
let result: StickerDataResult
}

0 comments on commit fd5a9a4

Please sign in to comment.