-
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.
Implemented tag suggestions from the api
- Loading branch information
1 parent
cdecde7
commit 35d68e8
Showing
4 changed files
with
105 additions
and
7 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
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
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,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; | ||
} |
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,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?; | ||
} |