-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4ef7a44
commit 19d7b6b
Showing
14 changed files
with
349 additions
and
16 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
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,23 @@ | ||
// | ||
// Int.swift | ||
// Mlem | ||
// | ||
// Created by Jake Shirley on 7/5/23. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Int { | ||
var roundedWithAbbreviations: String { | ||
let number = Double(self) | ||
let thousand = number / 1000 | ||
let million = number / 1000000 | ||
if million >= 1.0 { | ||
return "\(round(million*10)/10)m" | ||
} else if thousand >= 1.0 { | ||
return "\(round(thousand*10)/10)k" | ||
} else { | ||
return self.description | ||
} | ||
} | ||
} |
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,77 @@ | ||
// | ||
// RecentSearchesTracker.swift | ||
// Mlem | ||
// | ||
// Created by Jake Shirley on 7/6/23. | ||
// | ||
|
||
import Foundation | ||
|
||
class RecentSearchesTracker: ObservableObject { | ||
@Published var recentSearches: [String] = .init() | ||
|
||
init() { | ||
loadFromDisk() | ||
} | ||
|
||
func loadFromDisk() { | ||
if FileManager.default.fileExists(atPath: AppConstants.recentSearchesFilePath.path) { | ||
print("Favorite communities file exists, will attempt to load favorite communities") | ||
do { | ||
recentSearches = try decodeFromFile( | ||
fromURL: AppConstants.recentSearchesFilePath, | ||
whatToDecode: .recentSearches | ||
) as? [String] ?? [] | ||
} catch let decodingError { | ||
print("Failed while decoding recent searches, erasing file: \(decodingError)") | ||
} | ||
} else { | ||
print("Recent searches file does not exist, will try to create it") | ||
|
||
do { | ||
try createEmptyFile(at: AppConstants.recentSearchesFilePath) | ||
} catch let emptyFileCreationError { | ||
print("Failed while creating empty file: \(emptyFileCreationError)") | ||
} | ||
} | ||
} | ||
|
||
// Lazy save in the background | ||
func saveToDisk() { | ||
Task(priority: .background) { [recentSearches] in | ||
do { | ||
let encodedSearches: Data = try encodeForSaving(object: recentSearches) | ||
|
||
do { | ||
try writeDataToFile(data: encodedSearches, fileURL: AppConstants.recentSearchesFilePath) | ||
} catch let writingError { | ||
print("Failed while saving data to file: \(writingError)") | ||
clearRecentSearches() | ||
} | ||
} catch let encodingError { | ||
print("Failed while encoding recent searches to data: \(encodingError)") | ||
} | ||
} | ||
} | ||
|
||
func addRecentSearch(_ searchText: String) { | ||
// don't insert duplicates | ||
guard !recentSearches.contains(searchText) else { | ||
return | ||
} | ||
|
||
recentSearches.insert(searchText, at: 0) | ||
|
||
// Limit results to 5 | ||
while recentSearches.count > 5 { | ||
recentSearches.remove(at: 5) | ||
} | ||
|
||
saveToDisk() | ||
} | ||
|
||
func clearRecentSearches() { | ||
recentSearches = [] | ||
saveToDisk() | ||
} | ||
} |
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
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
Oops, something went wrong.