Skip to content

Commit

Permalink
Implemented tag suggestions from the api
Browse files Browse the repository at this point in the history
  • Loading branch information
CaramelKat committed Jul 21, 2022
1 parent cdecde7 commit 35d68e8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
10 changes: 8 additions & 2 deletions Tanuki's Stash.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
027E2B422785004600FCEDB2 /* SwiftUIGIF in Frameworks */ = {isa = PBXBuildFile; productRef = 027E2B412785004600FCEDB2 /* SwiftUIGIF */; };
027E2B4527850E2E00FCEDB2 /* VideoPlayer in Frameworks */ = {isa = PBXBuildFile; productRef = 027E2B4427850E2E00FCEDB2 /* VideoPlayer */; };
027E2B492789487000FCEDB2 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 027E2B482789486F00FCEDB2 /* SettingsView.swift */; };
02D21B1E288279C800239990 /* TagModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02D21B1D288279C800239990 /* TagModel.swift */; };
02D21B2028827AA700239990 /* TagManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02D21B1F28827AA700239990 /* TagManager.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -60,6 +62,8 @@
027E2B332784DFA200FCEDB2 /* libswift_Concurrency.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libswift_Concurrency.tbd; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/lib/swift/libswift_Concurrency.tbd; sourceTree = DEVELOPER_DIR; };
027E2B352784E19300FCEDB2 /* Tanuki-s-Stash-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = "Tanuki-s-Stash-Info.plist"; sourceTree = SOURCE_ROOT; };
027E2B482789486F00FCEDB2 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
02D21B1D288279C800239990 /* TagModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagModel.swift; sourceTree = "<group>"; };
02D21B1F28827AA700239990 /* TagManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagManager.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -126,6 +130,8 @@
027E2B2F2784CF9700FCEDB2 /* PostView.swift */,
027E2B482789486F00FCEDB2 /* SettingsView.swift */,
026C76DD27D6CC3D00BBE65A /* SearchView.swift */,
02D21B1D288279C800239990 /* TagModel.swift */,
02D21B1F28827AA700239990 /* TagManager.swift */,
);
path = "Tanuki's Stash";
sourceTree = "<group>";
Expand Down Expand Up @@ -305,6 +311,8 @@
buildActionMask = 2147483647;
files = (
0202880D2783C74500341901 /* ContentView.swift in Sources */,
02D21B1E288279C800239990 /* TagModel.swift in Sources */,
02D21B2028827AA700239990 /* TagManager.swift in Sources */,
020288352783E96600341901 /* PostModel.swift in Sources */,
027E2B492789487000FCEDB2 /* SettingsView.swift in Sources */,
027E2B302784CF9700FCEDB2 /* PostView.swift in Sources */,
Expand Down Expand Up @@ -476,7 +484,6 @@
"ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "Tanuki-s-Stash-Info.plist";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = "We want to save the loaded photo";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Allows the saving of posts to the device";
Expand Down Expand Up @@ -514,7 +521,6 @@
"ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "Tanuki-s-Stash-Info.plist";
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = "We want to save the loaded photo";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Allows the saving of posts to the device";
Expand Down
33 changes: 28 additions & 5 deletions Tanuki's Stash/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// ContentView.swift
// Tanuki's Stash
//
// Created by Jay Poffinbarger on 1/3/22.
// Created by Jemma Poffinbarger on 1/3/22.
//

import SwiftUI

struct SearchView: View {
@State var posts = [PostContent]();
@State var searchSuggestions = [String]();
@State var search: String;
@State var page = 1;
@State var source = defaults.string(forKey: "api_source") ?? "e926.net";
Expand Down Expand Up @@ -89,8 +90,6 @@ struct SearchView: View {
Task.init {
if(checkRefresh(post.id)) {
page += 1;
print("ayo")
print(page)
await fetchMoreRecentPosts(page, limit, search);
}
}
Expand All @@ -115,13 +114,27 @@ struct SearchView: View {
SettingsView()
})
.searchable(text: $search, placement: .navigationBarDrawer(displayMode: .always), prompt: "Search for tags") {
Text("Gay").searchCompletion("gay")
Text("Lesbian").searchCompletion("Lesbian")

ForEach(searchSuggestions, id: \.self) { tag in
Button(action: {
updateSearch(tag);
}) {
Text(tag);
}
}
}
.onChange(of: search) { newQuery in
Task.init { if(search.count >= 3) {
Task.init {
searchSuggestions = await createTagList(search);
}
} }
}
.onSubmit(of: .search) {
Task.init {
page = 1;
await fetchRecentPosts(page, limit, search)
searchSuggestions.removeAll();
}
}
}
Expand All @@ -137,6 +150,16 @@ struct SearchView: View {
}
}

func updateSearch(_ tag: String) {
if(search.contains(" ")) {
let index = search.lastIndex(of: " ");
if(index != nil) {
search = String(search[...index!] + " " + tag);
}
}
else { search = tag; }
}

func fetchRecentPosts(_ page: Int, _ limit: Int, _ tags: String) async {
do {
let encoded = tags.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
Expand Down
53 changes: 53 additions & 0 deletions Tanuki's Stash/TagManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// TagManager.swift
// Tanuki's Stash
//
// Created by Jemma Poffinbarger on 7/15/22.
//

import Foundation
import SwiftUI

var source = defaults.string(forKey: "api_source") ?? "e926.net";
var tagList = [String]();

func fetchTags(_ text: String) async {
do {
let userAgent = "Tanukis%20Stash/1.0%20(by%20JayDaBirb%20on%20e621)"
let url = URL(string: "https://\(source)/tags/autocomplete.json?search%5Bname_matches%5D=\(text)&expiry=7&_client=\(userAgent)")!
let (data, _) = try await URLSession.shared.data(from: url)
//print(url);
let parsedData = try JSONDecoder().decode([TagContent].self, from: data)
let tags = parsedData;
//print(tags);
await processTags(tags);
} catch {
print(error);
}
}

func processTags(_ tags: [TagContent]) async {
for tag in tags {
tagList.append(tag.name);
}
}

func parseSearch(_ searchText: String) -> String {
if(searchText.contains(" ")) {
let index = searchText.lastIndex(of: " ");
if(index != nil) {
return String(searchText[index!...]).trimmingCharacters(in: .whitespacesAndNewlines);
}
else {return "";}
}
else { return searchText; }
}

func createTagList(_ search: String) async -> [String] {
tagList.removeAll();
let newSearchText = parseSearch(search);
if(newSearchText.count >= 3) {
await fetchTags(newSearchText);
}
return tagList;
}
16 changes: 16 additions & 0 deletions Tanuki's Stash/TagModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// TagModel.swift
// Tanuki's Stash
//
// Created by Jemma Poffinbarger on 7/15/22.
//

import SwiftUI

struct TagContent: Decodable {
let id: Int;
let name: String;
let post_count: Int;
let category: Int;
let antecedent_name: String?;
}

0 comments on commit 35d68e8

Please sign in to comment.