MainViewModel documentation and image caching improvements
This commit is contained in:
@@ -63,6 +63,6 @@ struct RecipeDetail: Codable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct RecipeImage {
|
struct RecipeImage {
|
||||||
let thumb: UIImage
|
var thumb: UIImage?
|
||||||
let full: UIImage?
|
var full: UIImage?
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,13 +32,17 @@ class DataStore {
|
|||||||
return try await task.value
|
return try await task.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func save<D: Encodable>(data: D, toPath path: String) async throws {
|
func save<D: Encodable>(data: D, toPath path: String) async {
|
||||||
let task = Task {
|
let task = Task {
|
||||||
let data = try JSONEncoder().encode(data)
|
let data = try JSONEncoder().encode(data)
|
||||||
let outfile = try Self.fileURL(appending: path)
|
let outfile = try Self.fileURL(appending: path)
|
||||||
try data.write(to: outfile)
|
try data.write(to: outfile)
|
||||||
}
|
}
|
||||||
|
do {
|
||||||
_ = try await task.value
|
_ = try await task.value
|
||||||
|
} catch {
|
||||||
|
print("Could not save data (path: \(path)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearAll() {
|
func clearAll() {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ struct Nextcloud_Cookbook_iOS_ClientApp: App {
|
|||||||
@StateObject var userSettings = UserSettings()
|
@StateObject var userSettings = UserSettings()
|
||||||
var body: some Scene {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
MainView()
|
MainView(userSettings: userSettings)
|
||||||
.fullScreenCover(isPresented: $userSettings.onboarding) {
|
.fullScreenCover(isPresented: $userSettings.onboarding) {
|
||||||
OnboardingView(userSettings: userSettings)
|
OnboardingView(userSettings: userSettings)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,23 +17,45 @@ import UIKit
|
|||||||
let dataStore: DataStore
|
let dataStore: DataStore
|
||||||
let networkController: NetworkController
|
let networkController: NetworkController
|
||||||
|
|
||||||
|
/// The path of an image in storage
|
||||||
|
private var localImagePath: (Int, Bool) -> (String) = { recipeId, full in
|
||||||
|
return "image\(recipeId)_\(full ? "full" : "thumb")"
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The path of an image on the server
|
||||||
|
private var networkImagePath: (Int, Bool) -> (String) = { recipeId, full in
|
||||||
|
return "recipes/\(recipeId)/image?size=\(full ? "full" : "thumb")"
|
||||||
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
self.networkController = NetworkController()
|
self.networkController = NetworkController()
|
||||||
self.dataStore = DataStore()
|
self.dataStore = DataStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Try to load the category list from store or the server.
|
||||||
|
/// - Parameters
|
||||||
|
/// - needsUpdate: If true, the recipe will be loaded from the server directly, otherwise it will be loaded from store first.
|
||||||
func loadCategoryList(needsUpdate: Bool = false) async {
|
func loadCategoryList(needsUpdate: Bool = false) async {
|
||||||
if let categoryList: [Category] = await load(localPath: "categories.data", networkPath: "categories", needsUpdate: needsUpdate) {
|
if let categoryList: [Category] = await load(localPath: "categories.data", networkPath: "categories", needsUpdate: needsUpdate) {
|
||||||
self.categories = categoryList
|
self.categories = categoryList
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Try to load the recipe list from store or the server.
|
||||||
|
/// - Parameters
|
||||||
|
/// - categoryName: The name of the category containing the requested list of recipes.
|
||||||
|
/// - needsUpdate: If true, the recipe will be loaded from the server directly, otherwise it will be loaded from store first.
|
||||||
func loadRecipeList(categoryName: String, needsUpdate: Bool = false) async {
|
func loadRecipeList(categoryName: String, needsUpdate: Bool = false) async {
|
||||||
if let recipeList: [Recipe] = await load(localPath: "category_\(categoryName).data", networkPath: "category/\(categoryName)", needsUpdate: needsUpdate) {
|
if let recipeList: [Recipe] = await load(localPath: "category_\(categoryName).data", networkPath: "category/\(categoryName)", needsUpdate: needsUpdate) {
|
||||||
recipes[categoryName] = recipeList
|
recipes[categoryName] = recipeList
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Try to load the recipe details from cache. If not found, try to load from store or the server.
|
||||||
|
/// - Parameters
|
||||||
|
/// - recipeId: The id of the recipe.
|
||||||
|
/// - needsUpdate: If true, the recipe will be loaded from the server directly, otherwise it will be loaded from cache/store first.
|
||||||
|
/// - Returns: RecipeDetail struct. If not found locally, and unable to load from server, a RecipeDetail struct containing an error message.
|
||||||
func loadRecipeDetail(recipeId: Int, needsUpdate: Bool = false) async -> RecipeDetail {
|
func loadRecipeDetail(recipeId: Int, needsUpdate: Bool = false) async -> RecipeDetail {
|
||||||
if !needsUpdate {
|
if !needsUpdate {
|
||||||
if let recipeDetail = recipeDetails[recipeId] {
|
if let recipeDetail = recipeDetails[recipeId] {
|
||||||
@@ -47,50 +69,40 @@ import UIKit
|
|||||||
return RecipeDetail.error()
|
return RecipeDetail.error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Try to load the recipe image from cache. If not found, try to load from store or the server.
|
||||||
|
/// - Parameters
|
||||||
|
/// - recipeId: The id of a recipe.
|
||||||
|
/// - full: If true, load the full resolution image. Otherwise, load a thumbnail-sized image.
|
||||||
|
/// - needsUpdate: Determines wether the image should be loaded directly from the server, or if it should be loaded from cache/store first.
|
||||||
|
/// - Returns: The image if found locally or on the server, otherwise nil.
|
||||||
func loadImage(recipeId: Int, full: Bool, needsUpdate: Bool = false) async -> UIImage? {
|
func loadImage(recipeId: Int, full: Bool, needsUpdate: Bool = false) async -> UIImage? {
|
||||||
print("loadImage(recipeId: \(recipeId), full: \(full)")
|
print("loadImage(recipeId: \(recipeId), full: \(full))")
|
||||||
|
// If the image needs an update, request it from the server and overwrite the stored image
|
||||||
// Check if image is in image cache
|
if needsUpdate {
|
||||||
if !needsUpdate, let recipeImage = imageCache[recipeId] {
|
if let data = await imageDataFromServer(recipeId: recipeId, full: full) {
|
||||||
if full {
|
guard let image = UIImage(data: data) else { return nil }
|
||||||
if let fullImage = recipeImage.full {
|
await dataStore.save(data: data.base64EncodedString(), toPath: localImagePath(recipeId, full))
|
||||||
return recipeImage.full
|
imageToCache(image: image, recipeId: recipeId, full: full)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return recipeImage.thumb
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If image is not in image cache, request from local storage
|
|
||||||
do {
|
|
||||||
let localPath = "image\(recipeId)_\(full ? "full" : "thumb")"
|
|
||||||
if !needsUpdate, let data: String = try await dataStore.load(fromPath: localPath) {
|
|
||||||
print("Image data found locally. Decoding ...")
|
|
||||||
guard let dataDecoded = Data(base64Encoded: data) else { return nil }
|
|
||||||
print("Data to UIImage ...")
|
|
||||||
let image = UIImage(data: dataDecoded)
|
|
||||||
print("Done.")
|
|
||||||
return image
|
|
||||||
} else {
|
|
||||||
// If image is not in local storage, request from server
|
|
||||||
let networkPath = "recipes/\(recipeId)/image?size=full"
|
|
||||||
let request = RequestWrapper(method: .GET, path: networkPath, accept: .IMAGE)
|
|
||||||
let (data, error): (Data?, Error?) = try await networkController.sendHTTPRequest(path: request.path, request)
|
|
||||||
guard let data = data else {
|
|
||||||
print("Error receiving or decoding data.")
|
|
||||||
print("Error Message: \n", error)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
let image = UIImage(data: data)
|
|
||||||
if image != nil {
|
|
||||||
print("Saving image loaclly ...")
|
|
||||||
try await dataStore.save(data: data.base64EncodedString(), toPath: localPath)
|
|
||||||
}
|
|
||||||
print("Done.")
|
|
||||||
return image
|
return image
|
||||||
}
|
}
|
||||||
}catch {
|
}
|
||||||
print("An unknown error occurred.")
|
// Try to load image from cache
|
||||||
|
print("Attempting to load image from local storage ...")
|
||||||
|
if let image = imageFromCache(recipeId: recipeId, full: full) {
|
||||||
|
return image
|
||||||
|
}
|
||||||
|
// Try to load from store
|
||||||
|
print("Attempting to load image from server ...")
|
||||||
|
if let image = await imageFromStore(recipeId: recipeId, full: full) {
|
||||||
|
return image
|
||||||
|
}
|
||||||
|
// Try to load from the server. Store if successfull.
|
||||||
|
if let data = await imageDataFromServer(recipeId: recipeId, full: full) {
|
||||||
|
guard let image = UIImage(data: data) else { return nil }
|
||||||
|
await dataStore.save(data: data.base64EncodedString(), toPath: localImagePath(recipeId, full))
|
||||||
|
imageToCache(image: image, recipeId: recipeId, full: full)
|
||||||
|
return image
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -108,7 +120,7 @@ extension MainViewModel {
|
|||||||
} else {
|
} else {
|
||||||
let request = RequestWrapper(method: .GET, path: networkPath)
|
let request = RequestWrapper(method: .GET, path: networkPath)
|
||||||
let (data, error): (D?, Error?) = await networkController.sendDataRequest(request)
|
let (data, error): (D?, Error?) = await networkController.sendDataRequest(request)
|
||||||
print(error)
|
print(error as Any)
|
||||||
if let data = data {
|
if let data = data {
|
||||||
try await dataStore.save(data: data, toPath: localPath)
|
try await dataStore.save(data: data, toPath: localPath)
|
||||||
}
|
}
|
||||||
@@ -119,6 +131,59 @@ extension MainViewModel {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func imageToCache(image: UIImage, recipeId: Int, full: Bool) {
|
||||||
|
if imageCache[recipeId] == nil {
|
||||||
|
imageCache[recipeId] = RecipeImage()
|
||||||
|
}
|
||||||
|
if full {
|
||||||
|
imageCache[recipeId]!.full = image
|
||||||
|
} else {
|
||||||
|
imageCache[recipeId]!.thumb = image
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func imageFromCache(recipeId: Int, full: Bool) -> UIImage? {
|
||||||
|
if imageCache[recipeId] != nil {
|
||||||
|
if full {
|
||||||
|
return imageCache[recipeId]!.full
|
||||||
|
} else {
|
||||||
|
return imageCache[recipeId]!.thumb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
private func imageFromStore(recipeId: Int, full: Bool) async -> UIImage? {
|
||||||
|
do {
|
||||||
|
let localPath = localImagePath(recipeId, full)
|
||||||
|
if let data: String = try await dataStore.load(fromPath: localPath) {
|
||||||
|
guard let dataDecoded = Data(base64Encoded: data) else { return nil }
|
||||||
|
let image = UIImage(data: dataDecoded)
|
||||||
|
return image
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print("Could not find image in local storage.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
private func imageDataFromServer(recipeId: Int, full: Bool) async -> Data? {
|
||||||
|
do {
|
||||||
|
let networkPath = networkImagePath(recipeId, full)
|
||||||
|
let request = RequestWrapper(method: .GET, path: networkPath, accept: .IMAGE)
|
||||||
|
let (data, _): (Data?, Error?) = try await networkController.sendHTTPRequest(path: request.path, request)
|
||||||
|
guard let data = data else {
|
||||||
|
print("Error receiving or decoding data.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
} catch {
|
||||||
|
print("Could not load image from server.")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import SwiftUI
|
|||||||
|
|
||||||
struct MainView: View {
|
struct MainView: View {
|
||||||
@StateObject var viewModel = MainViewModel()
|
@StateObject var viewModel = MainViewModel()
|
||||||
|
@StateObject var userSettings: UserSettings
|
||||||
var columns: [GridItem] = [GridItem(.adaptive(minimum: 150), spacing: 0)]
|
var columns: [GridItem] = [GridItem(.adaptive(minimum: 150), spacing: 0)]
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
@@ -29,7 +30,7 @@ struct MainView: View {
|
|||||||
}
|
}
|
||||||
.navigationTitle("CookBook")
|
.navigationTitle("CookBook")
|
||||||
.toolbar {
|
.toolbar {
|
||||||
NavigationLink( destination: SettingsView()) {
|
NavigationLink( destination: SettingsView(userSettings: userSettings)) {
|
||||||
Image(systemName: "gear")
|
Image(systemName: "gear")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,7 +46,7 @@ struct MainView: View {
|
|||||||
|
|
||||||
struct MainView_Previews: PreviewProvider {
|
struct MainView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
MainView()
|
MainView(userSettings: UserSettings())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -146,7 +146,8 @@ struct LoginTextField: View {
|
|||||||
var body: some View {
|
var body: some View {
|
||||||
TextField(example, text: $text)
|
TextField(example, text: $text)
|
||||||
.textFieldStyle(.plain)
|
.textFieldStyle(.plain)
|
||||||
.textCase(.lowercase)
|
.autocorrectionDisabled()
|
||||||
|
.textInputAutocapitalization(.never)
|
||||||
.foregroundColor(.white)
|
.foregroundColor(.white)
|
||||||
.accentColor(.white)
|
.accentColor(.white)
|
||||||
.padding()
|
.padding()
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ struct RecipeCardView: View {
|
|||||||
HStack {
|
HStack {
|
||||||
Image(uiImage: recipeThumb ?? UIImage(named: "CookBook")!)
|
Image(uiImage: recipeThumb ?? UIImage(named: "CookBook")!)
|
||||||
.resizable()
|
.resizable()
|
||||||
.frame(maxWidth: 80, maxHeight: 80)
|
.aspectRatio(contentMode: .fill)
|
||||||
|
.frame(width: 80, height: 80)
|
||||||
|
.clipped()
|
||||||
Text(recipe.name)
|
Text(recipe.name)
|
||||||
.font(.headline)
|
.font(.headline)
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ struct RecipeDetailView: View {
|
|||||||
.frame(height: 300)
|
.frame(height: 300)
|
||||||
.clipped()
|
.clipped()
|
||||||
} else {
|
} else {
|
||||||
Color.blue
|
Color("ncblue")
|
||||||
.frame(height: 300)
|
.frame(height: 150)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let recipeDetail = recipeDetail {
|
if let recipeDetail = recipeDetail {
|
||||||
@@ -80,8 +80,7 @@ struct RecipeYieldSection: View {
|
|||||||
Text("Servings: \(recipeDetail.recipeYield)")
|
Text("Servings: \(recipeDetail.recipeYield)")
|
||||||
Spacer()
|
Spacer()
|
||||||
}.padding()
|
}.padding()
|
||||||
.background(Color("accent"))
|
|
||||||
.clipShape(RoundedRectangle(cornerRadius: 10))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,14 @@ import Foundation
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct SettingsView: View {
|
struct SettingsView: View {
|
||||||
@StateObject var userSettings = UserSettings()
|
@ObservedObject var userSettings: UserSettings
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
ScrollView(showsIndicators: false) {
|
List {
|
||||||
LazyVStack {
|
SettingsSection(title: "Language", description: "Language settings coming soon.")
|
||||||
SettingsSection(headline: "Language", description: "Language settings coming soon.")
|
SettingsSection(title: "Accent Color", description: "The accent color setting will be released in a future update.")
|
||||||
SettingsSection(headline: "Accent Color", description: "The accent color setting will be released in a future update.")
|
SettingsSection(title: "Log out", description: "Log out of your Nextcloud account in this app. Your recipes will be removed from local storage.")
|
||||||
|
{
|
||||||
Button("Log out") {
|
Button("Log out") {
|
||||||
print("Log out.")
|
print("Log out.")
|
||||||
userSettings.serverAddress = ""
|
userSettings.serverAddress = ""
|
||||||
@@ -26,7 +27,10 @@ struct SettingsView: View {
|
|||||||
.buttonStyle(.borderedProminent)
|
.buttonStyle(.borderedProminent)
|
||||||
.accentColor(.red)
|
.accentColor(.red)
|
||||||
.padding()
|
.padding()
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsSection(title: "Clear local data", description: "Your recipes will be removed from local storage.")
|
||||||
|
{
|
||||||
Button("Clear Cache") {
|
Button("Clear Cache") {
|
||||||
print("Clear cache.")
|
print("Clear cache.")
|
||||||
|
|
||||||
@@ -35,21 +39,40 @@ struct SettingsView: View {
|
|||||||
.accentColor(.red)
|
.accentColor(.red)
|
||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
|
|
||||||
}.navigationTitle("Settings")
|
}.navigationTitle("Settings")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct SettingsSection: View {
|
struct SettingsSection<Content: View>: View {
|
||||||
@State var headline: String
|
let title: String
|
||||||
@State var description: String
|
let description: String
|
||||||
|
@ViewBuilder let content: () -> Content
|
||||||
|
|
||||||
|
init(title: String, description: String, content: @escaping () -> Content) {
|
||||||
|
self.title = title
|
||||||
|
self.description = description
|
||||||
|
self.content = content
|
||||||
|
}
|
||||||
|
|
||||||
|
init(title: String, description: String) where Content == EmptyView {
|
||||||
|
self.title = title
|
||||||
|
self.description = description
|
||||||
|
self.content = { EmptyView() }
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
|
HStack {
|
||||||
VStack(alignment: .leading) {
|
VStack(alignment: .leading) {
|
||||||
Text(headline)
|
Text(title)
|
||||||
.font(.headline)
|
.font(.headline)
|
||||||
Text(description)
|
Text(description)
|
||||||
|
.font(.caption)
|
||||||
Divider()
|
|
||||||
}.padding()
|
}.padding()
|
||||||
|
Spacer()
|
||||||
|
content()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user