Skip to content

fix PickerMenu issues #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Core/Core/View/Base/FlexibleKeyboardInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public struct FlexibleKeyboardInputView: View {
}.frame(maxWidth: .infinity, maxHeight: commentSize + 16)
.background(
CoreAssets.commentCellBackground.swiftUIColor
.ignoresSafeArea()
)
.overlay(
GeometryReader { proxy in
Expand Down
145 changes: 90 additions & 55 deletions Core/Core/View/Base/PickerMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,68 +42,103 @@ public struct PickerMenu: View {
self.items = items
self.titleText = titleText
self.router = router
if let selectedItem {
self._selectedItem = State(initialValue: selectedItem)
}
self._selectedItem = State(initialValue: selectedItem ?? items.first ?? PickerItem(key: "", value: ""))
self.selected = selected
}


private var filteredItems: [PickerItem] {
if search.isEmpty {
return items
} else {
return items.filter { $0.value.localizedCaseInsensitiveContains(search) }
}
}

private var isSingleSelection: Bool {
return filteredItems.count == 1
}

private var isItemSelected: Bool {
return filteredItems.contains(selectedItem)
}

private var acceptButtonDisabled: Bool {
return !isItemSelected && !isSingleSelection
}

public var body: some View {
VStack {
ZStack(alignment: .bottom) {
Color.black.opacity(0.4)
.onTapGesture(perform: {
router.dismiss(animated: true)
})
Color.black.opacity(0.4)
.ignoresSafeArea()
.onTapGesture {
router.dismiss(animated: true)
}
VStack {
Spacer()
VStack {
VStack {
Text(titleText)
.foregroundColor(CoreAssets.textPrimary.swiftUIColor)
TextField(CoreLocalization.Picker.search, text: $search)
.padding(.all, 8)
.background(
CoreAssets.textInputStroke.swiftUIColor
.cornerRadius(6))
Picker("", selection: $selectedItem) {
let filteredItems = items
.filter({ $0.value.contains(search) })
ForEach(filteredItems.count != 0 ? filteredItems : items,
id: \.self) {
Text($0.value)
.foregroundColor(CoreAssets.textPrimary.swiftUIColor)
}
}.pickerStyle(.wheel)

}.frame(minWidth: 0, maxWidth: idiom == .pad ? ipadPickerWidth : .infinity)
.padding()
.background(
CoreAssets.textInputBackground.swiftUIColor
.cornerRadius(16)
).padding(.horizontal, 16)

Button(action: {
if items
.filter({ $0.value.contains(search) }).count == 1 {
selectedItem = items
.filter({ $0.value.contains(search) })[0]
Text(titleText)
.foregroundColor(CoreAssets.textPrimary.swiftUIColor)
TextField(CoreLocalization.Picker.search, text: $search)
.padding(.all, 8)
.background(CoreAssets.textInputStroke.swiftUIColor.cornerRadius(6))
Picker("", selection: $selectedItem) {
ForEach(filteredItems, id: \.self) { item in
Text(item.value)
.foregroundColor(CoreAssets.textPrimary.swiftUIColor)
}
selected(selectedItem)
router.dismiss(animated: true)
}, label: {
Text(CoreLocalization.Picker.accept)
.foregroundColor(CoreAssets.textPrimary.swiftUIColor)
.frame(minWidth: 0, maxWidth: idiom == .pad ? ipadPickerWidth : .infinity)
.padding()
.background(
CoreAssets.textInputBackground.swiftUIColor
.cornerRadius(16)
).padding(.horizontal, 16)
})
.padding(.bottom, 50)

}.transition(.move(edge: .bottom))
}
.pickerStyle(.wheel)
}
.frame(minWidth: 0, maxWidth: idiom == .pad ? ipadPickerWidth : .infinity)
.padding()
.background(CoreAssets.textInputBackground.swiftUIColor.cornerRadius(16))
.padding(.horizontal, 16)
.onChange(of: search, perform: { _ in
if let first = filteredItems.first {
self.selectedItem = first
}
})

Button(action: {
selected(selectedItem)
router.dismiss(animated: true)
}) {
Text(CoreLocalization.Picker.accept)
.foregroundColor(CoreAssets.textPrimary.swiftUIColor)
.frame(minWidth: 0, maxWidth: idiom == .pad ? ipadPickerWidth : .infinity)
.padding()
.background(CoreAssets.textInputBackground.swiftUIColor.cornerRadius(16))
.padding(.horizontal, 16)
}
.padding(.bottom, 4)
.disabled(acceptButtonDisabled)
}
.avoidKeyboard(dismissKeyboardByTap: true)
.transition(.move(edge: .bottom))
}
}.transition(.opacity)
.ignoresSafeArea()
}
.transition(.opacity)

}

}

#if DEBUG
struct PickerMenu_Previews: PreviewProvider {
static var previews: some View {

let items = [
PickerItem(key: "Uk", value: "Ukraine"),
PickerItem(key: "Us", value: "United States of America"),
PickerItem(key: "En", value: "England")
]

return PickerMenu(items: items,
titleText: "Select country",
router: BaseRouterMock(),
selectedItem: items.first,
selected: {_ in})
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public struct ResponsesView: View {
}
}
}
}
}.edgesIgnoringSafeArea(.bottom)
.background(
CoreAssets.background.swiftUIColor
.ignoresSafeArea()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public struct ThreadView: View {
}
}
}
}
}.edgesIgnoringSafeArea(.bottom)
.background(
CoreAssets.background.swiftUIColor
.ignoresSafeArea()
Expand Down