WalletView.swift 14.5 KB
//
//  WalletView.swift
//  WarplySDKFrameworkIOS
//
//  Created by Βασιλης Σκουρας on 20/4/22.
//

import SwiftUI

class ProfileDataModel {
    var data: NSDictionary

    init() { //initializer method
        let instanceOfMyApi = MyApi()
        let profile = instanceOfMyApi.getProfile()
        var profileData = NSDictionary()
        if let profileDictionary = profile as? [String : AnyObject] {
            profileData = (profileDictionary["result"] as! NSDictionary)
        }
        self.data = profileData
    }

    var getData: NSDictionary {
        get { // getter
            return data
        }
    }
}

extension WalletView {
    
    static func localizedWithParameter(parameter: String) -> LocalizedStringKey {
        return "\(parameter)"
    }
    
    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("My loyalty wallet")
                    .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 ImageView: View {
        @ObservedObject var imageLoader:UrlImageModel
        @State var width:CGFloat
        @State var height:CGFloat
        @State var isFill:Bool
        
        var uiscreen = UIScreen.main.bounds

        init(withURL url:String , width:CGFloat, height: CGFloat, isFill:Bool) {
            imageLoader = UrlImageModel(urlString:url)
            self.width = width
            self.height = height
            self.isFill = isFill
        }

        var body: some View {
            
            Image(uiImage: imageLoader.image ?? UIImage())
                .resizable()
                .frame(minWidth: self.uiscreen.width * 0.95, idealWidth: self.uiscreen.width * 0.95, maxWidth: self.uiscreen.width * 0.95, minHeight: self.uiscreen.height * 0.2, idealHeight: self.uiscreen.height * 0.2, maxHeight: self.uiscreen.height * 0.2, alignment: .leading)
                .scaledToFill()
                .aspectRatio(contentMode: isFill ? .fill : .fit)
                .cornerRadius(4)
        }
    }
    
    struct myCoupons: View {
        var uiscreen = UIScreen.main.bounds
        var instanceOfMySwiftApi = swiftApi()
//        var couponsLength = instanceOfMySwiftApi.getCoupons().filter({ $0.status! == 1 }).count
        var body: some View {
            VStack {
                HStack {
                    Text("My coupons")
                        .font(.system(size: 18))
                        .foregroundColor(Color.white)
                        .fontWeight(.medium)
                    Spacer()
                    Text("Παλαιότερα κουπόνια ->")
                        .font(.system(size: 14))
                        .foregroundColor(Color.white)
                        .fontWeight(.medium)
                }
                .padding(.leading, self.uiscreen.width * 0.05)
                .padding(.trailing, self.uiscreen.width * 0.05)
                HStack {
                    VStack {
                        Spacer()
                        Text(.init("Έχεις **" + String(instanceOfMySwiftApi.getCoupons().filter({ $0.status! == 1 }).count) + "** ενεργά κουπόνια"))
                            .frame(width: self.uiscreen.width * 0.35, alignment: .leading)
                            .font(.system(size: 17))
                        Spacer()
                        Text("Δες τα όλα ->")
                            .font(.system(size: 12))
                            .fontWeight(.medium)
                            .frame(width: self.uiscreen.width * 0.35, alignment: .leading)
                        Spacer()
                    }
                    .padding(.leading, self.uiscreen.width * 0.1)
                    Spacer()
                    Image("ic_gifts_for_you", bundle: Bundle(for: MyEmptyClass.self))
                        .frame(width: self.uiscreen.height * 0.08, height: self.uiscreen.height * 0.1)
                        .padding(.trailing, self.uiscreen.width * 0.08)
                }
                .padding(.top, self.uiscreen.height * 0.01)
                .padding(.bottom, self.uiscreen.height * 0.01)
                .background(
                    Image("coupons_container", bundle: Bundle(for: MyEmptyClass.self))
                        .resizable()
                        .frame(minWidth: self.uiscreen.width * 0.975, idealWidth: self.uiscreen.width * 0.975, maxWidth: self.uiscreen.width * 0.975)
                        .scaledToFill()
                        .aspectRatio(contentMode: .fill)
                )
                .frame(width: self.uiscreen.width * 0.975)
            }
            .padding(.top, self.uiscreen.height * 0.025)
            .padding(.bottom, self.uiscreen.height * 0.025)
            .frame(width: self.uiscreen.width)
            .background(Color(hex: 0xFFFFFF).opacity(0.13))
        }
    }
    
    struct myRewards: View {
        var uiscreen = UIScreen.main.bounds
        
        var instanceOfMySwiftApi = swiftApi()
        
        var body: some View {
            VStack {
                HStack {
                    Text("My rewards")
                        .font(.system(size: 18))
                        .foregroundColor(Color.white)
                        .fontWeight(.medium)
                    Spacer()
                    Text("Παλαιότερα δώρα ->")
                        .font(.system(size: 14))
                        .foregroundColor(Color.white)
                        .fontWeight(.medium)
                }
                .padding(.leading, self.uiscreen.width * 0.05)
                .padding(.trailing, self.uiscreen.width * 0.05)
                HStack {
                    VStack {
                        Spacer()
                        Text(.init("Έχεις **" + String(instanceOfMySwiftApi.getCoupons().filter({ $0.status! == 1 }).count) + "** ενεργά δώρα"))
                            .frame(width: self.uiscreen.width * 0.35, alignment: .leading)
                            .font(.system(size: 17))
                        Spacer()
                        Text("Δες τα όλα ->")
                            .font(.system(size: 12))
                            .fontWeight(.medium)
                            .frame(width: self.uiscreen.width * 0.35, alignment: .leading)
                        Spacer()
                    }
                    .padding(.leading, self.uiscreen.width * 0.1)
                    Spacer()
                    Image("ic_loyalty_rewards", bundle: Bundle(for: MyEmptyClass.self))
                        .resizable()
                        .frame(width: self.uiscreen.height * 0.08, height: self.uiscreen.height * 0.08)
                        .padding(.trailing, self.uiscreen.width * 0.08)
                }
                .padding(.top, self.uiscreen.height * 0.02)
                .padding(.bottom, self.uiscreen.height * 0.02)
                .frame(width: self.uiscreen.width * 0.975)
                .background(Color.white)
            }
            .padding(.top, self.uiscreen.height * 0.025)
            .padding(.bottom, self.uiscreen.height * 0.025)
            .frame(width: self.uiscreen.width)
            .background(Color(hex: 0xFFFFFF).opacity(0.13))
            .padding(.top, self.uiscreen.height * 0.02)
        }
    }
    
    struct linearView: View {
        var data: NSDictionary
        
        var uiscreen = UIScreen.main.bounds
        
        var body: some View {
            VStack{
                HStack {
                    VStack(alignment: .leading) {
                        Text("Ενεργός κωδικός:")
                            .font(.system(size: 17))
                            .fontWeight(.medium)
                            .foregroundColor(Color(hex: 0x3C5365))
                        Text("961544809")
                            .font(.system(size: 17))
                            .fontWeight(.bold)
                            .foregroundColor(Color(hex: 0x3C5365))
                        Text("Λήγει σε 4 ημέρες")
                            .font(.system(size: 11))
                            .fontWeight(.medium)
                            .foregroundColor(Color(hex: 0x84929E))
                    }
                    Spacer()
                    Image("deals_for_you", bundle: Bundle(for: MyEmptyClass.self))
                        .resizable()
                        .frame(width: self.uiscreen.height * 0.08, height: self.uiscreen.height * 0.08)
                        .padding(.top, self.uiscreen.height * 0.01)
                        .padding(.bottom, self.uiscreen.height * 0.01)
                }
                .padding(.leading, self.uiscreen.width * 0.04)
                .padding(.trailing, self.uiscreen.width * 0.04)
                .frame(width: self.uiscreen.width * 0.975, alignment: .leading)
                .background(Color.white)
                .cornerRadius(3)
                .padding(.top, self.uiscreen.height * 0.04)
                .padding(.bottom, self.uiscreen.height * 0.04)
                myCoupons()
                myRewards()
                HStack {
                    Text("Ανάλυση")
                        .font(.system(size: 14))
                        .fontWeight(.medium)
                        .foregroundColor(Color(hex: 0x509E2F))
                        .padding(.top, self.uiscreen.height * 0.015)
                        .padding(.bottom, self.uiscreen.height * 0.015)
                }
                .frame(width: self.uiscreen.width * 0.925)
                .background(Color(hex: 0xE6E6E6))
                .padding(.top, self.uiscreen.height * 0.02)
                .padding(.bottom, self.uiscreen.height * 0.03)
                
            }
            .frame(width: self.uiscreen.width, alignment: .center)
            .background(
                LinearGradient(gradient: Gradient(colors: [Color(hex: 0x1AADCC), Color(hex: 0x83C062)]), startPoint: .top, endPoint: .bottom)
            )
            .cornerRadius(15, corners: [.topLeft])
        }
    }
}

struct WalletView: View {
    var parentView: UIView
    
    var uiscreen = UIScreen.main.bounds
    
    var data: NSDictionary = ProfileDataModel().getData
    
    func goBack(){
        for subview in parentView.subviews {
            if(subview.tag == 3) {
                subview.removeFromSuperview()
            }
        }
    }
    
    var body: some View {
        VStack {
            headerView(goBack: goBack)
            HStack {
                ZStack {
                    if let value = (data["result"] as? String) {
                        ImageView(withURL: value, width: self.uiscreen.height * 0.05, height: self.uiscreen.height * 0.05, isFill: true)
                            .cornerRadius(self.uiscreen.height * 0.05)
                            .padding(.leading, self.uiscreen.width * 0.05)
                            
                    } else {
                        Image("generic_profile")
                            .frame(width: self.uiscreen.height * 0.05, height: self.uiscreen.height * 0.05)
                            .background(Color.black)
                            .cornerRadius(self.uiscreen.height * 0.05)
                            .padding(.leading, self.uiscreen.width * 0.05)
                    }
                    Circle()
                        .strokeBorder(Color(hex: 0x22A9B5), lineWidth: 1)
                        .frame(width: self.uiscreen.height * 0.05, height: self.uiscreen.height * 0.05)
                        .padding(.leading, self.uiscreen.width * 0.05)
                }
                VStack(alignment: .leading) {
                    Text(WalletView.localizedWithParameter(parameter: data["firstname"] as! String + " " + (data["lastname"] as! String)))
                        .font(.system(size: 13))
                        .padding(.top, self.uiscreen.height * 0.005)
                        .frame(width: self.uiscreen.width * 0.7, alignment: .leading)
                    Spacer()
                    if let value = (data["profile_metadata"] as? NSDictionary) {
                        if ((value["answered"] != nil) == true) {
                            VStack {
                            }
                            .background(
                                LinearGradient(gradient: Gradient(colors: [Color(hex: 0x1AADCC), Color(hex: 0x83C062)]), startPoint: .trailing, endPoint: .leading)
                            )
                        }
                    } else {
                        HStack {
                            Text("+")
                                .font(.system(size: 17))
                                .padding(.bottom, 2)
                            Text("Ερωτηματολόγιο")
                                .font(.system(size: 13))
                                .fontWeight(.medium)
                        }
                        .padding(.leading, 5)
                        .padding(.trailing, 5)
                        .overlay(
                            RoundedRectangle(cornerRadius: 16).stroke(Color(hex: 0x556778), lineWidth: 1)
                        )
                        .padding(.bottom, self.uiscreen.height * 0.005)
                    }
                }
                .padding(.leading , self.uiscreen.width * 0.025)
            }
            .frame(width: self.uiscreen.width, height: self.uiscreen.height * 0.06, alignment: .leading)
            .padding(.top, self.uiscreen.height * 0.04)
            .padding(.bottom, self.uiscreen.height * 0.02)
            ScrollView(showsIndicators: false) {
                linearView(data: data)
            }
            .frame(width:self.uiscreen.width, height:self.uiscreen.height * 0.78 )
            
        }
        .frame(width:self.uiscreen.width, height:self.uiscreen.height )
    }
}

//struct WalletView_Previews: PreviewProvider {
//    static var previews: some View {
//        WalletView()
//    }
//}