Skip to content

Commit

Permalink
add support for iPad. (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Livinglist authored Jun 7, 2023
1 parent 5748f52 commit 87dc751
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ public class SearchParams: Equatable {

public var filteredQuery: String {
var buffer = String()
guard let encodedQuery = URLComponents(string: query)?.string.orEmpty else { return String() }

if sorted {
buffer.append("search_by_date?query=\(encodedQuery)")
buffer.append("search_by_date?query=\(query)")
} else {
buffer.append("search?query=\(encodedQuery)")
buffer.append("search?query=\(query)")
}

if !filters.isEmpty {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class SearchRepository {
private let baseUrl = "https://hn.algolia.com/api/v1/"

public func search(params: SearchParams, onItemFetched: @escaping (any Item) -> Void) async -> Void {
guard let urlStr = "\(baseUrl)\(params.filteredQuery)".addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed),
guard let urlStr = "\(baseUrl)\(params.filteredQuery)"
.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed),
let url = URL(string: urlStr) else { return }
let response = await AF.request(url).serializingString().response

Expand Down
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ Another [Hacker News](https://news.ycombinator.com/) client made with SwiftUI.

<p align="center">
<img width="240" alt="01" src="Resources/Screenshots/iphone-1.png">
<img width="240" alt="03" src="Resources/Screenshots/iphone-2.png">
<img width="240" alt="04" src="Resources/Screenshots/iphone-3.png">

<img width="240" alt="01" src="Resources/Screenshots/extra2-1.png">
<img width="240" alt="03" src="Resources/Screenshots/extra2-2.png">
<img width="240" alt="04" src="Resources/Screenshots/extra2-3.png">

<img width="240" alt="01" src="Resources/Screenshots/iphone-4.png">
<img width="240" alt="03" src="Resources/Screenshots/iphone-5.png">
<img width="240" alt="04" src="Resources/Screenshots/iphone-6.png">

<img width="240" alt="01" src="Resources/Screenshots/extra2-4.png">
<img width="240" alt="03" src="Resources/Screenshots/extra2-5.png">
<img width="240" alt="04" src="Resources/Screenshots/extra2-6.png">
<img width="240" alt="02" src="Resources/Screenshots/iphone-2.png">
<img width="240" alt="03" src="Resources/Screenshots/iphone-3.png">

<img width="240" alt="04" src="Resources/Screenshots/extra2-1.png">
<img width="240" alt="05" src="Resources/Screenshots/extra2-2.png">
<img width="240" alt="06" src="Resources/Screenshots/extra2-3.png">

<img width="240" alt="07" src="Resources/Screenshots/iphone-4.png">
<img width="240" alt="08" src="Resources/Screenshots/iphone-5.png">
<img width="240" alt="09" src="Resources/Screenshots/iphone-6.png">

<img width="240" alt="10" src="Resources/Screenshots/extra2-4.png">
<img width="240" alt="11" src="Resources/Screenshots/extra2-5.png">
<img width="240" alt="12" src="Resources/Screenshots/extra2-6.png">

<img width="360" alt="ipad01" src="Resources/Screenshots/ipad-1.png">
<img width="360" alt="ipad02" src="Resources/Screenshots/ipad-2.png">
<img width="360" alt="ipad03" src="Resources/Screenshots/ipad-3.png">
<img width="360" alt="ipad04" src="Resources/Screenshots/ipad-4.png">
</p>


Binary file added Resources/Screenshots/ipad-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/Screenshots/ipad-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/Screenshots/ipad-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/Screenshots/ipad-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/zcombinator-ipad.xcf
Binary file not shown.
10 changes: 10 additions & 0 deletions Shared/Models/Status.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
public enum Status {
case idle
// Loading with visible loading indicator.
case loading
// Loading with no loading indicator.
case backgroundLoading
case loaded
case error

var isLoading: Bool {
switch self {
case .loading, .backgroundLoading: return true
default: return false
}
}
}
45 changes: 21 additions & 24 deletions Shared/Models/Stores/ItemStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ extension ItemView {
@Published var status: Status = .idle
@Published var item: (any Item)?
@Published var loadingItem: Int?
@Published var loadedItems = Set<Int>()
@Published var collapsed = Set<Int>()
@Published var hidden = Set<Int>()
@Published var loadedItems: Set<Int> = .init()
@Published var collapsed: Set<Int> = .init()
@Published var hidden: Set<Int> = .init()

func loadKids(of cmt: Comment) async {
if let parentIndex = kids.firstIndex(of: cmt),
Expand All @@ -35,6 +35,8 @@ extension ItemView {
}

func refresh() async -> Void {
if status.isLoading { return }

if let id = self.item?.id {
withAnimation {
self.kids.removeAll()
Expand All @@ -49,22 +51,17 @@ extension ItemView {
let kids = item.kids {
self.item = item

var comments = [Comment]()

await StoriesRepository.shared.fetchComments(ids: kids) { comment in
comments.append(comment.copyWith(level: 0))
}


withAnimation {
self.kids.append(contentsOf: comments)
self.status = .loaded
}
} else {
withAnimation {
self.status = .loaded
DispatchQueue.main.async {
withAnimation {
self.status = .backgroundLoading
self.kids.append(comment.copyWith(level: 0))
}
}
}
}

self.status = .loaded
}
}

Expand All @@ -79,30 +76,30 @@ extension ItemView {
func collapse(cmt: Comment) {
collapsed.insert(cmt.id)

hide(cmt: cmt)
hide(kidsOf: cmt)
}

func uncollapse(cmt: Comment) {
collapsed.remove(cmt.id)

unhide(cmt: cmt)
unhide(kidsOf: cmt)
}

func hide(cmt: Comment) {
guard let kids = cmt.kids else { return }
func hide(kidsOf parent: Comment) {
guard let kids = parent.kids else { return }

for childId in kids {
let child = self.kids.first { $0.id == childId }
guard let child = child else {
continue
}
hidden.insert(childId)
hide(cmt: child)
hide(kidsOf: child)
}
}

func unhide(cmt: Comment) {
guard let kids = cmt.kids else { return }
func unhide(kidsOf parent: Comment) {
guard let kids = parent.kids else { return }

for childId in kids {
let child = self.kids.first { $0.id == childId }
Expand All @@ -112,7 +109,7 @@ extension ItemView {

hidden.remove(childId)
if collapsed.contains(childId) == false {
unhide(cmt: child)
unhide(kidsOf: child)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Shared/Models/Stores/StoryStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension HomeView {

private let pageSize: Int = 10
private var currentPage: Int = 0
private var storyIds: [Int] = [Int]()
private var storyIds: [Int] = .init()

func fetchStories() async {
self.status = .loading
Expand Down
Loading

0 comments on commit 87dc751

Please sign in to comment.