Skip to content

Commit

Permalink
simplified package; added images to README
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Es committed Apr 1, 2023
1 parent 20668ab commit 2a793d8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 172 deletions.
Binary file added Images/PresetsPicker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/TextField.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,30 @@ _SwiftTextFieldPreset_ is part of the [OpenAlloc](https://github.com/openalloc)

## Features

Text Field | Presets Picker
:---:|:---:
![](https://github.com/openalloc/SwiftTextFieldPreset/blob/main/Images/TextField.png) | ![](https://github.com/openalloc/SwiftTextFieldPreset/blob/main/Images/PresetsPicker.png.png)

* Builds on existing `TextField` component
* Presently targeting .macOS(.v13), .iOS(.v16), .watchOS(.v9)
* Only Apple's _Collections_ as an external dependency

## Installation

In your `Package.swift`:

To package `.dependencies` add

```swift
.package(url: "https://github.com/openalloc/SwiftTextFieldPreset.git", .upToNextMajor(from: "1.1.2")),
```

To product `.dependencies` add

```swift
.product(name: "TextFieldPreset", package: "SwiftTextFieldPreset"),
```

## Example

A simple example where the presets are `String` type:
Expand Down Expand Up @@ -45,7 +65,7 @@ struct MyView: View {
}
```

Note that presets can be more than `String`, such as `struct`s with additional values that can be assigned via the `onSelect` callback.
Note that presets can be more than `String`, such as `struct`s with additional values that can used to populate your target value/object via the `onSelect` callback.

## TODO

Expand Down
59 changes: 11 additions & 48 deletions Sources/PresetsPickerSingle.swift → Sources/PresetsPicker.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// PresetsPickerSingle.swift
// PresetsPicker.swift
//
// Copyright 2023 OpenAlloc LLC
//
Expand All @@ -20,70 +20,33 @@ import Collections
import os
import SwiftUI

public struct PresetsPickerSingle<Key, PresettedItem, Label>: View
struct PresetsPicker<Key, PresettedItem, Label>: View
where Key: Hashable & CustomStringConvertible,
PresettedItem: PresettableItem,
Label: View
{
public typealias PresetsDictionary = OrderedDictionary<Key, [PresettedItem]>
typealias PresetsDictionary = OrderedDictionary<Key, [PresettedItem]>

// MARK: - Parameters

private let presets: PresetsDictionary
private let onSelect: (PresettedItem) -> Void
private let label: (PresettedItem) -> Label

public init(presets: PresetsDictionary,
onSelect: @escaping (PresettedItem) -> Void,
label: @escaping (PresettedItem) -> Label)
{
self.onSelect = onSelect
self.presets = presets
self.label = label
}

// MARK: - Locals

@State private var selected: PresettedItem?
let presets: PresetsDictionary
let onSelect: (PresettedItem) -> Void
let label: (PresettedItem) -> Label

// MARK: - Views

public var body: some View {
platformView {
var body: some View {
List {
ForEach(presets.elements, id: \.key) { element in
Section(header: Text(element.key.description)) {
ForEach(element.value, id: \.self) { item in
label(item)
#if os(watchOS)
.onTapGesture {
selected = item
}
#endif
Button(action: { onSelect(item) },
label: { label(item) })
}
}
}
}
.onChange(of: selected) { val in
guard let val else { return }
onSelect(val)
}
}

#if os(watchOS)
private func platformView(content: () -> some View) -> some View {
List {
content()
}
}
#endif

#if !os(watchOS)
private func platformView(content: () -> some View) -> some View {
List(selection: $selected) {
content()
}
}
#endif
}

struct PresetsPickerSingle_Previews: PreviewProvider {
Expand All @@ -105,7 +68,7 @@ struct PresetsPickerSingle_Previews: PreviewProvider {
@State var selected: String?
var body: some View {
NavigationStack {
PresetsPickerSingle(presets: presets) { _ in
PresetsPicker(presets: presets) { _ in

} label: {
Text($0.text)
Expand Down
112 changes: 0 additions & 112 deletions Sources/PresetsPickerMulti.swift

This file was deleted.

22 changes: 11 additions & 11 deletions Sources/TextFieldPreset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,16 @@ public struct TextFieldPreset<ButtonLabel, GroupKey, PresettedItem, PickerLabel>
TextField(text: $text,
prompt: Text(prompt),
axis: axis) { EmptyView() }
// #if os(iOS)
// .lineLimit(5)
// #endif

Button(action: { isPresented = true }, label: buttonLabel)
Button(action: { isPresented = true },
label: buttonLabel)
.buttonStyle(.borderless)
}
.sheet(isPresented: $isPresented) {
NavigationStack {
PresetsPickerSingle(presets: presets,
onSelect: selectAction,
label: pickerLabel)
PresetsPicker(presets: presets,
onSelect: selectAction,
label: pickerLabel)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") { isPresented = false }
Expand Down Expand Up @@ -110,16 +108,18 @@ struct TextFieldWithPresets_Previews: PreviewProvider {
@State var name: String = "New Exercise"
var body: some View {
Form {
TextFieldPreset($name, prompt: "Enter name", axis: .vertical, presets: presets, pickerLabel: {
Text($0.description)
})
Section("TextField with Preset") {
TextFieldPreset($name, prompt: "Enter name", axis: .vertical, presets: presets, pickerLabel: {
Text($0.description)
})
}
}
.padding()
}
}

static var previews: some View {
TestHolder()
.accentColor(.orange)
//.accentColor(.orange)
}
}

0 comments on commit 2a793d8

Please sign in to comment.