MapSerachView.swift 2.29 KB
Newer Older
1
//
2
//  ContentView.swift
3
4
//  MapKit Testing Search Functionality
//
5
6
//  Created by Brian Porumb on 25.11.21.
//
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

import SwiftUI

class SwiftUIViewHostingController: UIHostingController<ContentView> {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder, rootView: ContentView(locationService: LocationService()))
    }
}

struct ContentView: View {
    @ObservedObject var locationService: LocationService
    
    var body: some View {
        VStack {
            Form {
                
                Section(header: Text("Search")) {
                    ZStack(alignment: .trailing) {
                        TextField("Search", text: $locationService.queryFragment)
Brian Porumb's avatar
Brian Porumb committed
26
27
28
29
                        
                        // while user is typing input it sends the current query to the location service
                        // which in turns sets its status to searching; when searching status is set on
                        // searching then a clock symbol will be shown beside the search box
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
                        if locationService.status == .isSearching {
                            Image(systemName: "clock")
                            .foregroundColor(Color.gray)
                        }
                    }
                }
                
                Section(header: Text("Results")) {
                    List {
                        Group { () -> AnyView in
                            switch locationService.status {
                                case .noResults: return AnyView(Text("No Results"))
                                case .error(let description): return AnyView(Text("Error: \(description)"))
                                default: return AnyView(EmptyView())
                                }
                        }.foregroundColor(Color.gray)
Brian Porumb's avatar
Brian Porumb committed
46
47
                                       
                        // display the results as a list
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
                        ForEach(locationService.searchResults, id: \.self) {
                            completionResult in Text(completionResult.title)
                        }
                    }
                }
                
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(locationService: LocationService())
    }
}