mirror of
https://github.com/neon443/NearFuture.git
synced 2026-03-11 14:56:15 +00:00
custom symbols picker
This commit is contained in:
36
Shared/Model/SymbolsPicker/SymbolsLoader.swift
Normal file
36
Shared/Model/SymbolsPicker/SymbolsLoader.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// SymbolsLoader.swift
|
||||
// NearFuture
|
||||
//
|
||||
// Created by neon443 on 14/06/2025.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class SymbolsLoader: ObservableObject {
|
||||
@Published var allSymbols: [String] = []
|
||||
|
||||
init() {
|
||||
self.allSymbols = getAllSymbols()
|
||||
}
|
||||
|
||||
func getSymbols(_ searched: String) -> [String] {
|
||||
if searched.isEmpty {
|
||||
return allSymbols
|
||||
} else {
|
||||
return allSymbols.filter() { $0.localizedCaseInsensitiveContains(searched) }
|
||||
}
|
||||
}
|
||||
|
||||
func getAllSymbols() -> [String] {
|
||||
var allSymbols = [String]()
|
||||
if let bundle = Bundle(identifier: "com.apple.CoreGlyphs"),
|
||||
let resPath = bundle.path(forResource: "name_availability", ofType: "plist"),
|
||||
let plist = try? NSDictionary(contentsOf: URL(fileURLWithPath: resPath), error: ()),
|
||||
let plistSymbols = plist["symbols"] as? [String: String]
|
||||
{
|
||||
allSymbols = Array(plistSymbols.keys)
|
||||
}
|
||||
return allSymbols
|
||||
}
|
||||
}
|
||||
48
Shared/Model/SymbolsPicker/SymbolsPicker.swift
Normal file
48
Shared/Model/SymbolsPicker/SymbolsPicker.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// SymbolsPicker.swift
|
||||
// NearFuture
|
||||
//
|
||||
// Created by neon443 on 14/06/2025.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct SymbolsPicker: View {
|
||||
@StateObject private var symbolsLoader = SymbolsLoader()
|
||||
@Binding var selection: String
|
||||
|
||||
@State var searchInput: String = ""
|
||||
|
||||
private func gridLayout(forWidth geoSizeWidth: CGFloat) -> [GridItem] {
|
||||
let gridItem = GridItem(.fixed(40), spacing: 20, alignment: .center)
|
||||
let columns = Int(geoSizeWidth/60.rounded(.up))
|
||||
return Array(repeating: gridItem, count: columns)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geo in
|
||||
ScrollView {
|
||||
LazyVGrid(columns: gridLayout(forWidth: geo.size.width)) {
|
||||
ForEach(symbolsLoader.getSymbols(searchInput), id: \.self) { symbol in
|
||||
Button() {
|
||||
selection = symbol
|
||||
} label: {
|
||||
Image(systemName: symbol)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(maxWidth: 40, maxHeight: 40)
|
||||
.symbolRenderingMode(.palette)
|
||||
.foregroundStyle(.blue, .gray, .black)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
.searchable(text: $searchInput)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
SymbolsPicker(selection: .constant(""))
|
||||
}
|
||||
Reference in New Issue
Block a user