Skip to content

Commit

Permalink
Adding more SwiftUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackjacx committed Aug 1, 2019
1 parent 2c0d769 commit 8818c26
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
6 changes: 3 additions & 3 deletions Example/Source/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
}

public struct CountryPickerConfig: Configuration {
public var shadowRadius: Length = 10.0
public var shadowRadius: CGFloat = 10.0
public var textAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 15)]
public var lineWidth: CGFloat = 1.0 / UIScreen.main.scale
public var rasterSize: CGFloat = 12.0
public var separatorInsets: UIEdgeInsets {
return UIEdgeInsets(top: 0, left: rasterSize * 3.7, bottom: 0, right: rasterSize)
}
public var searchBarPlaceholder: String? = "Search"
public var flagWidth: Length = 30
public var searchBarPlaceholder: String = "Search"
public var flagWidth: CGFloat = 30

public init() {}
}
12 changes: 6 additions & 6 deletions Source/Classes/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
import SwiftUI

public protocol Configuration {
var shadowRadius: Length { get }
var shadowRadius: CGFloat { get }
var textAttributes: [NSAttributedString.Key: Any] { get }
var lineWidth: CGFloat { get }
var rasterSize: CGFloat { get }
var separatorInsets: UIEdgeInsets { get }
var searchBarPlaceholder: String? { get }
var flagWidth: Length { get }
var searchBarPlaceholder: String { get }
var flagWidth: CGFloat { get }
}

public struct DefaultConfig: Configuration {
public var shadowRadius: Length = 10.0
public var shadowRadius: CGFloat = 10.0
public var textAttributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 20)]
public var lineWidth: CGFloat = 1.0 / UIScreen.main.scale
public var rasterSize: CGFloat = 12.0
public var separatorInsets: UIEdgeInsets {
return UIEdgeInsets(top: 0, left: rasterSize, bottom: 0, right: rasterSize)
}
public var searchBarPlaceholder: String? = "Search"
public var flagWidth: Length = 30
public var searchBarPlaceholder: String = "Search"
public var flagWidth: CGFloat = 30

public init() {}
}
Expand Down
26 changes: 21 additions & 5 deletions Source/Classes/CountryListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import SwiftUI

public struct CountryListView: View {
@ObjectBinding private var store = CountryStore()
@ObservedObject private var store = CountryStore()
@State var query: String = ""
private let raster = Columbus.config.rasterSize

public init() {}
public init() {
}

#warning("Implement a search bar!")
#warning("Implement an index bar!")
Expand All @@ -21,12 +24,25 @@ public struct CountryListView: View {
#warning("Update README.md for SPM/Binary Package and usage instructions.")
public var body: some View {
NavigationView {
List(store.countries) { country in
CountryRow(country: country).tapAction {
print(country)
VStack(spacing: raster) {
HStack(spacing: raster) {
Image(systemName: "magnifyingglass")

TextField(Columbus.config.searchBarPlaceholder, text: $query) {
self.store.filter(query: self.query)
}
.textFieldStyle(RoundedBorderTextFieldStyle())
.foregroundColor(Color(.text))
}
.padding()

List(store.filteredCountries) { country in
CountryRow(country: country)
}
}
.navigationBarTitle(Text("Countries"))
}.onAppear {
self.store.load()
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions Source/Classes/CountryRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ struct CountryRow : View {
.layoutPriority(1.0)

Text(country.name)
.color(Color(.text))
.frame(alignment: .leading)
.foregroundColor(Color(.text))

Spacer(minLength: raster)

Text(country.dialingCodeWithPlusPrefix)
.color(Color(.text))
.frame(alignment: .trailing)
.background(Color.red)
.foregroundColor(Color(.text))
.layoutPriority(0.5)
}
}
Expand Down
27 changes: 19 additions & 8 deletions Source/Classes/CountryStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,37 @@
import SwiftUI
import Combine

final class CountryStore: BindableObject {
final class CountryStore: ObservableObject {

var countries: [Country] = [] {
didSet { didChange.send() }
}
@Published var countries: [Country] = []
@Published var filteredCountries: [Country] = []

var didChange = PassthroughSubject<Void, Never>()
init() {}

init() {
loadCountries()
func filter(query: String) {
let filteredByName = self.countries.filter {
$0.name.lowercased().contains(query.lowercased())
}
let filteredByDialingCode = self.countries.filter {
"+\($0.dialingCode)".contains(query)
}

if !filteredByName.isEmpty {
self.filteredCountries = filteredByName
} else {
self.filteredCountries = filteredByDialingCode
}
}

private func loadCountries() {
func load() {
guard
let filePath = Columbus.bundle.path(forResource: "Countries", ofType: "json"),
let data = FileManager.default.contents(atPath: filePath),
let countries = try? JSONDecoder().decode(CountryList.self, from: data).countries else {
return
}
self.countries = countries
self.filteredCountries = countries
}
}

Expand Down

0 comments on commit 8818c26

Please sign in to comment.