mobven / CountryPicker

Country Picker with unicode flags and country codes.
MIT License
66 stars 19 forks source link

region-code only country initializer #8

Closed Rashidium closed 2 years ago

Rashidium commented 2 years ago

@Rashidium What I meant was the set a default selected country for the picker.

Right now, I have set selectedCountry to TR which sets a green background in the picker view but this is not enough to pre-select the value.

https://user-images.githubusercontent.com/5788410/147405630-19fab385-5554-43b2-a7a8-2deaaef4c924.mp4

import SwiftUI
import CountryPicker

struct ContentView: View {
    @State private var country: Country?
    @State private var showCountryPicker = false

    var body: some View {
        VStack {
            Button {
                showCountryPicker = true
            } label: {
                Text("Select Country")
            }.sheet(isPresented: $showCountryPicker) {
                CountryPicker(country: $country)
            }

            Text("Selected Country: \(country?.localizedName ?? "N/A")")
        }
    }
}

Since with iOS, I can only get the Locale region, I thought it would be better to have a initializer in Country struct to set this default value.

public init(regionCode: String) {
        self.isoCode = regionCode

        if let country = CountryManager.shared.getCountries().first(where: { $0.isoCode == regionCode }) {
            self.phoneCode = country.phoneCode
        } else {
            self.phoneCode = ""
        }
    }

This way I can set @State private var country = Country(regionCode: Locale.current.regionCode ?? "TR")

Originally posted by @hcancelik in https://github.com/mobven/CountryPicker/issues/6#issuecomment-1001151984

Rashidium commented 2 years ago

@karazeynep @osmanavnikocoglu guys need your support for the issue šŸ¤ŸšŸ»

Rashidium commented 2 years ago

@karazeynep @osmanavnikocoglu šŸ‘€