view LazyBear/UI/IconPicker.swift @ 275:62f2c675b666

Interactive LineChart implemented
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Thu, 18 Mar 2021 17:23:21 +0100
parents 2f9845d59a59
children 22eba376075e
line wrap: on
line source

//
//  IconPicker.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 26/2/21.
//

import SwiftUI

struct IconPicker: View {
    var body: some View {
        List {
            Section(header: Text("White background")) {
                ForEach(icons, id: \.name) { icon in
                    if !icon.background {
                        IconRow(icon: icon)
                    }
                }
            }
            
            Section(header: Text("Black background")) {
                ForEach(icons, id: \.name) { icon in
                    if icon.background {
                        IconRow(icon: icon)
                    }
                }
            }
        }
        .listStyle(GroupedListStyle())
    }
}

struct IconRow: View {
    @Environment(\.colorScheme) var colorScheme  // Detect dark mode
    var icon: IconModel
    let haptics = Haptics()
    
    var body: some View {
        Button(action: { haptics.simpleSuccess(); changeIcon(key: icon.file) }) {
            HStack {
                Image(icon.file)
                    .resizable()
                    .cornerRadius(20)
                    .frame(width: 70, height: 70)
                    
                Text(icon.name)
                    .foregroundColor(colorScheme == .dark ? .white: .black)
            }
        }
    }
    
    private func changeIcon(key: String) {
        if key != "defaultIcon" {
            UIApplication.shared.setAlternateIconName(key) { error in
                if let error = error {
                    print(error.localizedDescription)
                } else {
                    print("Success!")
                }
            }
        } else {
            UIApplication.shared.setAlternateIconName(nil)
        }
    }
}

struct IconPicker_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            IconPicker()
            
        }
    }
}