Skip to content

Instantly share code, notes, and snippets.

# Privacy Policy for QueryPeeler
QueryPeeler does not collect, store, or transmit any personal data.
The app runs entirely on your device and does not send any information to external servers. It does not collect analytics, track user behaviour, or access sensitive information.
If you have any concerns, please contact [email protected].
@nashysolutions
nashysolutions / NetworkMonitor.swift
Created November 21, 2023 10:47
Connectivity monitor using AsyncThrowingStream.
func monitorChanges() async throws {
let stream = makeStream()
for try await status in stream {
self.status = status
}
}
private func makeStream() -> AsyncThrowingStream<Status, Error> {
AsyncThrowingStream { continuation in
let monitor = NWPathMonitor()
@nashysolutions
nashysolutions / ReportsView.swift
Created March 18, 2021 22:46
How to pass a binding from within ForEach
extension ReportStore where Report: Identifiable {
func binding(for report: Report) -> Binding<Report> {
let index = fetchedItems.firstIndex(where: { $0.id == report.id } )!
return Binding(
get: { self.fetchedItems[index] },
set: { self.fetchedItems[index] = $0 }
)
}
}
class MenuViewController: UIViewController {
let table = MenuTableHandler()
var categories: [MenuCategory] = []
var selections = Set<MenuItem>()
fileprivate var searchText: String? {
didSet {
if oldValue == searchText { return }
categories = MenuOption.allCases.map {
extension MenuViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
table.reloadItem(at: indexPath, save: { (item) in
switch selections.contains(item) {
case true: selections.remove(item)
case false: selections.insert(item)
}
}, animate: false)
}
func toggleSelectedStateOfItem(at indexPath: IndexPath) {
let item = dataSource.itemIdentifier(for: indexPath)!
var snapshot = dataSource.snapshot()
item.isSelected = !item.isSelected
snapshot.reloadItems([item])
dataSource.apply(snapshot)
}
let cellProvider: CellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(
extension MenuItem: Hashable, Comparable {
func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}
extension MenuItem: Comparable {
static func <(lhs: MenuItem, rhs: MenuItem) -> Bool {
private typealias CellProvider = (UITableView, IndexPath, MenuItem) -> UITableViewCell?
private let cellProvider: CellProvider = { (tableView, indexPath, item) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(
withIdentifier: MenuItemTableViewCell.identifier,
for: indexPath) as! MenuItemTableViewCell
cell.accessoryType = item.isSelected ? .checkmark : .none
cell.textLabel?.text = item.name
return cell
}
struct MenuCategory {
let name: String
let description: String?
let items: Set<MenuItem>
}
struct MenuItem {
let name: String
let description: String
let category: MenuCategory