Untitled

mail@pastecode.io avatar
unknown
swift
3 years ago
4.5 kB
1
Indexable
Never
//
//  LoginView.swift
//  SenseCap
//
//  Created by Hasan Ali Şişeci on 19.01.2022.
//

import SwiftUI

struct AddHotspotView: View {
    @State private var api: String = ""
    @State private var serialNumber: String = ""
    let sharedInstance = UserDefaultsManager.shared
    @StateObject var viewModel: AddHotspotViewModel = AddHotspotViewModel()

    @State private var apiErrorMessage: String = ""
    @State private var serialNoErrorMessage: String = ""
    @State private var decisionApiColor = true
    @State private var decisionSerialNoColor = true
    @State private var linkIsDisabled = true
    @State private var msg: Int? = 1
    @State private var selectedItem: Int? = nil
    @State private var showingAlert = false
    @State private var msgValue = 0



    var body: some View {
        NavigationView {
            VStack(spacing: 30) {
                HStack {
                    Text(TextConstants.addHotspotViewTitle)
                        .modifier(TextModifier(font: .boldTitleText, foregroundColor: .titleDarkgray))
                    Spacer()
                }
                VStack(spacing: 42) {
                    VStack(alignment: .leading, spacing: 15) {
                        AddHotspotTextFieldView(text: $api, placeholder: TextConstants.apiTextFieldPlaceholder, topPlaceholder: TextConstants.apiTextFieldTopPlaceholder, strokeColor: decisionApiColor ? .gray : .offlineRed, keyboardType: .default)
                        Text(apiErrorMessage)
                            .modifier(AddHotspotErrorTextModifier())
                            .padding(.top, -20)
                        AddHotspotTextFieldView(text: $serialNumber, placeholder: TextConstants.serialNoTextFieldPlaceholder, topPlaceholder: TextConstants.serialNoTextFieldTopPlaceholder, strokeColor: decisionSerialNoColor ? .gray : .offlineRed, keyboardType: .numberPad)
                        Text(serialNoErrorMessage)
                            .modifier(AddHotspotErrorTextModifier())
                            .padding(.top, -20)
                    }
                    Button {
                        DispatchQueue.main.async {
                            msg = viewModel.controlHotspot(apiKey: api, serialNumber: serialNumber)
                            showError(msg: ErrorMsgValues(rawValue: msg ?? 11101) ?? .apiError)
                        }
                    } label: {
                        buttonContentWithoutIcon(buttonTitle: TextConstants.addHotspotButtonTitle, color: Color.sensecapBlue, height: 52, width: 270, cornerRadius: 12, font: .boldButtonText)
                    }.background(
                        NavigationLink(destination: MyHotspotsView(), tag: msgValue,
                                       selection: $msg) { EmptyView() }
                            .buttonStyle(PlainButtonStyle())
                    )
                }
            }
                .padding(.horizontal, 20)
        }
            .navigationBarHidden(true)
            .onTapGesture {
            self.endEditing()
        }
    }
    
    private func endEditing() {
        UIApplication.shared.endEditing()
    }

    private func showError(msg: ErrorMsgValues) {

        print("show error is work")
        switch msg{
        case .success:
            sharedInstance.set(newUserModel: UserModel(apiKey: api, serialNumber: serialNumber))
            serialNoErrorMessage = ""
            apiErrorMessage = ""
            decisionApiColor = true
            decisionSerialNoColor = true
        case .apiError:
            sharedInstance.set(newUserModel: UserModel(apiKey: api, serialNumber: serialNumber))
            serialNoErrorMessage = ""
            apiErrorMessage = ""
            decisionApiColor = true
            decisionSerialNoColor = true
        case .snError:
            apiErrorMessage = "**Invalid Api Key"
            serialNoErrorMessage = ""
            decisionSerialNoColor = true
            decisionApiColor = false
        case .noSnError:
            serialNoErrorMessage = "**Invalid Serial Number"
            apiErrorMessage = ""
            decisionApiColor = true
            decisionSerialNoColor = false
        }
    }
    
    enum ErrorMsgValues: Int {
        case success = 0
        case apiError = 11101
        case snError = 11200
        case noSnError = 11203
    }
}

struct LoginView_Previews: PreviewProvider {
    static var previews: some View {
        AddHotspotView()
    }
}