CouponsView.swift 6.6 KB
//
//  ProfileView.swift
//  warplyFramework
//
//  Created by Βασιλης Σκουρας on 13/1/22.
//

#if canImport(SwiftUI)
import SwiftUI

class DataModel {
    var data: Array<NSDictionary> = []

    init() { //initializer method
        let instanceOfMyApi = MyApi()
        let couponSets = instanceOfMyApi.getCouponSets(withActive: true, andVisible: true, andUuids: nil)
        let coupons = instanceOfMyApi.getCoupons() as AnyObject?
        
        if let myCouponsSetsDictionary = couponSets as? [String : AnyObject] {
            let couponSetsData = (myCouponsSetsDictionary["MAPP_COUPON"] as! Array<NSMutableDictionary>)
            if let myCouponsDictionary = coupons as? [String : AnyObject] {
                let couponsData = (myCouponsDictionary["result"] as! Array<NSMutableDictionary>)
                if let sets = couponSetsData as? NSArray {
                    for set in sets {
                        let s = set as! NSDictionary
                        if let cpns = couponsData as? NSArray {
                            for coupon in cpns {
                                var c = coupon as! NSDictionary
//                                var temp = NSMutableDictionary(dictionary: s);
                                if c["couponset_uuid"] as! String == s["uuid"] as! String {
                                    var temp = NSMutableDictionary(dictionary: s);
                                    temp.addEntries(from: c as! [AnyHashable : Any])
                                    
                                    self.data.append(temp as NSDictionary)
                                }
                                    
                            }
                        }
                                
                    }
                }
            }
        }
    }

    var getData: Array<NSDictionary> {
        get { // getter
            return data
        }
    }
}

struct UrlImageView: View {
    @ObservedObject var urlImageModel: UrlImageModel

    init(urlString: String?) {
        urlImageModel = UrlImageModel(urlString: urlString)
    }

    var body: some View {
        Image(uiImage: urlImageModel.image ?? UrlImageView.defaultImage!)
            .resizable()
            .scaledToFill()
    }

    static var defaultImage = UIImage(systemName: "photo")
}

class UrlImageModel: ObservableObject {
    @Published var image: UIImage?
    var urlString: String?

    init(urlString: String?) {
        self.urlString = urlString
        loadImage()
    }

    func loadImage() {
        loadImageFromUrl()
    }

    func loadImageFromUrl() {
        guard let urlString = urlString else {
            return
        }
        
        let url = URL(string: urlString)!
        let task = URLSession.shared.dataTask(with: url, completionHandler:
        getImageFromResponse(data:response:error:))
        task.resume()
    }


    func getImageFromResponse(data: Data?, response: URLResponse?, error: Error?)
    {
        guard error == nil else {
            print("Error: \(error!)")
            return
        }
        guard let data = data else {
            print("No data found")
            return
        }
        
        DispatchQueue.main.async {
            guard let loadedImage = UIImage(data: data) else {
                return
            }
            self.image = loadedImage
        }
    }
}

extension CouponsView {
    struct headerView: View {
        
        var uiscreen = UIScreen.main.bounds
        
        var body: some View {
            ZStack {
               Image("ic_back", bundle: Bundle(for: MyEmptyClass.self))
                   .resizable()
                   .frame(width: self.uiscreen.height * 0.025, height: self.uiscreen.height * 0.02)
                   .offset(x: -self.uiscreen.width / 2 + self.uiscreen.width * 0.05, y: self.uiscreen.height * 0.07)
                Text("Όλα τα κουπόνια μου")
                    .frame(width: self.uiscreen.width * 0.8, height: self.uiscreen.height * 0.025, alignment: .center)
                    .offset( y:  self.uiscreen.height * 0.07)
                    .frame(width: self.uiscreen.width)
            }
        }
    }
    
    
    
    struct couponView: View {
        var result: NSDictionary
        var index: Int
        
        var uiscreen = UIScreen.main.bounds
        
        var body: some View {
            ZStack {
                UrlImageView(urlString: result["img_preview"] as? String)
                    .frame(width: self.uiscreen.height * 0.25, height: self.uiscreen.height * 0.05, alignment: .leading)
                    .offset(x: self.uiscreen.width * 0.05, y: self.uiscreen.height * 0.06)

            }
            .background(
                Image("coupons_container", bundle: Bundle(for: MyEmptyClass.self))
                    .resizable()
                    .offset(x: self.uiscreen.width * 0.05, y: (CGFloat(index + 1) * self.uiscreen.height * 0.06) + CGFloat(index) * self.uiscreen.height * 0.17)
                    .padding()
                    .frame(width: UIScreen.main.bounds.width * 0.9, height: UIScreen.main.bounds.height * 0.17, alignment: .topLeading)
            )
        }
    }
    
}

@available(iOS 13.0.0, *)
struct CouponsView: View {
    
    var data:Array<NSDictionary> = DataModel().getData
    
    var uiscreen = UIScreen.main.bounds
    
    var body: some View {
        VStack {
            headerView()
            ScrollView {
                VStack {
                    if (data.count) > 0 {
                        ForEach(Array(zip(data.indices, data)), id: \.0) { index, result  in
                            couponView(result: result, index: index)
                        }
                    }
            }
            .frame(width:self.uiscreen.width, height:self.uiscreen.height )
            }
        }
    }
}
#endif
//struct ProfileView_Previews: PreviewProvider {
//    static var previews: some View {
//        ProfileView()
//    }
//}

@available(iOS 13.0.0, *)
struct Previews_CouponsView_Previews: PreviewProvider {
    static var uiscreen = UIScreen.main.bounds
    static var previews: some View {
        ZStack {
            Image("ic_back", bundle: Bundle(for: MyEmptyClass.self))
                .resizable()
                .frame(width: self.uiscreen.height * 0.025, height: self.uiscreen.height * 0.02)
                .offset(x: -self.uiscreen.width / 2 + self.uiscreen.width * 0.05, y: self.uiscreen.height * 0.07)
            Text("Όλα τα κουπόνια μου")
                .frame(width: self.uiscreen.width * 0.8, height: self.uiscreen.height * 0.025, alignment: .center)
                .offset( y:  self.uiscreen.height * 0.07)
        }

    }
}