Skip to content

Commit

Permalink
Bookmarks view #59: Search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 6, 2019
1 parent 43a3289 commit ec0b0b7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ struct BookmarksView: View {

@State var selectedWord = ""
@State var showConfirm = false
@State var filter = ""

private let bookmarkWidth: CGFloat = 100

var body: some View {
VStack {
BookmarksView_List(selectedWord: $selectedWord)
TextField("", text: $filter).frame(width: bookmarkWidth * 3)
BookmarksView_List(width: bookmarkWidth, filter: $filter, selectedWord: $selectedWord)
HStack {
Button(action: {
self.showConfirm = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,53 @@ import SwiftUI

struct BookmarksView_List: View {
@ObservedObject var store = Store.shared

var width: CGFloat
@Binding var filter: String
@Binding var selectedWord: String

var bookmarks: [Bookmarks] {
private var bookmarks: [Bookmarks] {
if case let .bookmarks(text) = self.store.translateAction {
Store.shared.translateAction.next()
RunLoop.main.perform {
self.selectedWord = text
}
}

return self.store.bookmarks.chunked(into: 3)
var bookmarks = self.store.bookmarks
let filter = self.filter.trimmingCharacters(in: .whitespaces)

if !filter.isEmpty {
bookmarks = bookmarks.filter { $0.text.contains(filter) }
}

return bookmarks.chunked(into: 3)
}

var body: some View {
ScrollView {
VStack {
ForEach(bookmarks, id: \.self) { chunk in
HStack {
ForEach(chunk, id: \.self) { bookmark in
HStack {
Image.sfSymbol(bookmark.checked ? "checkmark.circle.fill" : "checkmark.circle")
.onTapGesture {
self.store.bookmarks.toggle(bookmark.text)
}
Text("\(bookmark.text)")
.onTapGesture {
self.selectedWord = bookmark.text
self.store.translateAction.addAll(text: bookmark.text, except: .bookmarks)
}
.frame(width: 100, alignment: .leading)
.foregroundColor(self.selectedWord == bookmark.text ? Color.yellow : Color.primary)
}
}
self.chunkView(chunk: chunk)
}
}
}
}

private func chunkView(chunk: Bookmarks) -> some View {
HStack {
ForEach(chunk, id: \.self) { bookmark in
HStack {
Image.sfSymbol(bookmark.checked ? "checkmark.circle.fill" : "checkmark.circle")
.onTapGesture {
self.store.bookmarks.toggle(bookmark.text)
}
Text("\(bookmark.text)")
.frame(width: self.width, alignment: .leading)
.foregroundColor(self.selectedWord == bookmark.text ? Color.yellow : Color.primary)
.onTapGesture {
self.selectedWord = bookmark.text
self.store.translateAction.addAll(text: bookmark.text, except: .bookmarks)
}
}
}
Expand All @@ -52,6 +66,6 @@ struct BookmarksView_List: View {

struct BookmarksView_List_Previews: PreviewProvider {
static var previews: some View {
BookmarksView_List(selectedWord: .constant(""))
BookmarksView_List(width: 100, filter: .constant(""), selectedWord: .constant(""))
}
}
2 changes: 1 addition & 1 deletion ReaderTranslatorMac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>448</string>
<string>452</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorSafari/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>692</string>
<string>706</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppleEventsUsageDescription</key>
Expand Down

0 comments on commit ec0b0b7

Please sign in to comment.