-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/powersearch' into develop
Finished advanced searching page, will move to finalize the last UI elements and Error handling
- Loading branch information
Showing
6 changed files
with
224 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
FlinkChallenge/FlinkChallenge/Controller/CharacterFilteringController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// CharacterFilteringController.swift | ||
// FlinkChallenge | ||
// | ||
// Created by Fernando Martin Garcia Del Angel on 19/02/20. | ||
// Copyright © 2020 Fernando Martin Garcia Del Angel. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class CharacterFiltering : ObservableObject, RandomAccessCollection { | ||
typealias Element = APICharacter | ||
@Published var characterListItems = [APICharacter]() | ||
|
||
var startIndex: Int { characterListItems.startIndex } | ||
var endIndex: Int { characterListItems.endIndex } | ||
var loadStatus = LoadStatus.ready(nextPage: 1) | ||
|
||
var baseURL = "https://rickandmortyapi.com/api/character/?" | ||
|
||
init(name : String, status : String, species: String, type : String, gender: String){ | ||
loadFilteredCharacters(params: [name,status,species,type, gender]) | ||
} | ||
|
||
subscript(position: Int) -> APICharacter { | ||
return characterListItems[position] | ||
} | ||
|
||
func loadFilteredCharacters(params : [String]) { | ||
var leadingQueryString : String = "" | ||
let labels = ["name","status","species","type","gender"] | ||
for (index, param) in params.enumerated() { | ||
if !param.isEmpty { | ||
leadingQueryString += "\(labels[index])=\(param.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? "")" | ||
} | ||
|
||
if index != params.count { | ||
leadingQueryString += "&" | ||
} | ||
} | ||
let completeURL = "\(baseURL)\(leadingQueryString)" | ||
let url = URL(string: completeURL)! | ||
let task = URLSession.shared.dataTask(with: url,completionHandler: parseCharactersFromResponse(data:response:error:)) | ||
task.resume() | ||
} | ||
|
||
func parseCharactersFromResponse(data: Data?, response : URLResponse?, error : Error?) { | ||
guard error == nil else { | ||
print("Error: \(error?.localizedDescription ?? "Unknown error produced")") | ||
return | ||
} | ||
|
||
guard let data = data else { | ||
print("Error: \(error?.localizedDescription ?? "Unknown error produced")") | ||
return | ||
} | ||
|
||
let apiCharacters = parseCharactersFromData(data: data) | ||
DispatchQueue.main.async { | ||
self.characterListItems.append(contentsOf: apiCharacters) | ||
if apiCharacters.count == 0 { | ||
print("load status done") | ||
} else { | ||
print("load status unknown") | ||
} | ||
} | ||
} | ||
|
||
func parseCharactersFromData(data: Data) -> [APICharacter] { | ||
var response : APICharactersResponse | ||
do { | ||
response = try JSONDecoder().decode(APICharactersResponse.self, from: data) | ||
} catch { | ||
print(error.localizedDescription) | ||
return [] | ||
} | ||
|
||
return response.results ?? [] | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
FlinkChallenge/FlinkChallenge/Views/AdvancedFilterView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// AdvancedFilterView.swift | ||
// FlinkChallenge | ||
// | ||
// Created by Fernando Martin Garcia Del Angel on 19/02/20. | ||
// Copyright © 2020 Fernando Martin Garcia Del Angel. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct RoundedButton : View { | ||
var name : String = "" | ||
var status : String = "" | ||
var species : String = "" | ||
var type: String = "" | ||
var gender : String = "" | ||
|
||
var body: some View { | ||
NavigationLink(destination: | ||
CharacterFilterView(name: name, | ||
status: status, | ||
species: species, | ||
type: type, | ||
gender: gender)) { | ||
Button(action: {}){ | ||
HStack { | ||
Spacer() | ||
Text("Search") | ||
.font(.headline) | ||
Spacer() | ||
} | ||
} | ||
.padding(.vertical, 10.0) | ||
.padding(.horizontal, 50) | ||
} | ||
} | ||
} | ||
|
||
struct AdvancedFilterView: View { | ||
var statuses = ["alive","dead","unknown"] | ||
var genders = ["female","male","genderless","unknown"] | ||
@State private var selectedStatus = 0 | ||
@State private var selectedGender = 0 | ||
@State private var name: String = "" | ||
@State private var status: String = "" | ||
@State private var species : String = "" | ||
@State private var type : String = "" | ||
@State private var gender : String = "" | ||
|
||
var body: some View { | ||
Form { | ||
Section(header: Text("Basic Data")) { | ||
TextField("Name",text: $name) | ||
} | ||
|
||
Section(header: Text("Other data")) { | ||
Picker (selection: $selectedStatus, label: Text("Status")) { | ||
ForEach(0 ..< statuses.count) { | ||
Text(self.statuses[$0].capitalized) | ||
} | ||
}.pickerStyle(SegmentedPickerStyle()) | ||
|
||
Picker (selection: $selectedGender, label: Text("Gender")) { | ||
ForEach(0 ..< genders.count) { | ||
Text(self.genders[$0].capitalized) | ||
} | ||
}.pickerStyle(SegmentedPickerStyle()) | ||
TextField("Species",text: $species) | ||
TextField("Type",text: $type) | ||
|
||
} | ||
RoundedButton(name: name, | ||
status: statuses[selectedStatus], | ||
species: species, | ||
type: type, | ||
gender: genders[selectedGender]) | ||
} | ||
} | ||
} | ||
|
||
struct AdvancedFilterView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
AdvancedFilterView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
FlinkChallenge/FlinkChallenge/Views/CharacterFilterView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// CharacterFilterView.swift | ||
// FlinkChallenge | ||
// | ||
// Created by Fernando Martin Garcia Del Angel on 19/02/20. | ||
// Copyright © 2020 Fernando Martin Garcia Del Angel. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct CharacterFilterView: View { | ||
@ObservedObject var characterFeed : CharacterFiltering | ||
@State private var searchText : String = "" | ||
|
||
init(name : String, status: String, species : String, type: String, gender: String) { | ||
characterFeed = CharacterFiltering(name: name, status: status, species: species, type: type, gender: gender) | ||
} | ||
|
||
var body: some View { | ||
VStack { | ||
SearchBar(text: $searchText, placeholder: "Search for characters") | ||
List { | ||
ForEach(self.characterFeed.filter { | ||
self.searchText.isEmpty ? true : $0.name!.lowercased().contains(self.searchText.lowercased()) | ||
}, id: \.id) { character in | ||
ZStack { | ||
Card(character: character).frame(width: 300, height: 300) | ||
NavigationLink(destination: CharacterDetail(character: character)) { | ||
EmptyView() | ||
}.buttonStyle(PlainButtonStyle()) | ||
} | ||
} | ||
} | ||
}.navigationBarTitle(Text("Search Results")) | ||
} | ||
} | ||
|
||
struct CharacterFilterView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
CharacterFilterView(name: "Rick", status: "", species: "", type: "", gender: "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters