Skip to content

Commit

Permalink
Merge pull request #111 from SURYAKANTSHARMA/feature/complete_navigat…
Browse files Browse the repository at this point in the history
…ionAndDismissSwiftui-

Feature/complete navigation and dismiss swiftui
  • Loading branch information
SURYAKANTSHARMA authored Aug 15, 2023
2 parents 970a130 + 66c409d commit 046eb15
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import SKCountryPicker

struct ContentView: View {
@State private var isCountryPickerPresented = false
@State private var selectedCountry: Country? = CountryManager.shared.currentCountry

@State private var selectedCountry: Country? = CountryManager.shared.lastCountrySelected ?? CountryManager.shared.currentCountry
var body: some View {
NavigationView {
VStack {
Expand All @@ -27,15 +27,13 @@ struct ContentView: View {
}
.sheet(isPresented: $isCountryPickerPresented) {
// CountryPickerWheelView()
CountryPickerView(configuration: Configuration())
// CountryPickerWithSections()
// CountryPickerView(configuration: Configuration(),
// selectedCountry: $selectedCountry)
CountryPickerWithSections(
selectedCountry: $selectedCountry)

}
.padding(.bottom, 50)
NavigationLink(destination:
CountryPickerView(configuration: Configuration())) {
Text("Select country Picker")
}
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ struct Configuration {

public init(
flagStyle: CountryFlagStyle = CountryFlagStyle.corner,
labelFont: Font = .title3,
labelColor: Color = .black,
labelFont: Font = .title2,
labelColor: Color = .primary,
detailFont: Font = .footnote,
detailColor: Color = .gray,
detailColor: Color = .secondary,
isCountryFlagHidden: Bool = false,
isCountryDialHidden: Bool = false,
navigationTitleText: String = "CountryPicker"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,37 @@ enum CountryFlagStyle {

public
struct CountryPickerView: View {
let manager: any CountryListDataSource

@Environment(\.presentationMode) var presentationMode

@State private var filterCountries = [Country]()
@State private var applySearch = false
@State private var searchText = ""
@State private var selectedCountry: Country?
@Binding private var selectedCountry: Country?

let configuration: Configuration
let manager: any CountryListDataSource

private var searchResults: [Country] {
searchText.isEmpty ? manager.allCountries([]) : filterCountries
}

public
init(manager: any CountryListDataSource = CountryManager.shared,
configuration: Configuration) {
configuration: Configuration = Configuration(),
selectedCountry: Binding<Optional<Country>>) {
self.manager = manager
self.configuration = configuration
self._selectedCountry = selectedCountry
}

public var body: some View {
NavigationView {
List(searchResults) { country in
CountryCell(country: country,
isFavorite: selectedCountry == country,
selectedCountry: $selectedCountry,
configuration: configuration)
.onTapGesture {
selectedCountry = country
}
isSelected: selectedCountry == country,
configuration: configuration,
selectedCountry: $selectedCountry)
}.listStyle(.grouped)
.searchable(text: $searchText)
.navigationTitle("Country Picker")
Expand All @@ -54,16 +57,32 @@ struct CountryPickerView: View {
.onDisappear {
manager.lastCountrySelected = selectedCountry
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "xmark")
.font(.callout)
}
}
}
}
.onChange(of: selectedCountry) { newValue in
if newValue != nil {
presentationMode.wrappedValue.dismiss()
}
}
}
}

struct CountryCell: View {

let country: Country
let isFavorite: Bool
@Binding var selectedCountry: Country?
let isSelected: Bool
let configuration: Configuration

@Binding var selectedCountry: Country?

var body: some View {
Button {
Expand Down Expand Up @@ -103,8 +122,9 @@ struct CountryCell: View {
}

Spacer()
if isFavorite {
Image(uiImage: UIImage(named: "tickMark", in: bundle, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate) ?? .init())
if isSelected {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
}
}
}
Expand All @@ -113,7 +133,9 @@ struct CountryCell: View {

struct CountryPickerView_Previews: PreviewProvider {
static var previews: some View {
CountryPickerView(configuration: Configuration())
CountryPickerView(
configuration: Configuration(),
selectedCountry: .constant(Country(countryCode: "IN")))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class CountryPickerWithSectionViewModel: ObservableObject {
private let dataService: any CountryListDataSource
private let mapper: SectionMapper

internal init(dataService: any CountryListDataSource,
mapper: SectionMapper
internal init(dataService: any CountryListDataSource = CountryManager.shared,
mapper: SectionMapper = SectionMapper(favoriteCountriesLocaleIdentifiers: []),
lastCountry: Country? = CountryManager.shared.lastCountrySelected ??
CountryManager.shared.currentCountry
) {
self.dataService = dataService
self.mapper = mapper
self.selectedCountry = dataService.lastCountrySelected
self.selectedCountry = selectedCountry
self.selectedCountry = lastCountry
sections = mapper.mapIntoSection(countries: dataService.allCountries(mapper.favoriteCountriesLocaleIdentifiers))
}

Expand All @@ -38,5 +39,9 @@ class CountryPickerWithSectionViewModel: ObservableObject {
func setLastSelectedCountry() {
dataService.lastCountrySelected = selectedCountry
}

func reset() {
filterWithText("")
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
import SwiftUI
public
struct CountryPickerWithSections: View {

@Environment(\.presentationMode) var presentationMode

@StateObject var viewModel: CountryPickerWithSectionViewModel = .default
let configuration: Configuration
@ObservedObject var viewModel: CountryPickerWithSectionViewModel = .default
@State var searchText: String

public init(configuration: Configuration = Configuration(),
searchText: String = "") {
self.configuration = configuration
@Binding private var selectedCountry: Country?

let configuration: Configuration

public init(
configuration: Configuration = Configuration(),
searchText: String = "",
selectedCountry: Binding<Optional<Country>>) {
self.configuration = configuration
_searchText = State(initialValue: searchText)
_selectedCountry = selectedCountry
viewModel.selectedCountry = selectedCountry.wrappedValue
}

public var body: some View {
Expand All @@ -30,12 +37,9 @@ struct CountryPickerWithSections: View {

ForEach(section.countries) { country in
CountryCell(country: country,
isFavorite: false,
selectedCountry: $viewModel.selectedCountry,
configuration: configuration)
.onTapGesture {
viewModel.selectedCountry = country
}
isSelected: selectedCountry == country,
configuration: configuration,
selectedCountry: $viewModel.selectedCountry)
}
} header: {
if let sectionTitle = section.title {
Expand All @@ -55,19 +59,28 @@ struct CountryPickerWithSections: View {
.onChange(of: searchText) {
viewModel.filterWithText($0)
}
.onChange(of: viewModel.selectedCountry) { newValue in
if newValue != nil {
selectedCountry = newValue
presentationMode.wrappedValue.dismiss()
}
}

.onDisappear {
viewModel.setLastSelectedCountry()

viewModel.reset()
}
.onAppear {
// Scroll to the selected country when appearing
if let selectedCountry = viewModel.selectedCountry {
withAnimation {
scrollView.scrollTo(selectedCountry.countryName, anchor: .top)
.listStyle(.grouped)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "xmark")
.font(.callout)
}
}
}
}
.listStyle(.grouped)
}
}
.searchable(text: $searchText)
Expand All @@ -78,7 +91,9 @@ struct CountryPickerWithSections: View {
struct CountryPickerWithSections_Previews: PreviewProvider {
static var previews: some View {
CountryPickerWithSections(
configuration: Configuration(), searchText: ""
configuration: Configuration(),
searchText: "",
selectedCountry: .constant(.none)
)
}
}
Expand All @@ -92,7 +107,6 @@ struct SectionIndexView: View {
ForEach(titles, id: \.self) { title in
HStack {
Spacer()
//need to figure out if there is a name in this section before I allow scrollto or it will crash
Button(action: {
withAnimation {
onClick(title)
Expand Down
Binary file not shown.

0 comments on commit 046eb15

Please sign in to comment.