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

#if canImport(SwiftUI)
import SwiftUI
import Combine
import Foundation
import UIKit

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
        }
    }
}

class ImageLoader: ObservableObject {
    var didChange = PassthroughSubject<Data, Never>()
    var data = Data() {
        didSet {
            didChange.send(data)
        }
    }

    init(urlString:String) {
        guard let url = URL(string: urlString) else { return }
        let task = URLSession.shared.dataTask(with: url) { data, response, error in
            guard let data = data else { return }
            DispatchQueue.main.async {
                self.data = data
            }
        }
        task.resume()
    }
}

struct ImageView: View {
    @ObservedObject var imageLoader:ImageLoader
    @State var image:UIImage = UIImage()

    init(withURL url:String) {
        imageLoader = ImageLoader(urlString:url)
    }

    var body: some View {
        
            Image(uiImage: image)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .onReceive(imageLoader.didChange) { data in
                self.image = UIImage(data: data) ?? UIImage()
        }
    }
}

struct VLine: Shape {
    func path(in rect: CGRect) -> Path {
        Path { path in
            path.move(to: CGPoint(x: rect.midX, y: rect.minY))
            path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))
        }
    }
}

extension CouponsView {
    struct headerView: View {
        var goBack: () -> ()
        var uiscreen = UIScreen.main.bounds
        
        
        var body: some View {
            HStack {
               Button(action: goBack) {
                   HStack {
                       Image("ic_back", bundle: Bundle(for: MyEmptyClass.self))
                           .resizable()
                           .frame(width: self.uiscreen.height * 0.022, height: self.uiscreen.height * 0.02)
                           .offset(y: self.uiscreen.height * 0.015)
                   }
               }
                Text("Όλα τα κουπόνια μου")
                    .frame(width: self.uiscreen.width * 0.8, height: self.uiscreen.height * 0.025, alignment: .center)
                    .offset( y:  self.uiscreen.height * 0.02)
                    .font(.system(size: 15))
            }
            .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.1)
        }
    }
    
    struct couponView: View {
        var result: NSDictionary
        var index: Int
        
        var uiscreen = UIScreen.main.bounds

        static func localizedWithParameter(parameter: String) -> LocalizedStringKey {
            return "\(parameter)"
        }
        
        static func convertDateFormat(inputDate: String) -> String {
             let olDateFormatter = DateFormatter()
             olDateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"

             let oldDate = olDateFormatter.date(from: inputDate)

             let convertDateFormatter = DateFormatter()
             convertDateFormatter.dateFormat = "dd/MM/yyyy"

             return convertDateFormatter.string(from: oldDate!)
        }
        
        var body: some View {
            
            
            VStack(alignment: .leading) {
                HStack(alignment: .center) {
                    ImageView(withURL: result["img_preview"] as! String)
                        .frame(maxWidth: self.uiscreen.width * 0.15)
                        .padding(.leading , self.uiscreen.width * 0.055)
                    VLine()
                        .stroke(style: StrokeStyle(lineWidth: 1, dash: [5]))
                        .foregroundColor(Color(red: 0.4392156862745098, green: 0.4392156862745098, blue: 0.4392156862745098))
                        .frame(width: 1)
                        .padding(.leading, self.uiscreen.width * 0.01)
                        .padding(.top, self.uiscreen.height * 0.01)
                        .padding(.bottom, self.uiscreen.height * 0.0075)
                    Spacer()
                    VStack {
                        Text(CouponsView.couponView.localizedWithParameter(parameter: result["admin_name"] as? String ?? ""))
                            .frame(maxWidth: self.uiscreen.width * 0.35, maxHeight: .infinity, alignment: .leading)
                            .font(.system(size: 15))
                        Text(CouponsView.couponView.localizedWithParameter(parameter: result["discount"] as? String ?? ""))
                            .frame(maxWidth: self.uiscreen.width * 0.35, maxHeight: self.uiscreen.height * 0.05, alignment: .leading)
                            .font(.system(size: 47))
                        Text(CouponsView.couponView.localizedWithParameter(parameter: "Ισχύει εώς " + CouponsView.couponView.convertDateFormat(inputDate: result["expiration"] as? String ?? "")))
                            .frame(maxWidth: self.uiscreen.width * 0.35, maxHeight: .infinity, alignment: .leading)
                            .font(.system(size: 10))
                    }
                    .frame(maxHeight: .infinity)
                    .padding(.top, self.uiscreen.height * 0.03)
                    .padding(.bottom, self.uiscreen.height * 0.03)
                    VStack {
                        Text(CouponsView.couponView.localizedWithParameter(parameter: result["short_description"] as! String))
                            .frame(maxWidth: self.uiscreen.width * 0.22, maxHeight: self.uiscreen.height * 0.10)
                            .font(.system(size: 10))
                    }
                    .frame(maxWidth: self.uiscreen.width * 0.25, maxHeight: .infinity)
                    Spacer()
                }
            }
            .frame(width: self.uiscreen.width * 0.95, height: self.uiscreen.height * 0.13)
            .background(
                Image("coupons_container", bundle: Bundle(for: MyEmptyClass.self))
                    .resizable()
            )
            .padding(.bottom, self.uiscreen.height * 0.03)
            
        }
    }
    
}

@available(iOS 13.0.0, *)
struct CouponsView: View {

    var data:Array<NSDictionary> = DataModel().getData
    
    var parentView: UIView
    
    var uiscreen = UIScreen.main.bounds
    
    func goBack(){
        for subview in parentView.subviews {
            if(subview.tag == 1) {
                subview.removeFromSuperview()
            }
        }
    }
    
    var body: some View {
        VStack {
            headerView(goBack: goBack)
            ScrollView {
                VStack {
                    if (data.count) > 0 {
                        ForEach(Array(zip(data.indices, data)), id: \.0) { index, result  in
                            couponView(result: result, index: index)                        }
                    }
                }
                .padding(.top, self.uiscreen.height * 0.05)
            }
            .frame(width:self.uiscreen.width, height:self.uiscreen.height * 0.9 )
            .background(
                Image("coupons_scrollview", bundle: Bundle(for: MyEmptyClass.self))
                    .resizable()
            )
            
        }
        .frame(width:self.uiscreen.width, height:self.uiscreen.height )
    }
}
#endif

@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)
        }

    }
}