MoreForYouView.swift 7.1 KB
//
//  MoreForYouView.swift
//  WarplySDKFrameworkIOS
//
//  Created by Βασιλης Σκουρας on 20/4/22.
//
import SwiftUI
import Combine
import Foundation
import UIKit

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

    init() { //initializer method
        let instanceOfMyApi = MyApi()
        let inbox = instanceOfMyApi.getInbox() as! Array<NSDictionary>
        data = inbox.filter({ $0["offer_category"] as! String == "more_for_you" })
    }

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

extension MoreForYouView {
    
    
    struct headerView: View {
        var goBack: () -> ()
        var uiscreen = UIScreen.main.bounds
        
        
        var body: some View {
            HStack(alignment: .center) {
                Button {
                    // Button Action
                    goBack()
                } label: {
                    Image("ic_back", bundle: Bundle(for: MyEmptyClass.self))
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: self.uiscreen.height * 0.025, height: self.uiscreen.height * 0.02)
                }
               
                Text("More for you")
                    .fontWeight(.medium)
                    .font(.system(size: 16))
                    .foregroundColor(Color(red: 0.20784313725490197, green: 0.3176470588235294, blue: 0.40784313725490196))
                    .multilineTextAlignment(.center)
                    .frame(maxWidth: .infinity)
                    .padding(.horizontal)
               
            }
            .frame(maxWidth: .infinity)
            .padding(.horizontal)
            .padding(.vertical, 10)
        }
    }
    
    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 inboxView: View {
        var result: NSDictionary
        var index: Int
        
        var uiscreen = UIScreen.main.bounds

        static func localizedWithParameter(parameter: String) -> LocalizedStringKey {
            return "\(parameter)"
        }
        
        var body: some View {
            HStack {
                VStack(alignment: .leading) {
                    Text(MoreForYouView.inboxView.localizedWithParameter(parameter: result["title"] as? String ?? ""))
                        .font(.system(size: 17).italic())
                        .fontWeight(.bold)
                        .foregroundColor(Color(hex: 0x3A5266))
                        .frame(maxWidth: self.uiscreen.width * 0.65, alignment: .topLeading)
                        .padding(.top, self.uiscreen.height * 0.01)
                        .padding(.leading, self.uiscreen.width * 0.03)
                    Spacer()
                    Text(MoreForYouView.inboxView.localizedWithParameter(parameter: result["subtitle"] as? String ?? ""))
                        .font(.system(size: 16))
                        .fontWeight(.medium)
                        .foregroundColor(Color(hex: 0x415564))
                        .frame(maxWidth: self.uiscreen.width * 0.72, alignment: .topLeading)
                        .padding(.leading, self.uiscreen.width * 0.03)
                        .padding(.bottom, self.uiscreen.height * 0.01)
                }
                .frame(width: self.uiscreen.width * 0.85, height: self.uiscreen.height * 0.2, alignment: .leading)
                .background(
                    Image("MFY_container", bundle: Bundle(for: MyEmptyClass.self))
                        .resizable()
                        .frame(minWidth: self.uiscreen.width * 0.85, idealWidth: self.uiscreen.width * 0.85, maxWidth: self.uiscreen.width * 0.85, minHeight: self.uiscreen.height * 0.2, idealHeight: self.uiscreen.height * 0.2, maxHeight: self.uiscreen.height * 0.2, alignment: .leading)
                        .scaledToFill()
                        .aspectRatio(contentMode: .fill)
                )
                .cornerRadius(4, corners: [.topLeft, .bottomLeft])
                Spacer()
            }
            .frame(width: self.uiscreen.width * 0.95, height: self.uiscreen.height * 0.2)
            .background(
                ImageView(withURL: result["logo_url"] as! String, width: self.uiscreen.width * 0.95, height: self.uiscreen.height * 0.2, isFill: true)
            )
            .padding(.bottom, self.uiscreen.height * 0.01)
            
        }
    }
}

extension Color {
    init(hex: Int, opacity: Double = 1.0) {
        let red = Double((hex & 0xff0000) >> 16) / 255.0
        let green = Double((hex & 0xff00) >> 8) / 255.0
        let blue = Double((hex & 0xff) >> 0) / 255.0
        self.init(.sRGB, red: red, green: green, blue: blue, opacity: opacity)
    }
}

struct MoreForYouView: View {
    
    var data:Array<NSDictionary> = DataMFYModel().getData
    
    var parentView: UIView
    
    var uiscreen = UIScreen.main.bounds
    
    func goBack(){
        for subview in parentView.subviews {
            if(subview.tag == 4) {
                subview.removeFromSuperview()
            }
        }
    }
    
    var body: some View {
        VStack {
            headerView(goBack: goBack)
            ScrollView(showsIndicators: false) {
                VStack {
                    if (data.count) > 0 {
                        ForEach(Array(zip(data.indices, data)), id: \.0) { index, result  in
                            inboxView(result: result, index: index)
                            
                        }
                    }
                }
                .padding(.top, self.uiscreen.height * 0.04)
                .padding(.bottom, self.uiscreen.height * 0.05)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity )
            .background(
                LinearGradient(gradient: Gradient(colors: [Color(hex: 0x1AADCC), Color(hex: 0x83C062)]), startPoint: .top, endPoint: .bottom)
            )
            .cornerRadius(20, corners: [.topLeft])
            
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity )
        // .frame(width:self.uiscreen.width, height:self.uiscreen.height )
    }
}

//struct MoreForYouView_Previews: PreviewProvider {
//    static var previews: some View {
//        MoreForYouView()
//    }
//}