Randomizes featured source + app order at app launch

This commit is contained in:
Riley Testut
2023-12-08 14:32:57 -06:00
committed by Magesh K
parent 36743c0cf4
commit 9ea94912d4
6 changed files with 84 additions and 6 deletions

View File

@@ -178,6 +178,49 @@ public extension DatabaseManager
}
}
}
func updateFeaturedSortIDs() async
{
let context = DatabaseManager.shared.persistentContainer.newBackgroundContext()
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy // DON'T use our custom merge policy, because that one ignores changes to featuredSortID.
await context.performAsync {
do
{
// Randomize source order
let fetchRequest = Source.fetchRequest()
let sources = try context.fetch(fetchRequest)
for source in sources
{
source.featuredSortID = UUID().uuidString
}
try context.save()
}
catch
{
Logger.main.error("Failed to update source order. \(error.localizedDescription, privacy: .public)")
}
do
{
// Randomize app order
let fetchRequest = StoreApp.fetchRequest()
let apps = try context.fetch(fetchRequest)
for app in apps
{
app.featuredSortID = UUID().uuidString
}
try context.save()
}
catch
{
Logger.main.error("Failed to update app order. \(error.localizedDescription, privacy: .public)")
}
}
}
}
public extension DatabaseManager
@@ -372,7 +415,11 @@ private extension DatabaseManager
do
{
try context.save()
completionHandler(.success(()))
Task(priority: .high) {
await self.updateFeaturedSortIDs()
completionHandler(.success(()))
}
}
catch
{