mobven / CountryPicker

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

'getCountries' is inaccessible due to 'internal' protection level #6

Closed hcancelik closed 2 years ago

hcancelik commented 2 years ago

I'm getting this error when I try to use getCountries method on CountryManager singleton.

Any idea how I can fix this?

Screen Shot 2021-12-14 at 21 38 44

Additionally is there a way to get Country without providing phone code and isoCode?

Thanks in advance.

Rashidium commented 2 years ago

Hello @hcancelik . I've release v1.0.2 fixing accessibility of getCountries.

For the 2nd part of your question. Can you please give details what do you mean by "way to get Country"?

hcancelik commented 2 years ago

Hi @Rashidium

Thanks for the update.

What I mean by getting the Country is basically an initializer in Country. Something like this:

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

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

Since there is no way to get the phone code with iOS, this will help anyone to set a default value of the picker by using the Locale.current.region

Rashidium commented 2 years ago

@hcancelik have you checked the CountryPickerViewController.selectedCountry? It can be used to set selected country by country code.

hcancelik commented 2 years ago

@hcancelik have you checked the CountryPickerViewController.selectedCountry? It can be used to set selected country by country code.

Yes I did. I might be doing something wrong but it doesn't provide a default value for the picker in swiftui. It only highlights the country in the list view.

Rashidium commented 2 years ago

can you please give any detail about highlighting? Do you mean selected background color under country code?

Also, could you please set selectedCountry in func updateUIViewController(_ uiViewController: CountryPickerViewController, context: Context) function?

hcancelik 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")

Rashidium commented 2 years ago

Ok I see what you meant now 👍🏻 Thanks for details. Though your solution would work, I think there could be better way to init Country without need to isoCode. I think we better open a pull request for the request you made.