GithubHelp home page GithubHelp logo

toastersocks / multipicker Goto Github PK

View Code? Open in Web Editor NEW
28.0 28.0 6.0 845 KB

A Picker for iOS that can be used to choose from one, one or none, or multiple options.

License: MIT License

Swift 100.00%
multipicker picker swift swiftui

multipicker's People

Contributors

toastersocks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

multipicker's Issues

The "none" text representation for optional selections should be configurable.

Situation

Right now, in navigationLink style pickers, when an optional binding is given to selection and there is nothing selected, the choice is represented by the string "(None)". This is localizable, but not otherwise configurable.

Solution

There should be a way to configure how the "none" choice is represented, with "(None)" being default.
It should be configurable with some View with a convenience String option.

Dilemma

I'm not sure if these should be init parameters, or view modifiers though.

Init approach

For

  • This is view content so seems more appropriate to pass into the init

Against

I'm hesitant to add more parameters to the init because:

  • It strays farther from SwiftUI.Picker API
  • and because the init already has two other view parameters in the init (the Label and content views), and the init might get pretty unwieldy with all these ViewBuilder closure parameters.

View Modifier approach

For

  • Don't have to change the init API
  • Can apply the configuration to multiple MultiPickers

Against

But on the other hand:

  • Creating more view modifiers that are only applicable to MultiPicker
  • view modifiers that take view content seems like not purely configuration

The selection indicator should be configurable

Situation

Right now the selection indicator is always a checkmark that matches SwiftUI's Picker. It might be nice for this to be configurable.

Solution

There should be a way to configure what the selection indicator is with the current checkmark being default.

Possible approaches

some View

Pass in a ViewBuilder closure to make the selection indicator be whatever we want.

A set of defaults

There could be a default set of selection indicators such as checkmark, bullet, highlight, radioButton, etc...

Combo

We could combine these two approaches with a set of built-in indicators, but also allow a totally custom indicator.

View Modifier vs. init parameter

I would lean more towards a view modifier rather than init parameter for this one.

Custom Struct

Situation

Wrong initialiser used when I was using this struct:

struct CurrencyPair: Hashable, Identifiable, Codable {
    var id: String { description }
    
    var base: String
    var quote: String
    var enabled: Bool
    
    var description: String {
        "\(base)/\(quote)"
    }
}

with this code:

@State private var currencyPairSelection: Set<CurrencyPair>
@State private var currencyPairOptions: [CurrencyPair]

var settings: Settings

init(settings: Settings) {
    self.settings = settings
    _currencyPairOptions = State(initialValue: settings.currencyPairs)
    
    let set =  Set(settings.currencyPairs.filter { $0.enabled })
    _currencyPairSelection = State(initialValue: set)
}

            MultiPicker(selection: $currencyPairSelection, content: {
                ForEach(currencyPairOptions, id: \.self) { option in
                    Text(option.description).mpTag(option)
                }
            }, label: {
                Text("Enabled Currencies")
            })
            .mpPickerStyle(.navigationLink)

`

I wanted a Multiple option picker, but could not find the reason this was not working. So I CMD + Clicked on the MultiPicker to see what Initialiser was being used, turns out the selection property was set to single.

After I commented out the other intialisers except for the Binding<Set<>> one, Xcode finally gave the error that my struct needed to conform to CustomStringConvertible.

Finally, after adding this to the struct like this:

 struct CurrencyPair: Hashable, Identifiable, Codable, CustomStringConvertible {
    var id: String { description }
    
    var base: String
    var quote: String
    var enabled: Bool
    
    var description: String {
        "\(base)/\(quote)"
    }
}

it works perfectly!

Solution

Either add to the documentation that a custom struct must conform to CustomStringConvertible, or some kind of extra check to get the correct initialiser. Thanks!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.