Compare commits
4 Commits
7c824b492e
...
1.10.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
078c01808d | ||
|
|
31dd6c6926 | ||
|
|
d7272026bb | ||
|
|
6cecdcf1fd |
114
CLAUDE.md
114
CLAUDE.md
@@ -1,114 +0,0 @@
|
|||||||
# CLAUDE.md
|
|
||||||
|
|
||||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
||||||
|
|
||||||
## Project Overview
|
|
||||||
|
|
||||||
Nextcloud Cookbook iOS Client — a native iOS/iPadOS/macOS (Mac Catalyst) client for the [Nextcloud Cookbook](https://github.com/nextcloud/cookbook) server application. Built entirely in Swift and SwiftUI. Licensed under GPLv3.
|
|
||||||
|
|
||||||
**This is not a standalone app.** It requires a Nextcloud server with the Cookbook app installed.
|
|
||||||
|
|
||||||
## Build & Run
|
|
||||||
|
|
||||||
This is an Xcode project (no workspace, no CocoaPods). Open `Nextcloud Cookbook iOS Client.xcodeproj` in Xcode.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Build from command line
|
|
||||||
xcodebuild -project "Nextcloud Cookbook iOS Client.xcodeproj" \
|
|
||||||
-scheme "Nextcloud Cookbook iOS Client" \
|
|
||||||
-destination 'platform=iOS Simulator,name=iPhone 16' \
|
|
||||||
build
|
|
||||||
|
|
||||||
# Run tests (note: tests are currently boilerplate stubs with no real coverage)
|
|
||||||
xcodebuild -project "Nextcloud Cookbook iOS Client.xcodeproj" \
|
|
||||||
-scheme "Nextcloud Cookbook iOS Client" \
|
|
||||||
-destination 'platform=iOS Simulator,name=iPhone 16' \
|
|
||||||
test
|
|
||||||
```
|
|
||||||
|
|
||||||
- **Deployment target**: iOS 16.4
|
|
||||||
- **Swift version**: 5.0
|
|
||||||
- **Targets**: iPhone and iPad, Mac Catalyst enabled
|
|
||||||
|
|
||||||
## Dependencies (SPM)
|
|
||||||
|
|
||||||
Only two third-party packages, managed via Swift Package Manager integrated in the Xcode project:
|
|
||||||
|
|
||||||
| Package | Purpose |
|
|
||||||
|---------|---------|
|
|
||||||
| [SwiftSoup](https://github.com/scinfu/SwiftSoup.git) (2.6.1) | HTML parsing for client-side recipe scraping |
|
|
||||||
| [TPPDF](https://github.com/techprimate/TPPDF.git) (2.4.1) | PDF generation for recipe export |
|
|
||||||
|
|
||||||
## Architecture
|
|
||||||
|
|
||||||
### Central State Pattern
|
|
||||||
|
|
||||||
The app uses a **centralized `AppState` ObservableObject** (`AppState.swift`) as the primary ViewModel, injected via `.environmentObject()` into the SwiftUI view hierarchy. `AppState` owns:
|
|
||||||
- All data (categories, recipes, recipe details, images, timers)
|
|
||||||
- All CRUD operations against the Cookbook API
|
|
||||||
- A `FetchMode` enum (`preferLocal`, `preferServer`, `onlyLocal`, `onlyServer`) that governs the data-fetching strategy (server-first vs local-first)
|
|
||||||
- Local persistence via `DataStore`
|
|
||||||
|
|
||||||
Additional ViewModels exist as nested classes within their views (`RecipeTabView.ViewModel`, `SearchTabView.ViewModel`) or as standalone classes (`RecipeEditViewModel`, `GroceryList`).
|
|
||||||
|
|
||||||
### Data Flow
|
|
||||||
|
|
||||||
```
|
|
||||||
SwiftUI Views
|
|
||||||
├── @EnvironmentObject appState: AppState
|
|
||||||
├── @EnvironmentObject groceryList: GroceryList
|
|
||||||
└── Per-view @StateObject ViewModels
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
AppState
|
|
||||||
├── cookbookApi (CookbookApiV1 — static methods) → ApiRequest → URLSession
|
|
||||||
├── DataStore (file-based JSON persistence in Documents directory)
|
|
||||||
└── UserSettings.shared (UserDefaults singleton)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Network Layer
|
|
||||||
|
|
||||||
- `CookbookApi` protocol defines all endpoints; `CookbookApiV1` is the concrete implementation with all `static` methods.
|
|
||||||
- The global `cookbookApi` constant (`CookbookApi.swift:14`) resolves the API version at launch.
|
|
||||||
- `ApiRequest` is a generic HTTP request builder using `URLSession.shared.data(for:)` with HTTP Basic Auth.
|
|
||||||
- `NextcloudApi` handles Nextcloud-specific auth (Login Flow v2 and token-based login).
|
|
||||||
|
|
||||||
### Persistence
|
|
||||||
|
|
||||||
- **`DataStore`**: File-based persistence using `JSONEncoder`/`JSONDecoder` writing to the app's Documents directory. No Core Data or SQLite.
|
|
||||||
- **`UserSettings`**: Singleton wrapping `UserDefaults` for all user preferences and credentials.
|
|
||||||
- **Image caching**: Two-tier — in-memory dictionary in `AppState` + on-disk base64-encoded PNG files via `DataStore`.
|
|
||||||
|
|
||||||
### Key Source Directories
|
|
||||||
|
|
||||||
```
|
|
||||||
Nextcloud Cookbook iOS Client/
|
|
||||||
├── Data/ # Models (Category, Recipe, RecipeDetail, Nutrition) + DataStore + UserSettings
|
|
||||||
├── Models/ # RecipeEditViewModel
|
|
||||||
├── Network/ # ApiRequest, NetworkError, CookbookApi protocol + V1, NextcloudApi
|
|
||||||
├── Views/
|
|
||||||
│ ├── Tabs/ # Main tab views (RecipeTab, SearchTab, GroceryListTab)
|
|
||||||
│ ├── Recipes/ # Recipe detail, list, card, share, timer views
|
|
||||||
│ ├── RecipeViewSections/ # Decomposed recipe detail sections (ingredients, instructions, etc.)
|
|
||||||
│ ├── Onboarding/ # Login flows (V2LoginView, TokenLoginView)
|
|
||||||
│ └── ReusableViews/
|
|
||||||
├── Extensions/ # Color, Date, JSONCoder, Logger extensions
|
|
||||||
├── Util/ # Alerts, DurationComponents (ISO 8601 PT parser), JsonAny, NumberFormatter
|
|
||||||
├── RecipeExport/ # PDF, text, JSON export via RecipeExporter
|
|
||||||
└── RecipeImport/ # HTML scraping via SwiftSoup (schema.org ld+json Recipe data)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Localization
|
|
||||||
|
|
||||||
Four languages supported via `Localizable.xcstrings`: English, German, Spanish, French. Spanish and French are mostly machine-translated.
|
|
||||||
|
|
||||||
## Notable Design Decisions
|
|
||||||
|
|
||||||
- The `cookbookApi` global is resolved once at launch based on `UserSettings.shared.cookbookApiVersion` and uses static protocol methods, which makes dependency injection and unit testing difficult.
|
|
||||||
- Server credentials (username, token, authString) are stored in `UserDefaults` via `UserSettings`, not in Keychain.
|
|
||||||
- No `.gitignore` file exists in the repository.
|
|
||||||
- No CI/CD, no linting tools, and no meaningful test coverage.
|
|
||||||
|
|
||||||
## Workflow
|
|
||||||
|
|
||||||
- Do not run `xcodebuild` directly. Ask the user to build manually in Xcode and report the results back.
|
|
||||||
@@ -26,10 +26,13 @@
|
|||||||
A70171CD2AB501B100064C43 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171CC2AB501B100064C43 /* SettingsView.swift */; };
|
A70171CD2AB501B100064C43 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171CC2AB501B100064C43 /* SettingsView.swift */; };
|
||||||
A703226A2ABAF49800D7C4ED /* JSONCoderExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70322692ABAF49800D7C4ED /* JSONCoderExtension.swift */; };
|
A703226A2ABAF49800D7C4ED /* JSONCoderExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70322692ABAF49800D7C4ED /* JSONCoderExtension.swift */; };
|
||||||
A703226F2ABB1DD700D7C4ED /* ColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A703226E2ABB1DD700D7C4ED /* ColorExtension.swift */; };
|
A703226F2ABB1DD700D7C4ED /* ColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A703226E2ABB1DD700D7C4ED /* ColorExtension.swift */; };
|
||||||
|
A74D33BE2AF82AAE00D06555 /* SwiftSoup in Frameworks */ = {isa = PBXBuildFile; productRef = A74D33BD2AF82AAE00D06555 /* SwiftSoup */; };
|
||||||
|
A74D33C32AFCD1C300D06555 /* RecipeScraper.swift in Sources */ = {isa = PBXBuildFile; fileRef = A74D33C22AFCD1C300D06555 /* RecipeScraper.swift */; };
|
||||||
A76B8A6F2ADFFA8800096CEC /* SupportedLanguage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A76B8A6E2ADFFA8800096CEC /* SupportedLanguage.swift */; };
|
A76B8A6F2ADFFA8800096CEC /* SupportedLanguage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A76B8A6E2ADFFA8800096CEC /* SupportedLanguage.swift */; };
|
||||||
A76B8A712AE002AE00096CEC /* Alerts.swift in Sources */ = {isa = PBXBuildFile; fileRef = A76B8A702AE002AE00096CEC /* Alerts.swift */; };
|
A76B8A712AE002AE00096CEC /* Alerts.swift in Sources */ = {isa = PBXBuildFile; fileRef = A76B8A702AE002AE00096CEC /* Alerts.swift */; };
|
||||||
A787B0782B2B1E6400C2DF1B /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A787B0772B2B1E6400C2DF1B /* DateExtension.swift */; };
|
A787B0782B2B1E6400C2DF1B /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A787B0772B2B1E6400C2DF1B /* DateExtension.swift */; };
|
||||||
A79AA8E02AFF80E3007D25F2 /* DurationComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8DF2AFF80E3007D25F2 /* DurationComponents.swift */; };
|
A79AA8E02AFF80E3007D25F2 /* DurationComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8DF2AFF80E3007D25F2 /* DurationComponents.swift */; };
|
||||||
|
A79AA8E22AFF8C14007D25F2 /* RecipeEditViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8E12AFF8C14007D25F2 /* RecipeEditViewModel.swift */; };
|
||||||
A79AA8E42B02A962007D25F2 /* CookbookApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8E32B02A961007D25F2 /* CookbookApi.swift */; };
|
A79AA8E42B02A962007D25F2 /* CookbookApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8E32B02A961007D25F2 /* CookbookApi.swift */; };
|
||||||
A79AA8E62B02C3CB007D25F2 /* LoggerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8E52B02C3CB007D25F2 /* LoggerExtension.swift */; };
|
A79AA8E62B02C3CB007D25F2 /* LoggerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8E52B02C3CB007D25F2 /* LoggerExtension.swift */; };
|
||||||
A79AA8E92B062DD1007D25F2 /* CookbookApiV1.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8E82B062DD1007D25F2 /* CookbookApiV1.swift */; };
|
A79AA8E92B062DD1007D25F2 /* CookbookApiV1.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8E82B062DD1007D25F2 /* CookbookApiV1.swift */; };
|
||||||
@@ -37,7 +40,6 @@
|
|||||||
A79AA8ED2B063AD5007D25F2 /* NextcloudApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8EC2B063AD5007D25F2 /* NextcloudApi.swift */; };
|
A79AA8ED2B063AD5007D25F2 /* NextcloudApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79AA8EC2B063AD5007D25F2 /* NextcloudApi.swift */; };
|
||||||
A7AEAE642AD5521400135378 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = A7AEAE632AD5521400135378 /* Localizable.xcstrings */; };
|
A7AEAE642AD5521400135378 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = A7AEAE632AD5521400135378 /* Localizable.xcstrings */; };
|
||||||
A7CD3FD22B2C546A00D764AD /* CollapsibleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7CD3FD12B2C546A00D764AD /* CollapsibleView.swift */; };
|
A7CD3FD22B2C546A00D764AD /* CollapsibleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7CD3FD12B2C546A00D764AD /* CollapsibleView.swift */; };
|
||||||
DFCB4E9FD4E0884AF217E5C5 /* LiquidGlassModifiers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04B6ECAD063AEE501543FC76 /* LiquidGlassModifiers.swift */; };
|
|
||||||
A7F3F8E82ACBFC760076C227 /* RecipeKeywordSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7F3F8E72ACBFC760076C227 /* RecipeKeywordSection.swift */; };
|
A7F3F8E82ACBFC760076C227 /* RecipeKeywordSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7F3F8E72ACBFC760076C227 /* RecipeKeywordSection.swift */; };
|
||||||
A7FB0D7A2B25C66600A3469E /* OnboardingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7FB0D792B25C66600A3469E /* OnboardingView.swift */; };
|
A7FB0D7A2B25C66600A3469E /* OnboardingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7FB0D792B25C66600A3469E /* OnboardingView.swift */; };
|
||||||
A7FB0D7C2B25C68500A3469E /* TokenLoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7FB0D7B2B25C68500A3469E /* TokenLoginView.swift */; };
|
A7FB0D7C2B25C68500A3469E /* TokenLoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7FB0D7B2B25C68500A3469E /* TokenLoginView.swift */; };
|
||||||
@@ -107,10 +109,12 @@
|
|||||||
A70171CC2AB501B100064C43 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
|
A70171CC2AB501B100064C43 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
|
||||||
A70322692ABAF49800D7C4ED /* JSONCoderExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONCoderExtension.swift; sourceTree = "<group>"; };
|
A70322692ABAF49800D7C4ED /* JSONCoderExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONCoderExtension.swift; sourceTree = "<group>"; };
|
||||||
A703226E2ABB1DD700D7C4ED /* ColorExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExtension.swift; sourceTree = "<group>"; };
|
A703226E2ABB1DD700D7C4ED /* ColorExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExtension.swift; sourceTree = "<group>"; };
|
||||||
|
A74D33C22AFCD1C300D06555 /* RecipeScraper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeScraper.swift; sourceTree = "<group>"; };
|
||||||
A76B8A6E2ADFFA8800096CEC /* SupportedLanguage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportedLanguage.swift; sourceTree = "<group>"; };
|
A76B8A6E2ADFFA8800096CEC /* SupportedLanguage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportedLanguage.swift; sourceTree = "<group>"; };
|
||||||
A76B8A702AE002AE00096CEC /* Alerts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alerts.swift; sourceTree = "<group>"; };
|
A76B8A702AE002AE00096CEC /* Alerts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alerts.swift; sourceTree = "<group>"; };
|
||||||
A787B0772B2B1E6400C2DF1B /* DateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = "<group>"; };
|
A787B0772B2B1E6400C2DF1B /* DateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = "<group>"; };
|
||||||
A79AA8DF2AFF80E3007D25F2 /* DurationComponents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DurationComponents.swift; sourceTree = "<group>"; };
|
A79AA8DF2AFF80E3007D25F2 /* DurationComponents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DurationComponents.swift; sourceTree = "<group>"; };
|
||||||
|
A79AA8E12AFF8C14007D25F2 /* RecipeEditViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeEditViewModel.swift; sourceTree = "<group>"; };
|
||||||
A79AA8E32B02A961007D25F2 /* CookbookApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CookbookApi.swift; sourceTree = "<group>"; };
|
A79AA8E32B02A961007D25F2 /* CookbookApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CookbookApi.swift; sourceTree = "<group>"; };
|
||||||
A79AA8E52B02C3CB007D25F2 /* LoggerExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggerExtension.swift; sourceTree = "<group>"; };
|
A79AA8E52B02C3CB007D25F2 /* LoggerExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggerExtension.swift; sourceTree = "<group>"; };
|
||||||
A79AA8E82B062DD1007D25F2 /* CookbookApiV1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CookbookApiV1.swift; sourceTree = "<group>"; };
|
A79AA8E82B062DD1007D25F2 /* CookbookApiV1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CookbookApiV1.swift; sourceTree = "<group>"; };
|
||||||
@@ -118,7 +122,6 @@
|
|||||||
A79AA8EC2B063AD5007D25F2 /* NextcloudApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudApi.swift; sourceTree = "<group>"; };
|
A79AA8EC2B063AD5007D25F2 /* NextcloudApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudApi.swift; sourceTree = "<group>"; };
|
||||||
A7AEAE632AD5521400135378 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
|
A7AEAE632AD5521400135378 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
|
||||||
A7CD3FD12B2C546A00D764AD /* CollapsibleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollapsibleView.swift; sourceTree = "<group>"; };
|
A7CD3FD12B2C546A00D764AD /* CollapsibleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollapsibleView.swift; sourceTree = "<group>"; };
|
||||||
04B6ECAD063AEE501543FC76 /* LiquidGlassModifiers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiquidGlassModifiers.swift; sourceTree = "<group>"; };
|
|
||||||
A7F3F8E72ACBFC760076C227 /* RecipeKeywordSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeKeywordSection.swift; sourceTree = "<group>"; };
|
A7F3F8E72ACBFC760076C227 /* RecipeKeywordSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeKeywordSection.swift; sourceTree = "<group>"; };
|
||||||
A7FB0D792B25C66600A3469E /* OnboardingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingView.swift; sourceTree = "<group>"; };
|
A7FB0D792B25C66600A3469E /* OnboardingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingView.swift; sourceTree = "<group>"; };
|
||||||
A7FB0D7B2B25C68500A3469E /* TokenLoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenLoginView.swift; sourceTree = "<group>"; };
|
A7FB0D7B2B25C68500A3469E /* TokenLoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenLoginView.swift; sourceTree = "<group>"; };
|
||||||
@@ -152,6 +155,7 @@
|
|||||||
isa = PBXFrameworksBuildPhase;
|
isa = PBXFrameworksBuildPhase;
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
|
A74D33BE2AF82AAE00D06555 /* SwiftSoup in Frameworks */,
|
||||||
A9CA6CF62B4C63F200F78AB5 /* TPPDF in Frameworks */,
|
A9CA6CF62B4C63F200F78AB5 /* TPPDF in Frameworks */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
@@ -257,6 +261,7 @@
|
|||||||
A70171B72AB2445700064C43 /* Models */ = {
|
A70171B72AB2445700064C43 /* Models */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
A79AA8E12AFF8C14007D25F2 /* RecipeEditViewModel.swift */,
|
||||||
);
|
);
|
||||||
path = Models;
|
path = Models;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -307,6 +312,7 @@
|
|||||||
A781E75F2AF8228100452F6F /* RecipeImport */ = {
|
A781E75F2AF8228100452F6F /* RecipeImport */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
A74D33C22AFCD1C300D06555 /* RecipeScraper.swift */,
|
||||||
);
|
);
|
||||||
path = RecipeImport;
|
path = RecipeImport;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -395,7 +401,6 @@
|
|||||||
A9BBB38B2B8D3B0C002DA7FF /* ParallaxHeaderView.swift */,
|
A9BBB38B2B8D3B0C002DA7FF /* ParallaxHeaderView.swift */,
|
||||||
A7CD3FD12B2C546A00D764AD /* CollapsibleView.swift */,
|
A7CD3FD12B2C546A00D764AD /* CollapsibleView.swift */,
|
||||||
A9BBB38D2B8E44B3002DA7FF /* BottomClipper.swift */,
|
A9BBB38D2B8E44B3002DA7FF /* BottomClipper.swift */,
|
||||||
04B6ECAD063AEE501543FC76 /* LiquidGlassModifiers.swift */,
|
|
||||||
);
|
);
|
||||||
path = ReusableViews;
|
path = ReusableViews;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@@ -433,6 +438,7 @@
|
|||||||
);
|
);
|
||||||
name = "Nextcloud Cookbook iOS Client";
|
name = "Nextcloud Cookbook iOS Client";
|
||||||
packageProductDependencies = (
|
packageProductDependencies = (
|
||||||
|
A74D33BD2AF82AAE00D06555 /* SwiftSoup */,
|
||||||
A9CA6CF52B4C63F200F78AB5 /* TPPDF */,
|
A9CA6CF52B4C63F200F78AB5 /* TPPDF */,
|
||||||
);
|
);
|
||||||
productName = "Nextcloud Cookbook iOS Client";
|
productName = "Nextcloud Cookbook iOS Client";
|
||||||
@@ -511,6 +517,7 @@
|
|||||||
);
|
);
|
||||||
mainGroup = A70171752AA8E71900064C43;
|
mainGroup = A70171752AA8E71900064C43;
|
||||||
packageReferences = (
|
packageReferences = (
|
||||||
|
A74D33BC2AF82AAE00D06555 /* XCRemoteSwiftPackageReference "SwiftSoup" */,
|
||||||
A9CA6CF42B4C63F200F78AB5 /* XCRemoteSwiftPackageReference "TPPDF" */,
|
A9CA6CF42B4C63F200F78AB5 /* XCRemoteSwiftPackageReference "TPPDF" */,
|
||||||
);
|
);
|
||||||
productRefGroup = A701717F2AA8E71900064C43 /* Products */;
|
productRefGroup = A701717F2AA8E71900064C43 /* Products */;
|
||||||
@@ -565,7 +572,7 @@
|
|||||||
A9805BED2BAAC70E003B7231 /* NumberFormatter.swift in Sources */,
|
A9805BED2BAAC70E003B7231 /* NumberFormatter.swift in Sources */,
|
||||||
A9D89AB02B4FE97800F49D92 /* TimerView.swift in Sources */,
|
A9D89AB02B4FE97800F49D92 /* TimerView.swift in Sources */,
|
||||||
A9BBB38C2B8D3B0C002DA7FF /* ParallaxHeaderView.swift in Sources */,
|
A9BBB38C2B8D3B0C002DA7FF /* ParallaxHeaderView.swift in Sources */,
|
||||||
A79AA8E02AFF80E3007D25F2 /* DurationComponents.swift in Sources */,
|
A79AA8E22AFF8C14007D25F2 /* RecipeEditViewModel.swift in Sources */,
|
||||||
A97506152B920DF200E86029 /* RecipeGenericViews.swift in Sources */,
|
A97506152B920DF200E86029 /* RecipeGenericViews.swift in Sources */,
|
||||||
A7FB0D7C2B25C68500A3469E /* TokenLoginView.swift in Sources */,
|
A7FB0D7C2B25C68500A3469E /* TokenLoginView.swift in Sources */,
|
||||||
A977D0E22B60034E009783A9 /* GroceryListTabView.swift in Sources */,
|
A977D0E22B60034E009783A9 /* GroceryListTabView.swift in Sources */,
|
||||||
@@ -579,7 +586,6 @@
|
|||||||
A787B0782B2B1E6400C2DF1B /* DateExtension.swift in Sources */,
|
A787B0782B2B1E6400C2DF1B /* DateExtension.swift in Sources */,
|
||||||
A79AA8E92B062DD1007D25F2 /* CookbookApiV1.swift in Sources */,
|
A79AA8E92B062DD1007D25F2 /* CookbookApiV1.swift in Sources */,
|
||||||
A7CD3FD22B2C546A00D764AD /* CollapsibleView.swift in Sources */,
|
A7CD3FD22B2C546A00D764AD /* CollapsibleView.swift in Sources */,
|
||||||
DFCB4E9FD4E0884AF217E5C5 /* LiquidGlassModifiers.swift in Sources */,
|
|
||||||
A9E78A2B2BE7799F00206866 /* JsonAny.swift in Sources */,
|
A9E78A2B2BE7799F00206866 /* JsonAny.swift in Sources */,
|
||||||
A9BBB3902B91BE31002DA7FF /* ObservableRecipeDetail.swift in Sources */,
|
A9BBB3902B91BE31002DA7FF /* ObservableRecipeDetail.swift in Sources */,
|
||||||
A97506212B92104700E86029 /* RecipeMetadataSection.swift in Sources */,
|
A97506212B92104700E86029 /* RecipeMetadataSection.swift in Sources */,
|
||||||
@@ -603,6 +609,7 @@
|
|||||||
A79AA8ED2B063AD5007D25F2 /* NextcloudApi.swift in Sources */,
|
A79AA8ED2B063AD5007D25F2 /* NextcloudApi.swift in Sources */,
|
||||||
A97506132B920D9F00E86029 /* RecipeDurationSection.swift in Sources */,
|
A97506132B920D9F00E86029 /* RecipeDurationSection.swift in Sources */,
|
||||||
A70171822AA8E71900064C43 /* Nextcloud_Cookbook_iOS_ClientApp.swift in Sources */,
|
A70171822AA8E71900064C43 /* Nextcloud_Cookbook_iOS_ClientApp.swift in Sources */,
|
||||||
|
A74D33C32AFCD1C300D06555 /* RecipeScraper.swift in Sources */,
|
||||||
A70171AD2AA8EF4700064C43 /* AppState.swift in Sources */,
|
A70171AD2AA8EF4700064C43 /* AppState.swift in Sources */,
|
||||||
A76B8A6F2ADFFA8800096CEC /* SupportedLanguage.swift in Sources */,
|
A76B8A6F2ADFFA8800096CEC /* SupportedLanguage.swift in Sources */,
|
||||||
A977D0E02B600318009783A9 /* RecipeTabView.swift in Sources */,
|
A977D0E02B600318009783A9 /* RecipeTabView.swift in Sources */,
|
||||||
@@ -786,11 +793,11 @@
|
|||||||
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
|
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
|
||||||
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
|
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
|
||||||
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
|
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 18.0;
|
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
|
||||||
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
|
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
|
||||||
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
|
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
|
||||||
MACOSX_DEPLOYMENT_TARGET = 14.0;
|
MACOSX_DEPLOYMENT_TARGET = 14.0;
|
||||||
MARKETING_VERSION = 1.10.1;
|
MARKETING_VERSION = 1.10.2;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-Client";
|
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-Client";
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
SDKROOT = auto;
|
SDKROOT = auto;
|
||||||
@@ -830,11 +837,11 @@
|
|||||||
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
|
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
|
||||||
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
|
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
|
||||||
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
|
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 18.0;
|
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
|
||||||
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
|
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
|
||||||
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
|
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
|
||||||
MACOSX_DEPLOYMENT_TARGET = 14.0;
|
MACOSX_DEPLOYMENT_TARGET = 14.0;
|
||||||
MARKETING_VERSION = 1.10.1;
|
MARKETING_VERSION = 1.10.2;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-Client";
|
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-Client";
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
SDKROOT = auto;
|
SDKROOT = auto;
|
||||||
@@ -856,7 +863,7 @@
|
|||||||
DEAD_CODE_STRIPPING = YES;
|
DEAD_CODE_STRIPPING = YES;
|
||||||
DEVELOPMENT_TEAM = EF2ABA36D9;
|
DEVELOPMENT_TEAM = EF2ABA36D9;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 18.0;
|
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
|
||||||
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
||||||
MARKETING_VERSION = 1.0;
|
MARKETING_VERSION = 1.0;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-ClientTests";
|
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-ClientTests";
|
||||||
@@ -880,7 +887,7 @@
|
|||||||
DEAD_CODE_STRIPPING = YES;
|
DEAD_CODE_STRIPPING = YES;
|
||||||
DEVELOPMENT_TEAM = EF2ABA36D9;
|
DEVELOPMENT_TEAM = EF2ABA36D9;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 18.0;
|
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
|
||||||
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
||||||
MARKETING_VERSION = 1.0;
|
MARKETING_VERSION = 1.0;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-ClientTests";
|
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-ClientTests";
|
||||||
@@ -903,7 +910,7 @@
|
|||||||
DEAD_CODE_STRIPPING = YES;
|
DEAD_CODE_STRIPPING = YES;
|
||||||
DEVELOPMENT_TEAM = EF2ABA36D9;
|
DEVELOPMENT_TEAM = EF2ABA36D9;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 18.0;
|
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
|
||||||
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
||||||
MARKETING_VERSION = 1.0;
|
MARKETING_VERSION = 1.0;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-ClientUITests";
|
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-ClientUITests";
|
||||||
@@ -926,7 +933,7 @@
|
|||||||
DEAD_CODE_STRIPPING = YES;
|
DEAD_CODE_STRIPPING = YES;
|
||||||
DEVELOPMENT_TEAM = EF2ABA36D9;
|
DEVELOPMENT_TEAM = EF2ABA36D9;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 18.0;
|
IPHONEOS_DEPLOYMENT_TARGET = 16.4;
|
||||||
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
||||||
MARKETING_VERSION = 1.0;
|
MARKETING_VERSION = 1.0;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-ClientUITests";
|
PRODUCT_BUNDLE_IDENTIFIER = "VincentMeilinger.Nextcloud-Cookbook-iOS-ClientUITests";
|
||||||
@@ -982,6 +989,14 @@
|
|||||||
/* End XCConfigurationList section */
|
/* End XCConfigurationList section */
|
||||||
|
|
||||||
/* Begin XCRemoteSwiftPackageReference section */
|
/* Begin XCRemoteSwiftPackageReference section */
|
||||||
|
A74D33BC2AF82AAE00D06555 /* XCRemoteSwiftPackageReference "SwiftSoup" */ = {
|
||||||
|
isa = XCRemoteSwiftPackageReference;
|
||||||
|
repositoryURL = "https://github.com/scinfu/SwiftSoup.git";
|
||||||
|
requirement = {
|
||||||
|
kind = upToNextMajorVersion;
|
||||||
|
minimumVersion = 2.6.1;
|
||||||
|
};
|
||||||
|
};
|
||||||
A9CA6CF42B4C63F200F78AB5 /* XCRemoteSwiftPackageReference "TPPDF" */ = {
|
A9CA6CF42B4C63F200F78AB5 /* XCRemoteSwiftPackageReference "TPPDF" */ = {
|
||||||
isa = XCRemoteSwiftPackageReference;
|
isa = XCRemoteSwiftPackageReference;
|
||||||
repositoryURL = "https://github.com/techprimate/TPPDF.git";
|
repositoryURL = "https://github.com/techprimate/TPPDF.git";
|
||||||
@@ -993,6 +1008,11 @@
|
|||||||
/* End XCRemoteSwiftPackageReference section */
|
/* End XCRemoteSwiftPackageReference section */
|
||||||
|
|
||||||
/* Begin XCSwiftPackageProductDependency section */
|
/* Begin XCSwiftPackageProductDependency section */
|
||||||
|
A74D33BD2AF82AAE00D06555 /* SwiftSoup */ = {
|
||||||
|
isa = XCSwiftPackageProductDependency;
|
||||||
|
package = A74D33BC2AF82AAE00D06555 /* XCRemoteSwiftPackageReference "SwiftSoup" */;
|
||||||
|
productName = SwiftSoup;
|
||||||
|
};
|
||||||
A9CA6CF52B4C63F200F78AB5 /* TPPDF */ = {
|
A9CA6CF52B4C63F200F78AB5 /* TPPDF */ = {
|
||||||
isa = XCSwiftPackageProductDependency;
|
isa = XCSwiftPackageProductDependency;
|
||||||
package = A9CA6CF42B4C63F200F78AB5 /* XCRemoteSwiftPackageReference "TPPDF" */;
|
package = A9CA6CF42B4C63F200F78AB5 /* XCRemoteSwiftPackageReference "TPPDF" */;
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
{
|
{
|
||||||
"originHash" : "314ca0b5cf5f134470eb4e9e12133500ae78d8b9a08f490e0065f2b3ceb4a25a",
|
|
||||||
"pins" : [
|
"pins" : [
|
||||||
|
{
|
||||||
|
"identity" : "swiftsoup",
|
||||||
|
"kind" : "remoteSourceControl",
|
||||||
|
"location" : "https://github.com/scinfu/SwiftSoup.git",
|
||||||
|
"state" : {
|
||||||
|
"revision" : "8b6cf29eead8841a1fa7822481cb3af4ddaadba6",
|
||||||
|
"version" : "2.6.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"identity" : "tppdf",
|
"identity" : "tppdf",
|
||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
@@ -11,5 +19,5 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"version" : 3
|
"version" : 2
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
@@ -20,75 +19,97 @@ import UIKit
|
|||||||
var imagesNeedUpdate: [Int: [String: Bool]] = [:]
|
var imagesNeedUpdate: [Int: [String: Bool]] = [:]
|
||||||
var lastUpdates: [String: Date] = [:]
|
var lastUpdates: [String: Date] = [:]
|
||||||
var allKeywords: [RecipeKeyword] = []
|
var allKeywords: [RecipeKeyword] = []
|
||||||
|
|
||||||
private let dataStore: DataStore
|
private let dataStore: DataStore
|
||||||
private let api: CookbookApiProtocol
|
|
||||||
|
init() {
|
||||||
init(api: CookbookApiProtocol? = nil) {
|
print("Created MainViewModel")
|
||||||
Logger.network.debug("Created AppState")
|
|
||||||
self.dataStore = DataStore()
|
self.dataStore = DataStore()
|
||||||
self.api = api ?? CookbookApiFactory.makeClient()
|
|
||||||
|
|
||||||
if UserSettings.shared.authString == "" {
|
if UserSettings.shared.authString == "" {
|
||||||
let loginString = "\(UserSettings.shared.username):\(UserSettings.shared.token)"
|
let loginString = "\(UserSettings.shared.username):\(UserSettings.shared.token)"
|
||||||
let loginData = loginString.data(using: String.Encoding.utf8)!
|
let loginData = loginString.data(using: String.Encoding.utf8)!
|
||||||
UserSettings.shared.authString = loginData.base64EncodedString()
|
UserSettings.shared.authString = loginData.base64EncodedString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum FetchMode {
|
enum FetchMode {
|
||||||
case preferLocal, preferServer, onlyLocal, onlyServer
|
case preferLocal, preferServer, onlyLocal, onlyServer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously loads and updates the list of categories.
|
||||||
|
|
||||||
|
This function attempts to fetch the list of categories from the server. If the server connection is successful, it updates the `categories` property in the `MainViewModel` instance and saves the categories locally. If the server connection fails, it attempts to load the categories from local storage.
|
||||||
|
|
||||||
// MARK: - Categories
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
|
*/
|
||||||
func getCategories() async {
|
func getCategories() async {
|
||||||
do {
|
let (categories, _) = await cookbookApi.getCategories(
|
||||||
let categories = try await api.getCategories()
|
auth: UserSettings.shared.authString
|
||||||
Logger.data.debug("Successfully loaded categories")
|
)
|
||||||
|
if let categories = categories {
|
||||||
|
print("Successfully loaded categories")
|
||||||
self.categories = categories
|
self.categories = categories
|
||||||
await saveLocal(self.categories, path: "categories.data")
|
await saveLocal(self.categories, path: "categories.data")
|
||||||
} catch {
|
} else {
|
||||||
Logger.data.debug("Loading categories from store ...")
|
// If there's no server connection, try loading categories from local storage
|
||||||
|
print("Loading categories from store ...")
|
||||||
if let categories: [Category] = await loadLocal(path: "categories.data") {
|
if let categories: [Category] = await loadLocal(path: "categories.data") {
|
||||||
self.categories = categories
|
self.categories = categories
|
||||||
Logger.data.debug("Loaded categories from local store")
|
print("Success!")
|
||||||
} else {
|
} else {
|
||||||
Logger.data.error("Failed to load categories from local store")
|
print("Failure!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize the lastUpdates with distantPast dates, so that each recipeDetail is updated on launch for all categories
|
||||||
for category in self.categories {
|
for category in self.categories {
|
||||||
lastUpdates[category.name] = Date.distantPast
|
lastUpdates[category.name] = Date.distantPast
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Fetches recipes for a specified category from either the server or local storage.
|
||||||
|
|
||||||
|
- Parameters:
|
||||||
|
- name: The name of the category. Use "*" to fetch recipes without assigned categories.
|
||||||
|
- needsUpdate: If true, recipes will be loaded from the server directly; otherwise, they will be loaded from local storage first.
|
||||||
|
|
||||||
|
This function asynchronously retrieves recipes for the specified category from the server or local storage based on the provided parameters. If `needsUpdate` is true, the function fetches recipes from the server and updates the local storage. If `needsUpdate` is false, it attempts to load recipes from local storage.
|
||||||
|
|
||||||
|
- Note: The category name "*" is used for all uncategorized recipes.
|
||||||
|
|
||||||
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
|
*/
|
||||||
func getCategory(named name: String, fetchMode: FetchMode) async {
|
func getCategory(named name: String, fetchMode: FetchMode) async {
|
||||||
Logger.data.debug("getCategory(\(name), fetchMode: \(String(describing: fetchMode)))")
|
print("getCategory(\(name), fetchMode: \(fetchMode))")
|
||||||
func getLocal() async -> Bool {
|
func getLocal() async -> Bool {
|
||||||
let categoryString = name == "*" ? "_" : name
|
|
||||||
if let recipes: [Recipe] = await loadLocal(path: "category_\(categoryString).data") {
|
if let recipes: [Recipe] = await loadLocal(path: "category_\(categoryString).data") {
|
||||||
self.recipes[name] = recipes
|
self.recipes[name] = recipes
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServer(store: Bool = false) async -> Bool {
|
func getServer(store: Bool = false) async -> Bool {
|
||||||
let categoryString = name == "*" ? "_" : name
|
let (recipes, _) = await cookbookApi.getCategory(
|
||||||
do {
|
auth: UserSettings.shared.authString,
|
||||||
let recipes = try await api.getCategory(named: categoryString)
|
named: categoryString
|
||||||
|
)
|
||||||
|
if let recipes = recipes {
|
||||||
self.recipes[name] = recipes
|
self.recipes[name] = recipes
|
||||||
if store {
|
if store {
|
||||||
await saveLocal(recipes, path: "category_\(categoryString).data")
|
await saveLocal(recipes, path: "category_\(categoryString).data")
|
||||||
}
|
}
|
||||||
|
//userSettings.lastUpdate = Date()
|
||||||
return true
|
return true
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let categoryString = name == "*" ? "_" : name
|
||||||
switch fetchMode {
|
switch fetchMode {
|
||||||
case .preferLocal:
|
case .preferLocal:
|
||||||
if await getLocal() { return }
|
if await getLocal() { return }
|
||||||
@@ -102,68 +123,102 @@ import UIKit
|
|||||||
if await getServer() { return }
|
if await getServer() { return }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Recipe details
|
|
||||||
|
|
||||||
func updateAllRecipeDetails() async {
|
func updateAllRecipeDetails() async {
|
||||||
for category in self.categories {
|
for category in self.categories {
|
||||||
await updateRecipeDetails(in: category.name)
|
await updateRecipeDetails(in: category.name)
|
||||||
}
|
}
|
||||||
UserSettings.shared.lastUpdate = Date()
|
UserSettings.shared.lastUpdate = Date()
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateRecipeDetails(in category: String) async {
|
func updateRecipeDetails(in category: String) async {
|
||||||
guard UserSettings.shared.storeRecipes else { return }
|
guard UserSettings.shared.storeRecipes else { return }
|
||||||
guard let recipes = self.recipes[category] else { return }
|
guard let recipes = self.recipes[category] else { return }
|
||||||
for recipe in recipes {
|
for recipe in recipes {
|
||||||
if let dateModified = recipe.dateModified {
|
if let dateModified = recipe.dateModified {
|
||||||
if needsUpdate(category: category, lastModified: dateModified) {
|
if needsUpdate(category: category, lastModified: dateModified) {
|
||||||
Logger.data.debug("\(recipe.name) needs an update. (last modified: \(recipe.dateModified ?? "unknown"))")
|
print("\(recipe.name) needs an update. (last modified: \(recipe.dateModified)")
|
||||||
await updateRecipeDetail(id: recipe.recipe_id, withThumb: UserSettings.shared.storeThumb, withImage: UserSettings.shared.storeImages)
|
await updateRecipeDetail(id: recipe.recipe_id, withThumb: UserSettings.shared.storeThumb, withImage: UserSettings.shared.storeImages)
|
||||||
} else {
|
} else {
|
||||||
Logger.data.debug("\(recipe.name) is up to date.")
|
print("\(recipe.name) is up to date.")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await updateRecipeDetail(id: recipe.recipe_id, withThumb: UserSettings.shared.storeThumb, withImage: UserSettings.shared.storeImages)
|
await updateRecipeDetail(id: recipe.recipe_id, withThumb: UserSettings.shared.storeThumb, withImage: UserSettings.shared.storeImages)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously retrieves all recipes either from the server or the locally cached data.
|
||||||
|
|
||||||
|
This function attempts to fetch all recipes from the server using the provided `api`. If the server connection is successful, it returns the fetched recipes. If the server connection fails, it falls back to combining locally cached recipes from different categories.
|
||||||
|
|
||||||
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance, and categories have been previously loaded.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
```swift
|
||||||
|
let recipes = await mainViewModel.getRecipes()
|
||||||
|
*/
|
||||||
func getRecipes() async -> [Recipe] {
|
func getRecipes() async -> [Recipe] {
|
||||||
do {
|
let (recipes, error) = await cookbookApi.getRecipes(
|
||||||
return try await api.getRecipes()
|
auth: UserSettings.shared.authString
|
||||||
} catch {
|
)
|
||||||
Logger.network.error("Failed to fetch recipes: \(error.localizedDescription)")
|
if let recipes = recipes {
|
||||||
|
return recipes
|
||||||
|
} else if let error = error {
|
||||||
|
print(error)
|
||||||
}
|
}
|
||||||
var allRecipes: [Recipe] = []
|
var allRecipes: [Recipe] = []
|
||||||
for category in categories {
|
for category in categories {
|
||||||
|
if self.recipes[category.name] == nil {
|
||||||
|
await getCategory(named: category.name, fetchMode: .preferLocal)
|
||||||
|
}
|
||||||
if let recipeArray = self.recipes[category.name] {
|
if let recipeArray = self.recipes[category.name] {
|
||||||
allRecipes.append(contentsOf: recipeArray)
|
allRecipes.append(contentsOf: recipeArray)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return allRecipes.sorted(by: { $0.name < $1.name })
|
return allRecipes.sorted(by: {
|
||||||
|
$0.name < $1.name
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously retrieves a recipe detail either from the server or locally cached data.
|
||||||
|
|
||||||
|
This function attempts to fetch a recipe detail with the specified `id` from the server using the provided `api`. If the server connection is successful, it returns the fetched recipe detail. If the server connection fails, it falls back to loading the recipe detail from local storage.
|
||||||
|
|
||||||
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
|
|
||||||
|
- Parameters:
|
||||||
|
- id: The identifier of the recipe to retrieve.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
```swift
|
||||||
|
let recipeDetail = await mainViewModel.getRecipe(id: 123)
|
||||||
|
*/
|
||||||
func getRecipe(id: Int, fetchMode: FetchMode, save: Bool = false) async -> RecipeDetail? {
|
func getRecipe(id: Int, fetchMode: FetchMode, save: Bool = false) async -> RecipeDetail? {
|
||||||
func getLocal() async -> RecipeDetail? {
|
func getLocal() async -> RecipeDetail? {
|
||||||
if let recipe: RecipeDetail = await loadLocal(path: "recipe\(id).data") { return recipe }
|
if let recipe: RecipeDetail = await loadLocal(path: "recipe\(id).data") { return recipe }
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServer() async -> RecipeDetail? {
|
func getServer() async -> RecipeDetail? {
|
||||||
do {
|
let (recipe, error) = await cookbookApi.getRecipe(
|
||||||
let recipe = try await api.getRecipe(id: id)
|
auth: UserSettings.shared.authString,
|
||||||
|
id: id
|
||||||
|
)
|
||||||
|
if let recipe = recipe {
|
||||||
if save {
|
if save {
|
||||||
self.recipeDetails[id] = recipe
|
self.recipeDetails[id] = recipe
|
||||||
await self.saveLocal(recipe, path: "recipe\(id).data")
|
await self.saveLocal(recipe, path: "recipe\(id).data")
|
||||||
}
|
}
|
||||||
return recipe
|
return recipe
|
||||||
} catch {
|
} else if let error = error {
|
||||||
Logger.network.error("Failed to fetch recipe \(id): \(error.localizedDescription)")
|
print(error)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch fetchMode {
|
switch fetchMode {
|
||||||
case .preferLocal:
|
case .preferLocal:
|
||||||
if let recipe = await getLocal() { return recipe }
|
if let recipe = await getLocal() { return recipe }
|
||||||
@@ -178,19 +233,31 @@ import UIKit
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously downloads and saves details, thumbnails, and full images for all recipes.
|
||||||
|
|
||||||
|
This function iterates through all loaded categories, fetches and updates the recipes from the server, and then downloads and saves details, thumbnails, and full images for each recipe.
|
||||||
|
|
||||||
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
```swift
|
||||||
|
await mainViewModel.downloadAllRecipes()
|
||||||
|
*/
|
||||||
func updateRecipeDetail(id: Int, withThumb: Bool, withImage: Bool) async {
|
func updateRecipeDetail(id: Int, withThumb: Bool, withImage: Bool) async {
|
||||||
if let recipeDetail = await getRecipe(id: id, fetchMode: .onlyServer) {
|
if let recipeDetail = await getRecipe(id: id, fetchMode: .onlyServer) {
|
||||||
await saveLocal(recipeDetail, path: "recipe\(id).data")
|
await saveLocal(recipeDetail, path: "recipe\(id).data")
|
||||||
}
|
}
|
||||||
|
|
||||||
if withThumb {
|
if withThumb {
|
||||||
let thumbnail = await getImage(id: id, size: .THUMB, fetchMode: .onlyServer)
|
let thumbnail = await getImage(id: id, size: .THUMB, fetchMode: .onlyServer)
|
||||||
guard let thumbnail = thumbnail else { return }
|
guard let thumbnail = thumbnail else { return }
|
||||||
guard let thumbnailData = thumbnail.pngData() else { return }
|
guard let thumbnailData = thumbnail.pngData() else { return }
|
||||||
await saveLocal(thumbnailData.base64EncodedString(), path: "image\(id)_thumb")
|
await saveLocal(thumbnailData.base64EncodedString(), path: "image\(id)_thumb")
|
||||||
}
|
}
|
||||||
|
|
||||||
if withImage {
|
if withImage {
|
||||||
let image = await getImage(id: id, size: .FULL, fetchMode: .onlyServer)
|
let image = await getImage(id: id, size: .FULL, fetchMode: .onlyServer)
|
||||||
guard let image = image else { return }
|
guard let image = image else { return }
|
||||||
@@ -198,26 +265,50 @@ import UIKit
|
|||||||
await saveLocal(imageData.base64EncodedString(), path: "image\(id)_full")
|
await saveLocal(imageData.base64EncodedString(), path: "image\(id)_full")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Check if recipeDetail is stored locally, either in cache or on disk
|
||||||
|
/// - Parameters
|
||||||
|
/// - recipeId: The id of a recipe.
|
||||||
|
/// - Returns: True if the recipeDetail is stored, otherwise false
|
||||||
func recipeDetailExists(recipeId: Int) -> Bool {
|
func recipeDetailExists(recipeId: Int) -> Bool {
|
||||||
return dataStore.recipeDetailExists(recipeId: recipeId)
|
if (dataStore.recipeDetailExists(recipeId: recipeId)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously retrieves and returns an image for a recipe with the specified ID and size.
|
||||||
|
|
||||||
// MARK: - Images
|
This function attempts to fetch an image for a recipe with the specified `id` and `size` from the server using the provided `api`. If the server connection is successful, it returns the fetched image. If the server connection fails or `needsUpdate` is false, it attempts to load the image from local storage.
|
||||||
|
|
||||||
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
|
|
||||||
|
- Parameters:
|
||||||
|
- id: The identifier of the recipe associated with the image.
|
||||||
|
- size: The size of the desired image (thumbnail or full).
|
||||||
|
- needsUpdate: If true, the image will be loaded from the server directly; otherwise, it will be loaded from local storage.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
```swift
|
||||||
|
let thumbnail = await mainViewModel.getImage(id: 123, size: .THUMB, needsUpdate: true)
|
||||||
|
*/
|
||||||
func getImage(id: Int, size: RecipeImage.RecipeImageSize, fetchMode: FetchMode) async -> UIImage? {
|
func getImage(id: Int, size: RecipeImage.RecipeImageSize, fetchMode: FetchMode) async -> UIImage? {
|
||||||
func getLocal() async -> UIImage? {
|
func getLocal() async -> UIImage? {
|
||||||
return await imageFromStore(id: id, size: size)
|
return await imageFromStore(id: id, size: size)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServer() async -> UIImage? {
|
func getServer() async -> UIImage? {
|
||||||
do {
|
let (image, _) = await cookbookApi.getImage(
|
||||||
return try await api.getImage(id: id, size: size)
|
auth: UserSettings.shared.authString,
|
||||||
} catch {
|
id: id,
|
||||||
return nil
|
size: size
|
||||||
}
|
)
|
||||||
|
if let image = image { return image }
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch fetchMode {
|
switch fetchMode {
|
||||||
case .preferLocal:
|
case .preferLocal:
|
||||||
if let image = imageFromCache(id: id, size: size) {
|
if let image = imageFromCache(id: id, size: size) {
|
||||||
@@ -267,20 +358,28 @@ import UIKit
|
|||||||
imagesNeedUpdate[id] = [size.rawValue: false]
|
imagesNeedUpdate[id] = [size.rawValue: false]
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously retrieves and returns a list of keywords (tags).
|
||||||
|
|
||||||
// MARK: - Keywords
|
This function attempts to fetch a list of keywords from the server using the provided `api`. If the server connection is successful, it returns the fetched keywords. If the server connection fails, it attempts to load the keywords from local storage.
|
||||||
|
|
||||||
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
```swift
|
||||||
|
let keywords = await mainViewModel.getKeywords()
|
||||||
|
*/
|
||||||
func getKeywords(fetchMode: FetchMode) async -> [RecipeKeyword] {
|
func getKeywords(fetchMode: FetchMode) async -> [RecipeKeyword] {
|
||||||
func getLocal() async -> [RecipeKeyword]? {
|
func getLocal() async -> [RecipeKeyword]? {
|
||||||
return await loadLocal(path: "keywords.data")
|
return await loadLocal(path: "keywords.data")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServer() async -> [RecipeKeyword]? {
|
func getServer() async -> [RecipeKeyword]? {
|
||||||
do {
|
let (tags, _) = await cookbookApi.getTags(
|
||||||
return try await api.getTags()
|
auth: UserSettings.shared.authString
|
||||||
} catch {
|
)
|
||||||
return nil
|
return tags
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch fetchMode {
|
switch fetchMode {
|
||||||
@@ -303,9 +402,7 @@ import UIKit
|
|||||||
}
|
}
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Data management
|
|
||||||
|
|
||||||
func deleteAllData() {
|
func deleteAllData() {
|
||||||
if dataStore.clearAll() {
|
if dataStore.clearAll() {
|
||||||
self.categories = []
|
self.categories = []
|
||||||
@@ -315,14 +412,31 @@ import UIKit
|
|||||||
self.imagesNeedUpdate = [:]
|
self.imagesNeedUpdate = [:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously deletes a recipe with the specified ID from the server and local storage.
|
||||||
|
|
||||||
|
This function attempts to delete a recipe with the specified `id` from the server using the provided `api`. If the server connection is successful, it proceeds to delete the local copy of the recipe and its details. If the server connection fails, it returns `RequestAlert.REQUEST_DROPPED`.
|
||||||
|
|
||||||
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
|
|
||||||
|
- Parameters:
|
||||||
|
- id: The identifier of the recipe to delete.
|
||||||
|
- categoryName: The name of the category to which the recipe belongs.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
```swift
|
||||||
|
let requestResult = await mainViewModel.deleteRecipe(withId: 123, categoryName: "Desserts")
|
||||||
|
*/
|
||||||
func deleteRecipe(withId id: Int, categoryName: String) async -> RequestAlert? {
|
func deleteRecipe(withId id: Int, categoryName: String) async -> RequestAlert? {
|
||||||
do {
|
let (error) = await cookbookApi.deleteRecipe(
|
||||||
try await api.deleteRecipe(id: id)
|
auth: UserSettings.shared.authString,
|
||||||
} catch {
|
id: id
|
||||||
|
)
|
||||||
|
|
||||||
|
if let error = error {
|
||||||
return .REQUEST_DROPPED
|
return .REQUEST_DROPPED
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = "recipe\(id).data"
|
let path = "recipe\(id).data"
|
||||||
dataStore.delete(path: path)
|
dataStore.delete(path: path)
|
||||||
if recipes[categoryName] != nil {
|
if recipes[categoryName] != nil {
|
||||||
@@ -333,59 +447,95 @@ import UIKit
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously checks the server connection by attempting to fetch categories.
|
||||||
|
|
||||||
|
This function attempts to fetch categories from the server using the provided `api` to check the server connection status. If the server connection is successful, it updates the `categories` property in the `MainViewModel` instance and saves the categories locally. If the server connection fails, it returns `false`.
|
||||||
|
|
||||||
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
```swift
|
||||||
|
let isConnected = await mainViewModel.checkServerConnection()
|
||||||
|
*/
|
||||||
func checkServerConnection() async -> Bool {
|
func checkServerConnection() async -> Bool {
|
||||||
do {
|
let (categories, _) = await cookbookApi.getCategories(
|
||||||
let categories = try await api.getCategories()
|
auth: UserSettings.shared.authString
|
||||||
|
)
|
||||||
|
if let categories = categories {
|
||||||
self.categories = categories
|
self.categories = categories
|
||||||
await saveLocal(categories, path: "categories.data")
|
await saveLocal(categories, path: "categories.data")
|
||||||
return true
|
return true
|
||||||
} catch {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Asynchronously uploads a recipe to the server.
|
||||||
|
|
||||||
func uploadRecipe(recipeDetail: RecipeDetail, createNew: Bool) async -> (Int?, RequestAlert?) {
|
This function attempts to create or update a recipe on the server using the provided `api`. If the server connection is successful, it uploads the provided `recipeDetail`. If the server connection fails, it returns `RequestAlert.REQUEST_DROPPED`.
|
||||||
do {
|
|
||||||
if createNew {
|
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||||
let id = try await api.createRecipe(recipeDetail)
|
|
||||||
return (id, nil)
|
- Parameters:
|
||||||
} else {
|
- recipeDetail: The detailed information of the recipe to upload.
|
||||||
let id = try await api.updateRecipe(recipeDetail)
|
- createNew: If true, creates a new recipe on the server; otherwise, updates an existing one.
|
||||||
return (id, nil)
|
|
||||||
}
|
Example usage:
|
||||||
} catch {
|
```swift
|
||||||
return (nil, .REQUEST_DROPPED)
|
let uploadResult = await mainViewModel.uploadRecipe(recipeDetail: myRecipeDetail, createNew: true)
|
||||||
|
*/
|
||||||
|
func uploadRecipe(recipeDetail: RecipeDetail, createNew: Bool) async -> RequestAlert? {
|
||||||
|
var error: NetworkError? = nil
|
||||||
|
if createNew {
|
||||||
|
error = await cookbookApi.createRecipe(
|
||||||
|
auth: UserSettings.shared.authString,
|
||||||
|
recipe: recipeDetail
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
error = await cookbookApi.updateRecipe(
|
||||||
|
auth: UserSettings.shared.authString,
|
||||||
|
recipe: recipeDetail
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
if error != nil {
|
||||||
|
return .REQUEST_DROPPED
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func importRecipe(url: String) async -> (RecipeDetail?, RequestAlert?) {
|
func importRecipe(url: String) async -> (RecipeDetail?, RequestAlert?) {
|
||||||
do {
|
guard let data = JSONEncoder.safeEncode(RecipeImportRequest(url: url)) else { return (nil, .REQUEST_DROPPED) }
|
||||||
let recipeDetail = try await api.importRecipe(url: url)
|
let (recipeDetail, error) = await cookbookApi.importRecipe(
|
||||||
return (recipeDetail, nil)
|
auth: UserSettings.shared.authString,
|
||||||
} catch {
|
data: data
|
||||||
|
)
|
||||||
|
if error != nil {
|
||||||
return (nil, .REQUEST_DROPPED)
|
return (nil, .REQUEST_DROPPED)
|
||||||
}
|
}
|
||||||
|
return (recipeDetail, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: - Local storage helpers
|
|
||||||
|
|
||||||
extension AppState {
|
extension AppState {
|
||||||
func loadLocal<T: Codable>(path: String) async -> T? {
|
func loadLocal<T: Codable>(path: String) async -> T? {
|
||||||
do {
|
do {
|
||||||
return try await dataStore.load(fromPath: path)
|
return try await dataStore.load(fromPath: path)
|
||||||
} catch {
|
} catch (let error) {
|
||||||
Logger.data.debug("Failed to load local data: \(error.localizedDescription)")
|
print(error)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveLocal<T: Codable>(_ object: T, path: String) async {
|
func saveLocal<T: Codable>(_ object: T, path: String) async {
|
||||||
await dataStore.save(data: object, toPath: path)
|
await dataStore.save(data: object, toPath: path)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func imageFromStore(id: Int, size: RecipeImage.RecipeImageSize) async -> UIImage? {
|
private func imageFromStore(id: Int, size: RecipeImage.RecipeImageSize) async -> UIImage? {
|
||||||
do {
|
do {
|
||||||
let localPath = "image\(id)_\(size == .FULL ? "full" : "thumb")"
|
let localPath = "image\(id)_\(size == .FULL ? "full" : "thumb")"
|
||||||
@@ -395,18 +545,18 @@ extension AppState {
|
|||||||
return image
|
return image
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
Logger.data.debug("Could not find image in local storage.")
|
print("Could not find image in local storage.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
private func imageToStore(id: Int, size: RecipeImage.RecipeImageSize, image: UIImage) async {
|
private func imageToStore(id: Int, size: RecipeImage.RecipeImageSize, image: UIImage) async {
|
||||||
if let data = image.pngData() {
|
if let data = image.pngData() {
|
||||||
await saveLocal(data.base64EncodedString(), path: "image\(id)_\(size.rawValue)")
|
await saveLocal(data.base64EncodedString(), path: "image\(id)_\(size.rawValue)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func imageToCache(id: Int, size: RecipeImage.RecipeImageSize, image: UIImage) {
|
private func imageToCache(id: Int, size: RecipeImage.RecipeImageSize, image: UIImage) {
|
||||||
if recipeImages[id] != nil {
|
if recipeImages[id] != nil {
|
||||||
recipeImages[id]![size.rawValue] = image
|
recipeImages[id]![size.rawValue] = image
|
||||||
@@ -419,14 +569,14 @@ extension AppState {
|
|||||||
imagesNeedUpdate[id] = [size.rawValue: false]
|
imagesNeedUpdate[id] = [size.rawValue: false]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func imageFromCache(id: Int, size: RecipeImage.RecipeImageSize) -> UIImage? {
|
private func imageFromCache(id: Int, size: RecipeImage.RecipeImageSize) -> UIImage? {
|
||||||
if recipeImages[id] != nil {
|
if recipeImages[id] != nil {
|
||||||
return recipeImages[id]![size.rawValue]
|
return recipeImages[id]![size.rawValue]
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
private func imageUpdateNeeded(id: Int, size: RecipeImage.RecipeImageSize) -> Bool {
|
private func imageUpdateNeeded(id: Int, size: RecipeImage.RecipeImageSize) -> Bool {
|
||||||
if imagesNeedUpdate[id] != nil {
|
if imagesNeedUpdate[id] != nil {
|
||||||
if imagesNeedUpdate[id]![size.rawValue] != nil {
|
if imagesNeedUpdate[id]![size.rawValue] != nil {
|
||||||
@@ -435,22 +585,26 @@ extension AppState {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private func needsUpdate(category: String, lastModified: String) -> Bool {
|
private func needsUpdate(category: String, lastModified: String) -> Bool {
|
||||||
|
print("=======================")
|
||||||
|
print("original date string: \(lastModified)")
|
||||||
|
// Create a DateFormatter
|
||||||
let dateFormatter = DateFormatter()
|
let dateFormatter = DateFormatter()
|
||||||
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
||||||
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
|
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||||
|
|
||||||
|
// Convert the string to a Date object
|
||||||
if let date = dateFormatter.date(from: lastModified), let lastUpdate = lastUpdates[category] {
|
if let date = dateFormatter.date(from: lastModified), let lastUpdate = lastUpdates[category] {
|
||||||
if date < lastUpdate {
|
if date < lastUpdate {
|
||||||
Logger.data.debug("No update needed for \(category)")
|
print("No update needed. (recipe: \(dateFormatter.string(from: date)), last: \(dateFormatter.string(from: lastUpdate))")
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
Logger.data.debug("Update needed for \(category)")
|
print("Update needed. (recipe: \(dateFormatter.string(from: date)), last: \(dateFormatter.string(from: lastUpdate))")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Logger.data.debug("Date parse failed, update needed for \(category)")
|
print("String is not a date. Update needed.")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -473,11 +627,11 @@ extension AppState {
|
|||||||
timers[recipeId] = timer
|
timers[recipeId] = timer
|
||||||
return timer
|
return timer
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTimer(forRecipe recipeId: String, duration: DurationComponents) -> RecipeTimer {
|
func getTimer(forRecipe recipeId: String, duration: DurationComponents) -> RecipeTimer {
|
||||||
return timers[recipeId] ?? createTimer(forRecipe: recipeId, duration: duration)
|
return timers[recipeId] ?? createTimer(forRecipe: recipeId, duration: duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteTimer(forRecipe recipeId: String) {
|
func deleteTimer(forRecipe recipeId: String) {
|
||||||
timers.removeValue(forKey: recipeId)
|
timers.removeValue(forKey: recipeId)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,14 +20,6 @@ struct Category: Codable {
|
|||||||
|
|
||||||
extension Category: Identifiable, Hashable {
|
extension Category: Identifiable, Hashable {
|
||||||
var id: String { name }
|
var id: String { name }
|
||||||
|
|
||||||
static func == (lhs: Category, rhs: Category) -> Bool {
|
|
||||||
lhs.name == rhs.name
|
|
||||||
}
|
|
||||||
|
|
||||||
func hash(into hasher: inout Hasher) {
|
|
||||||
hasher.combine(name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,12 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
class DataStore {
|
class DataStore {
|
||||||
let fileManager = FileManager.default
|
let fileManager = FileManager.default
|
||||||
static let shared = DataStore()
|
static let shared = DataStore()
|
||||||
|
|
||||||
private static func fileURL(appending: String) throws -> URL {
|
private static func fileURL(appending: String) throws -> URL {
|
||||||
try FileManager.default.url(
|
try FileManager.default.url(
|
||||||
for: .documentDirectory,
|
for: .documentDirectory,
|
||||||
@@ -22,7 +21,7 @@ class DataStore {
|
|||||||
)
|
)
|
||||||
.appendingPathComponent(appending)
|
.appendingPathComponent(appending)
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func fileURL() throws -> URL {
|
private static func fileURL() throws -> URL {
|
||||||
try FileManager.default.url(
|
try FileManager.default.url(
|
||||||
for: .documentDirectory,
|
for: .documentDirectory,
|
||||||
@@ -31,7 +30,7 @@ class DataStore {
|
|||||||
create: false
|
create: false
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func load<D: Decodable>(fromPath path: String) async throws -> D? {
|
func load<D: Decodable>(fromPath path: String) async throws -> D? {
|
||||||
let task = Task<D?, Error> {
|
let task = Task<D?, Error> {
|
||||||
let fileURL = try Self.fileURL(appending: path)
|
let fileURL = try Self.fileURL(appending: path)
|
||||||
@@ -43,7 +42,7 @@ class DataStore {
|
|||||||
}
|
}
|
||||||
return try await task.value
|
return try await task.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func save<D: Encodable>(data: D, toPath path: String) async {
|
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)
|
||||||
@@ -53,36 +52,42 @@ class DataStore {
|
|||||||
do {
|
do {
|
||||||
_ = try await task.value
|
_ = try await task.value
|
||||||
} catch {
|
} catch {
|
||||||
Logger.data.error("Could not save data (path: \(path))")
|
print("Could not save data (path: \(path)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func delete(path: String) {
|
func delete(path: String) {
|
||||||
Task {
|
Task {
|
||||||
let fileURL = try Self.fileURL(appending: path)
|
let fileURL = try Self.fileURL(appending: path)
|
||||||
try fileManager.removeItem(at: fileURL)
|
try fileManager.removeItem(at: fileURL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func recipeDetailExists(recipeId: Int) -> Bool {
|
func recipeDetailExists(recipeId: Int) -> Bool {
|
||||||
let filePath = "recipe\(recipeId).data"
|
let filePath = "recipe\(recipeId).data"
|
||||||
guard let folderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.path() else { return false }
|
guard let folderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.path() else { return false }
|
||||||
return fileManager.fileExists(atPath: folderPath + filePath)
|
return fileManager.fileExists(atPath: folderPath + filePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearAll() -> Bool {
|
func clearAll() -> Bool {
|
||||||
Logger.data.debug("Attempting to delete all data ...")
|
print("Attempting to delete all data ...")
|
||||||
guard let folderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.path() else { return false }
|
guard let folderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.path() else { return false }
|
||||||
|
print("Folder path: ", folderPath)
|
||||||
do {
|
do {
|
||||||
let filePaths = try fileManager.contentsOfDirectory(atPath: folderPath)
|
let filePaths = try fileManager.contentsOfDirectory(atPath: folderPath)
|
||||||
for filePath in filePaths {
|
for filePath in filePaths {
|
||||||
|
print("File path: ", filePath)
|
||||||
try fileManager.removeItem(atPath: folderPath + filePath)
|
try fileManager.removeItem(atPath: folderPath + filePath)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
Logger.data.error("Could not delete documents folder contents: \(error.localizedDescription)")
|
print("Could not delete documents folder contents: \(error)")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
Logger.data.debug("All data deleted successfully.")
|
print("Done.")
|
||||||
return true
|
return true
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
class ObservableRecipeDetail: ObservableObject {
|
class ObservableRecipeDetail: ObservableObject {
|
||||||
@@ -14,6 +13,7 @@ class ObservableRecipeDetail: ObservableObject {
|
|||||||
var id: String
|
var id: String
|
||||||
@Published var name: String
|
@Published var name: String
|
||||||
@Published var keywords: [String]
|
@Published var keywords: [String]
|
||||||
|
@Published var image: String
|
||||||
@Published var imageUrl: String
|
@Published var imageUrl: String
|
||||||
@Published var prepTime: DurationComponents
|
@Published var prepTime: DurationComponents
|
||||||
@Published var cookTime: DurationComponents
|
@Published var cookTime: DurationComponents
|
||||||
@@ -36,6 +36,7 @@ class ObservableRecipeDetail: ObservableObject {
|
|||||||
id = ""
|
id = ""
|
||||||
name = String(localized: "New Recipe")
|
name = String(localized: "New Recipe")
|
||||||
keywords = []
|
keywords = []
|
||||||
|
image = ""
|
||||||
imageUrl = ""
|
imageUrl = ""
|
||||||
prepTime = DurationComponents()
|
prepTime = DurationComponents()
|
||||||
cookTime = DurationComponents()
|
cookTime = DurationComponents()
|
||||||
@@ -56,6 +57,7 @@ class ObservableRecipeDetail: ObservableObject {
|
|||||||
id = recipeDetail.id
|
id = recipeDetail.id
|
||||||
name = recipeDetail.name
|
name = recipeDetail.name
|
||||||
keywords = recipeDetail.keywords.isEmpty ? [] : recipeDetail.keywords.components(separatedBy: ",")
|
keywords = recipeDetail.keywords.isEmpty ? [] : recipeDetail.keywords.components(separatedBy: ",")
|
||||||
|
image = recipeDetail.image ?? ""
|
||||||
imageUrl = recipeDetail.imageUrl ?? ""
|
imageUrl = recipeDetail.imageUrl ?? ""
|
||||||
prepTime = DurationComponents.fromPTString(recipeDetail.prepTime ?? "")
|
prepTime = DurationComponents.fromPTString(recipeDetail.prepTime ?? "")
|
||||||
cookTime = DurationComponents.fromPTString(recipeDetail.cookTime ?? "")
|
cookTime = DurationComponents.fromPTString(recipeDetail.cookTime ?? "")
|
||||||
@@ -78,6 +80,7 @@ class ObservableRecipeDetail: ObservableObject {
|
|||||||
keywords: self.keywords.joined(separator: ","),
|
keywords: self.keywords.joined(separator: ","),
|
||||||
dateCreated: "",
|
dateCreated: "",
|
||||||
dateModified: "",
|
dateModified: "",
|
||||||
|
image: self.image,
|
||||||
imageUrl: self.imageUrl,
|
imageUrl: self.imageUrl,
|
||||||
id: self.id,
|
id: self.id,
|
||||||
prepTime: self.prepTime.toPTString(),
|
prepTime: self.prepTime.toPTString(),
|
||||||
@@ -94,21 +97,59 @@ class ObservableRecipeDetail: ObservableObject {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
static func adjustIngredient(_ ingredient: String, by factor: Double) -> AttributedString {
|
static func applyMarkdownStyling(_ text: String) -> AttributedString {
|
||||||
|
if var markdownString = try? AttributedString(
|
||||||
|
markdown: text,
|
||||||
|
options: .init(
|
||||||
|
allowsExtendedAttributes: true,
|
||||||
|
interpretedSyntax: .full,
|
||||||
|
failurePolicy: .returnPartiallyParsedIfPossible
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
for (intentBlock, intentRange) in markdownString.runs[AttributeScopes.FoundationAttributes.PresentationIntentAttribute.self].reversed() {
|
||||||
|
guard let intentBlock = intentBlock else { continue }
|
||||||
|
for intent in intentBlock.components {
|
||||||
|
switch intent.kind {
|
||||||
|
case .header(level: let level):
|
||||||
|
switch level {
|
||||||
|
case 1:
|
||||||
|
markdownString[intentRange].font = .system(.title).bold()
|
||||||
|
case 2:
|
||||||
|
markdownString[intentRange].font = .system(.title2).bold()
|
||||||
|
case 3:
|
||||||
|
markdownString[intentRange].font = .system(.title3).bold()
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if intentRange.lowerBound != markdownString.startIndex {
|
||||||
|
markdownString.characters.insert(contentsOf: "\n", at: intentRange.lowerBound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return markdownString
|
||||||
|
}
|
||||||
|
return AttributedString(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func adjustIngredient(_ ingredient: AttributedString, by factor: Double) -> AttributedString {
|
||||||
if factor == 0 {
|
if factor == 0 {
|
||||||
return AttributedString(ingredient)
|
return ingredient
|
||||||
}
|
}
|
||||||
// Match mixed fractions first
|
// Match mixed fractions first
|
||||||
var matches = ObservableRecipeDetail.matchPatternAndMultiply(
|
var matches = ObservableRecipeDetail.matchPatternAndMultiply(
|
||||||
.mixedFraction,
|
.mixedFraction,
|
||||||
in: ingredient,
|
in: String(ingredient.characters),
|
||||||
multFactor: factor
|
multFactor: factor
|
||||||
)
|
)
|
||||||
// Then match fractions, exclude mixed fraction ranges
|
// Then match fractions, exclude mixed fraction ranges
|
||||||
matches.append(contentsOf:
|
matches.append(contentsOf:
|
||||||
ObservableRecipeDetail.matchPatternAndMultiply(
|
ObservableRecipeDetail.matchPatternAndMultiply(
|
||||||
.fraction,
|
.fraction,
|
||||||
in: ingredient,
|
in: String(ingredient.characters),
|
||||||
multFactor: factor,
|
multFactor: factor,
|
||||||
excludedRanges: matches.map({ tuple in tuple.1 })
|
excludedRanges: matches.map({ tuple in tuple.1 })
|
||||||
)
|
)
|
||||||
@@ -117,7 +158,7 @@ class ObservableRecipeDetail: ObservableObject {
|
|||||||
matches.append(contentsOf:
|
matches.append(contentsOf:
|
||||||
ObservableRecipeDetail.matchPatternAndMultiply(
|
ObservableRecipeDetail.matchPatternAndMultiply(
|
||||||
.number,
|
.number,
|
||||||
in: ingredient,
|
in: String(ingredient.characters),
|
||||||
multFactor: factor,
|
multFactor: factor,
|
||||||
excludedRanges: matches.map({ tuple in tuple.1 })
|
excludedRanges: matches.map({ tuple in tuple.1 })
|
||||||
)
|
)
|
||||||
@@ -125,7 +166,8 @@ class ObservableRecipeDetail: ObservableObject {
|
|||||||
// Sort matches by match range lower bound, descending.
|
// Sort matches by match range lower bound, descending.
|
||||||
matches.sort(by: { a, b in a.1.lowerBound > b.1.lowerBound})
|
matches.sort(by: { a, b in a.1.lowerBound > b.1.lowerBound})
|
||||||
|
|
||||||
var attributedString = AttributedString(ingredient)
|
var attributedString = ingredient
|
||||||
|
|
||||||
for (newSubstring, matchRange) in matches {
|
for (newSubstring, matchRange) in matches {
|
||||||
guard let range = Range(matchRange, in: attributedString) else { continue }
|
guard let range = Range(matchRange, in: attributedString) else { continue }
|
||||||
var attributedSubString = AttributedString(newSubstring)
|
var attributedSubString = AttributedString(newSubstring)
|
||||||
@@ -181,7 +223,7 @@ class ObservableRecipeDetail: ObservableObject {
|
|||||||
}
|
}
|
||||||
return foundMatches
|
return foundMatches
|
||||||
} catch {
|
} catch {
|
||||||
Logger.data.error("Regex error: \(error.localizedDescription)")
|
print("Regex error: \(error.localizedDescription)")
|
||||||
}
|
}
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ struct Recipe: Codable {
|
|||||||
|
|
||||||
|
|
||||||
extension Recipe: Identifiable, Hashable {
|
extension Recipe: Identifiable, Hashable {
|
||||||
var id: Int { recipe_id }
|
var id: String { name }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -38,6 +38,7 @@ struct RecipeDetail: Codable {
|
|||||||
var dateCreated: String?
|
var dateCreated: String?
|
||||||
var dateModified: String?
|
var dateModified: String?
|
||||||
var imageUrl: String?
|
var imageUrl: String?
|
||||||
|
var image: String?
|
||||||
var id: String
|
var id: String
|
||||||
var prepTime: String?
|
var prepTime: String?
|
||||||
var cookTime: String?
|
var cookTime: String?
|
||||||
@@ -51,11 +52,12 @@ struct RecipeDetail: Codable {
|
|||||||
var recipeInstructions: [String]
|
var recipeInstructions: [String]
|
||||||
var nutrition: [String:String]
|
var nutrition: [String:String]
|
||||||
|
|
||||||
init(name: String, keywords: String, dateCreated: String, dateModified: String, imageUrl: String, id: String, prepTime: String? = nil, cookTime: String? = nil, totalTime: String? = nil, description: String, url: String, recipeYield: Int, recipeCategory: String, tool: [String], recipeIngredient: [String], recipeInstructions: [String], nutrition: [String:String]) {
|
init(name: String, keywords: String, dateCreated: String, dateModified: String, image: String, imageUrl: String, id: String, prepTime: String? = nil, cookTime: String? = nil, totalTime: String? = nil, description: String, url: String, recipeYield: Int, recipeCategory: String, tool: [String], recipeIngredient: [String], recipeInstructions: [String], nutrition: [String:String]) {
|
||||||
self.name = name
|
self.name = name
|
||||||
self.keywords = keywords
|
self.keywords = keywords
|
||||||
self.dateCreated = dateCreated
|
self.dateCreated = dateCreated
|
||||||
self.dateModified = dateModified
|
self.dateModified = dateModified
|
||||||
|
self.image = image
|
||||||
self.imageUrl = imageUrl
|
self.imageUrl = imageUrl
|
||||||
self.id = id
|
self.id = id
|
||||||
self.prepTime = prepTime
|
self.prepTime = prepTime
|
||||||
@@ -76,6 +78,7 @@ struct RecipeDetail: Codable {
|
|||||||
keywords = ""
|
keywords = ""
|
||||||
dateCreated = ""
|
dateCreated = ""
|
||||||
dateModified = ""
|
dateModified = ""
|
||||||
|
image = ""
|
||||||
imageUrl = ""
|
imageUrl = ""
|
||||||
id = ""
|
id = ""
|
||||||
prepTime = ""
|
prepTime = ""
|
||||||
@@ -99,61 +102,25 @@ struct RecipeDetail: Codable {
|
|||||||
init(from decoder: Decoder) throws {
|
init(from decoder: Decoder) throws {
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
name = try container.decode(String.self, forKey: .name)
|
name = try container.decode(String.self, forKey: .name)
|
||||||
keywords = (try? container.decode(String.self, forKey: .keywords)) ?? ""
|
keywords = try container.decode(String.self, forKey: .keywords)
|
||||||
dateCreated = try container.decodeIfPresent(String.self, forKey: .dateCreated)
|
dateCreated = try container.decodeIfPresent(String.self, forKey: .dateCreated)
|
||||||
dateModified = try container.decodeIfPresent(String.self, forKey: .dateModified)
|
dateModified = try container.decodeIfPresent(String.self, forKey: .dateModified)
|
||||||
// Server import returns "image"; show/index responses and local storage use "imageUrl"
|
image = try container.decodeIfPresent(String.self, forKey: .image)
|
||||||
imageUrl = (try? container.decodeIfPresent(String.self, forKey: .image))
|
imageUrl = try container.decodeIfPresent(String.self, forKey: .imageUrl)
|
||||||
?? (try? container.decodeIfPresent(String.self, forKey: .imageUrl))
|
|
||||||
id = try container.decode(String.self, forKey: .id)
|
id = try container.decode(String.self, forKey: .id)
|
||||||
prepTime = try container.decodeIfPresent(String.self, forKey: .prepTime)
|
prepTime = try container.decodeIfPresent(String.self, forKey: .prepTime)
|
||||||
cookTime = try container.decodeIfPresent(String.self, forKey: .cookTime)
|
cookTime = try container.decodeIfPresent(String.self, forKey: .cookTime)
|
||||||
totalTime = try container.decodeIfPresent(String.self, forKey: .totalTime)
|
totalTime = try container.decodeIfPresent(String.self, forKey: .totalTime)
|
||||||
description = (try? container.decode(String.self, forKey: .description)) ?? ""
|
description = try container.decode(String.self, forKey: .description)
|
||||||
url = try? container.decode(String.self, forKey: .url)
|
url = try container.decode(String.self, forKey: .url)
|
||||||
recipeCategory = (try? container.decode(String.self, forKey: .recipeCategory)) ?? ""
|
recipeYield = try container.decode(Int?.self, forKey: .recipeYield) ?? 1
|
||||||
|
recipeCategory = try container.decode(String.self, forKey: .recipeCategory)
|
||||||
|
tool = try container.decode([String].self, forKey: .tool)
|
||||||
|
recipeIngredient = try container.decode([String].self, forKey: .recipeIngredient)
|
||||||
|
recipeInstructions = try container.decode([String].self, forKey: .recipeInstructions)
|
||||||
|
|
||||||
// recipeYield: try Int first, then parse leading digits from String
|
nutrition = try container.decode(Dictionary<String, JSONAny>.self, forKey: .nutrition).mapValues { String(describing: $0.value) }
|
||||||
if let yieldInt = try? container.decode(Int.self, forKey: .recipeYield) {
|
|
||||||
recipeYield = yieldInt
|
|
||||||
} else if let yieldString = try? container.decode(String.self, forKey: .recipeYield) {
|
|
||||||
let digits = yieldString.prefix(while: { $0.isNumber })
|
|
||||||
recipeYield = Int(digits) ?? 0
|
|
||||||
} else {
|
|
||||||
recipeYield = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
tool = (try? container.decode([String].self, forKey: .tool)) ?? []
|
|
||||||
recipeIngredient = (try? container.decode([String].self, forKey: .recipeIngredient)) ?? []
|
|
||||||
recipeInstructions = (try? container.decode([String].self, forKey: .recipeInstructions)) ?? []
|
|
||||||
|
|
||||||
if let nutritionDict = try? container.decode(Dictionary<String, JSONAny>.self, forKey: .nutrition) {
|
|
||||||
nutrition = nutritionDict.mapValues { String(describing: $0.value) }
|
|
||||||
} else {
|
|
||||||
nutrition = [:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func encode(to encoder: Encoder) throws {
|
|
||||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
||||||
try container.encode(name, forKey: .name)
|
|
||||||
try container.encode(keywords, forKey: .keywords)
|
|
||||||
try container.encodeIfPresent(dateCreated, forKey: .dateCreated)
|
|
||||||
try container.encodeIfPresent(dateModified, forKey: .dateModified)
|
|
||||||
// Encode under "image" — the key the server expects for create/update
|
|
||||||
try container.encodeIfPresent(imageUrl, forKey: .image)
|
|
||||||
try container.encode(id, forKey: .id)
|
|
||||||
try container.encodeIfPresent(prepTime, forKey: .prepTime)
|
|
||||||
try container.encodeIfPresent(cookTime, forKey: .cookTime)
|
|
||||||
try container.encodeIfPresent(totalTime, forKey: .totalTime)
|
|
||||||
try container.encode(description, forKey: .description)
|
|
||||||
try container.encodeIfPresent(url, forKey: .url)
|
|
||||||
try container.encode(recipeYield, forKey: .recipeYield)
|
|
||||||
try container.encode(recipeCategory, forKey: .recipeCategory)
|
|
||||||
try container.encode(tool, forKey: .tool)
|
|
||||||
try container.encode(recipeIngredient, forKey: .recipeIngredient)
|
|
||||||
try container.encode(recipeInstructions, forKey: .recipeInstructions)
|
|
||||||
try container.encode(nutrition, forKey: .nutrition)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +132,7 @@ extension RecipeDetail {
|
|||||||
keywords: "",
|
keywords: "",
|
||||||
dateCreated: "",
|
dateCreated: "",
|
||||||
dateModified: "",
|
dateModified: "",
|
||||||
|
image: "",
|
||||||
imageUrl: "",
|
imageUrl: "",
|
||||||
id: "",
|
id: "",
|
||||||
prepTime: "",
|
prepTime: "",
|
||||||
|
|||||||
@@ -6,15 +6,14 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
|
|
||||||
extension JSONDecoder {
|
extension JSONDecoder {
|
||||||
static func safeDecode<T: Decodable>(_ data: Data) -> T? {
|
static func safeDecode<T: Decodable>(_ data: Data) -> T? {
|
||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
do {
|
do {
|
||||||
return try decoder.decode(T.self, from: data)
|
return try decoder.decode(T.self, from: data)
|
||||||
} catch {
|
} catch (let error) {
|
||||||
Logger.data.error("JSONDecoder - safeDecode(): \(error.localizedDescription)")
|
print(error)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,7 +24,7 @@ extension JSONEncoder {
|
|||||||
do {
|
do {
|
||||||
return try JSONEncoder().encode(object)
|
return try JSONEncoder().encode(object)
|
||||||
} catch {
|
} catch {
|
||||||
Logger.data.error("JSONEncoder - safeEncode(): Could not encode \(String(describing: T.self))")
|
print("JSONDecoder - safeEncode(): Could not encode object \(T.self)")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,4 @@ extension Logger {
|
|||||||
|
|
||||||
/// Network related logging
|
/// Network related logging
|
||||||
static let network = Logger(subsystem: subsystem, category: "network")
|
static let network = Logger(subsystem: subsystem, category: "network")
|
||||||
|
|
||||||
/// Data/persistence related logging
|
|
||||||
static let data = Logger(subsystem: subsystem, category: "data")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -485,7 +485,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Add" : {
|
"Add" : {
|
||||||
"extractionState" : "stale",
|
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"de" : {
|
"de" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
@@ -663,7 +662,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Bad URL" : {
|
"Bad URL" : {
|
||||||
"extractionState" : "stale",
|
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"de" : {
|
"de" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
@@ -932,7 +930,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Connection error" : {
|
"Connection error" : {
|
||||||
"extractionState" : "stale",
|
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"de" : {
|
"de" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
@@ -2658,7 +2655,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Parsing error" : {
|
"Parsing error" : {
|
||||||
"extractionState" : "stale",
|
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"de" : {
|
"de" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
@@ -2725,7 +2721,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Please check the entered URL." : {
|
"Please check the entered URL." : {
|
||||||
"extractionState" : "stale",
|
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"de" : {
|
"de" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
@@ -3792,7 +3787,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"This website might not be currently supported. If this appears incorrect, you can use the support options in the app settings to raise awareness about this issue." : {
|
"This website might not be currently supported. If this appears incorrect, you can use the support options in the app settings to raise awareness about this issue." : {
|
||||||
"extractionState" : "stale",
|
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"de" : {
|
"de" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
@@ -3837,6 +3831,28 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"To add grocieries manually, type them in the box below and press the button. To add multiple items at once, separate them by a new line." : {
|
||||||
|
"localizations" : {
|
||||||
|
"de" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "Über das Textfeld können Einkäufe manuell hinzugefügt werden. Durch Zeilenumbrüche können mehrere Artikel auf einmal hinzugefügt werden."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"es" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "Para añadir comestibles manualmente, escríbelos en el cuadro de abajo y pulsa el botón.\nPara añadir varios artículos a la vez, sepáralos con una nueva línea."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"fr" : {
|
||||||
|
"stringUnit" : {
|
||||||
|
"state" : "translated",
|
||||||
|
"value" : "Pour ajouter des courses manuellement, tape-les dans la case ci-dessous et appuie sur le bouton.\nPour ajouter plusieurs articles à la fois, sépare-les par un saut de ligne."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"Tool" : {
|
"Tool" : {
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"de" : {
|
"de" : {
|
||||||
@@ -4016,7 +4032,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Unable to load website content. Please check your internet connection." : {
|
"Unable to load website content. Please check your internet connection." : {
|
||||||
"extractionState" : "stale",
|
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"de" : {
|
"de" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
|
|||||||
137
Nextcloud Cookbook iOS Client/Models/RecipeEditViewModel.swift
Normal file
137
Nextcloud Cookbook iOS Client/Models/RecipeEditViewModel.swift
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
//
|
||||||
|
// RecipeEditViewModel.swift
|
||||||
|
// Nextcloud Cookbook iOS Client
|
||||||
|
//
|
||||||
|
// Created by Vincent Meilinger on 11.11.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
@MainActor class RecipeEditViewModel: ObservableObject {
|
||||||
|
@ObservedObject var mainViewModel: AppState
|
||||||
|
@Published var recipe: RecipeDetail = RecipeDetail()
|
||||||
|
|
||||||
|
@Published var prepDuration: DurationComponents = DurationComponents()
|
||||||
|
@Published var cookDuration: DurationComponents = DurationComponents()
|
||||||
|
@Published var totalDuration: DurationComponents = DurationComponents()
|
||||||
|
|
||||||
|
@Published var searchText: String = ""
|
||||||
|
@Published var keywords: [String] = []
|
||||||
|
@Published var keywordSuggestions: [RecipeKeyword] = []
|
||||||
|
|
||||||
|
@Published var showImportSection: Bool = false
|
||||||
|
@Published var importURL: String = ""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var uploadNew: Bool = true
|
||||||
|
var waitingForUpload: Bool = false
|
||||||
|
|
||||||
|
|
||||||
|
init(mainViewModel: AppState, uploadNew: Bool) {
|
||||||
|
self.mainViewModel = mainViewModel
|
||||||
|
self.uploadNew = uploadNew
|
||||||
|
}
|
||||||
|
|
||||||
|
init(mainViewModel: AppState, recipeDetail: RecipeDetail, uploadNew: Bool) {
|
||||||
|
self.mainViewModel = mainViewModel
|
||||||
|
self.recipe = recipeDetail
|
||||||
|
self.uploadNew = uploadNew
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func createRecipe() {
|
||||||
|
self.recipe.prepTime = prepDuration.toPTString()
|
||||||
|
self.recipe.cookTime = cookDuration.toPTString()
|
||||||
|
self.recipe.totalTime = totalDuration.toPTString()
|
||||||
|
self.recipe.setKeywordsFromArray(keywords)
|
||||||
|
}
|
||||||
|
|
||||||
|
func recipeValid() -> RecipeAlert? {
|
||||||
|
// Check if the recipe has a name
|
||||||
|
if recipe.name.replacingOccurrences(of: " ", with: "") == "" {
|
||||||
|
return RecipeAlert.NO_TITLE
|
||||||
|
}
|
||||||
|
// Check if the recipe has a unique name
|
||||||
|
for recipeList in mainViewModel.recipes.values {
|
||||||
|
for r in recipeList {
|
||||||
|
if r.name
|
||||||
|
.replacingOccurrences(of: " ", with: "")
|
||||||
|
.lowercased() ==
|
||||||
|
recipe.name
|
||||||
|
.replacingOccurrences(of: " ", with: "")
|
||||||
|
.lowercased()
|
||||||
|
{
|
||||||
|
return RecipeAlert.DUPLICATE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func uploadNewRecipe() async -> UserAlert? {
|
||||||
|
print("Uploading new recipe.")
|
||||||
|
waitingForUpload = true
|
||||||
|
createRecipe()
|
||||||
|
if let recipeValidationError = recipeValid() {
|
||||||
|
return recipeValidationError
|
||||||
|
}
|
||||||
|
|
||||||
|
return await mainViewModel.uploadRecipe(recipeDetail: self.recipe, createNew: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func uploadEditedRecipe() async -> UserAlert? {
|
||||||
|
waitingForUpload = true
|
||||||
|
print("Uploading changed recipe.")
|
||||||
|
guard let recipeId = Int(recipe.id) else { return RequestAlert.REQUEST_DROPPED }
|
||||||
|
createRecipe()
|
||||||
|
|
||||||
|
return await mainViewModel.uploadRecipe(recipeDetail: self.recipe, createNew: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteRecipe() async -> RequestAlert? {
|
||||||
|
guard let id = Int(recipe.id) else {
|
||||||
|
return .REQUEST_DROPPED
|
||||||
|
}
|
||||||
|
return await mainViewModel.deleteRecipe(withId: id, categoryName: recipe.recipeCategory)
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareView() {
|
||||||
|
if let prepTime = recipe.prepTime {
|
||||||
|
prepDuration.fromPTString(prepTime)
|
||||||
|
}
|
||||||
|
if let cookTime = recipe.cookTime {
|
||||||
|
cookDuration.fromPTString(cookTime)
|
||||||
|
}
|
||||||
|
if let totalTime = recipe.totalTime {
|
||||||
|
totalDuration.fromPTString(totalTime)
|
||||||
|
}
|
||||||
|
self.keywords = recipe.getKeywordsArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
func importRecipe() async -> UserAlert? {
|
||||||
|
let (scrapedRecipe, error) = await mainViewModel.importRecipe(url: importURL)
|
||||||
|
if let scrapedRecipe = scrapedRecipe {
|
||||||
|
self.recipe = scrapedRecipe
|
||||||
|
prepareView()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
let (scrapedRecipe, error) = try await RecipeScraper().scrape(url: importURL)
|
||||||
|
if let scrapedRecipe = scrapedRecipe {
|
||||||
|
self.recipe = scrapedRecipe
|
||||||
|
prepareView()
|
||||||
|
}
|
||||||
|
if let error = error {
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print("Error")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ struct ApiRequest {
|
|||||||
let authString: String?
|
let authString: String?
|
||||||
let headerFields: [HeaderField]
|
let headerFields: [HeaderField]
|
||||||
let body: Data?
|
let body: Data?
|
||||||
|
|
||||||
init(
|
init(
|
||||||
path: String,
|
path: String,
|
||||||
method: RequestMethod,
|
method: RequestMethod,
|
||||||
@@ -28,21 +28,21 @@ struct ApiRequest {
|
|||||||
self.authString = authString
|
self.authString = authString
|
||||||
self.body = body
|
self.body = body
|
||||||
}
|
}
|
||||||
|
|
||||||
func send(pathCompletion: Bool = true) async -> (Data?, NetworkError?) {
|
func send(pathCompletion: Bool = true) async -> (Data?, NetworkError?) {
|
||||||
Logger.network.debug("\(method.rawValue) \(path) sending ...")
|
Logger.network.debug("\(method.rawValue) \(path) sending ...")
|
||||||
|
|
||||||
// Prepare URL
|
// Prepare URL
|
||||||
let urlString = pathCompletion ? UserSettings.shared.serverProtocol + UserSettings.shared.serverAddress + path : path
|
let urlString = pathCompletion ? UserSettings.shared.serverProtocol + UserSettings.shared.serverAddress + path : path
|
||||||
guard var components = URLComponents(string: urlString) else { return (nil, .missingUrl) }
|
print("Full path: \(urlString)")
|
||||||
// Ensure path percent encoding is applied correctly
|
//Logger.network.debug("Full path: \(urlString)")
|
||||||
components.percentEncodedPath = components.path.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? components.path
|
guard let urlStringSanitized = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return (nil, .unknownError) }
|
||||||
guard let url = components.url else { return (nil, .missingUrl) }
|
guard let url = URL(string: urlStringSanitized) else { return (nil, .unknownError) }
|
||||||
|
|
||||||
// Create URL request
|
// Create URL request
|
||||||
var request = URLRequest(url: url)
|
var request = URLRequest(url: url)
|
||||||
request.httpMethod = method.rawValue
|
request.httpMethod = method.rawValue
|
||||||
|
|
||||||
// Set authentication string, if needed
|
// Set authentication string, if needed
|
||||||
if let authString = authString {
|
if let authString = authString {
|
||||||
request.setValue(
|
request.setValue(
|
||||||
@@ -50,7 +50,7 @@ struct ApiRequest {
|
|||||||
forHTTPHeaderField: "Authorization"
|
forHTTPHeaderField: "Authorization"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set other header fields
|
// Set other header fields
|
||||||
for headerField in headerFields {
|
for headerField in headerFields {
|
||||||
request.setValue(
|
request.setValue(
|
||||||
@@ -58,38 +58,46 @@ struct ApiRequest {
|
|||||||
forHTTPHeaderField: headerField.getField()
|
forHTTPHeaderField: headerField.getField()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set http body
|
// Set http body
|
||||||
if let body = body {
|
if let body = body {
|
||||||
request.httpBody = body
|
request.httpBody = body
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for and return data and (decoded) response
|
// Wait for and return data and (decoded) response
|
||||||
|
var data: Data? = nil
|
||||||
|
var response: URLResponse? = nil
|
||||||
do {
|
do {
|
||||||
let (data, response) = try await URLSession.shared.data(for: request)
|
(data, response) = try await URLSession.shared.data(for: request)
|
||||||
if let error = decodeURLResponse(response: response as? HTTPURLResponse, data: data) {
|
Logger.network.debug("\(method.rawValue) \(path) SUCCESS!")
|
||||||
Logger.network.debug("\(method.rawValue) \(path) FAILURE: \(error.localizedDescription)")
|
if let error = decodeURLResponse(response: response as? HTTPURLResponse) {
|
||||||
|
print("\(method.rawValue) \(path) FAILURE: \(error.localizedDescription)")
|
||||||
return (nil, error)
|
return (nil, error)
|
||||||
}
|
}
|
||||||
Logger.network.debug("\(method.rawValue) \(path) SUCCESS!")
|
if let data = data {
|
||||||
return (data, nil)
|
print(data, String(data: data, encoding: .utf8) as Any)
|
||||||
|
return (data, nil)
|
||||||
|
}
|
||||||
|
return (nil, .unknownError)
|
||||||
} catch {
|
} catch {
|
||||||
Logger.network.debug("\(method.rawValue) \(path) FAILURE: \(error.localizedDescription)")
|
let error = decodeURLResponse(response: response as? HTTPURLResponse)
|
||||||
return (nil, .connectionError(underlying: error))
|
Logger.network.debug("\(method.rawValue) \(path) FAILURE: \(error.debugDescription)")
|
||||||
|
return (nil, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func decodeURLResponse(response: HTTPURLResponse?, data: Data?) -> NetworkError? {
|
private func decodeURLResponse(response: HTTPURLResponse?) -> NetworkError? {
|
||||||
guard let response = response else {
|
guard let response = response else {
|
||||||
return .unknownError(detail: "No HTTP response")
|
return NetworkError.unknownError
|
||||||
}
|
}
|
||||||
let statusCode = response.statusCode
|
print("Status code: ", response.statusCode)
|
||||||
switch statusCode {
|
switch response.statusCode {
|
||||||
case 200...299:
|
case 200...299: return (nil)
|
||||||
return nil
|
case 300...399: return (NetworkError.redirectionError)
|
||||||
default:
|
case 400...499: return (NetworkError.clientError)
|
||||||
let body = data.flatMap { String(data: $0, encoding: .utf8) }
|
case 500...599: return (NetworkError.serverError)
|
||||||
return .httpError(statusCode: statusCode, body: body)
|
case 600: return (NetworkError.invalidRequest)
|
||||||
|
default: return (NetworkError.unknownError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,38 +10,169 @@ import OSLog
|
|||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
|
|
||||||
|
/// The Cookbook API class used for requests to the Nextcloud Cookbook service.
|
||||||
|
let cookbookApi: CookbookApi.Type = {
|
||||||
|
switch UserSettings.shared.cookbookApiVersion {
|
||||||
|
case .v1:
|
||||||
|
return CookbookApiV1.self
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
/// The Cookbook API version.
|
/// The Cookbook API version.
|
||||||
enum CookbookApiVersion: String {
|
enum CookbookApiVersion: String {
|
||||||
case v1 = "v1"
|
case v1 = "v1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// A protocol defining common API endpoints for the Cookbook API.
|
/// A protocol defining common API endpoints that are likely to remain the same over future Cookbook API versions.
|
||||||
protocol CookbookApiProtocol {
|
protocol CookbookApi {
|
||||||
func importRecipe(url: String) async throws -> RecipeDetail
|
static var basePath: String { get }
|
||||||
func getImage(id: Int, size: RecipeImage.RecipeImageSize) async throws -> UIImage?
|
|
||||||
func getRecipes() async throws -> [Recipe]
|
/// Not implemented yet.
|
||||||
func createRecipe(_ recipe: RecipeDetail) async throws -> Int
|
static func importRecipe(
|
||||||
func getRecipe(id: Int) async throws -> RecipeDetail
|
auth: String,
|
||||||
func updateRecipe(_ recipe: RecipeDetail) async throws -> Int
|
data: Data
|
||||||
func deleteRecipe(id: Int) async throws
|
) async -> (RecipeDetail?, NetworkError?)
|
||||||
func getCategories() async throws -> [Category]
|
|
||||||
func getCategory(named: String) async throws -> [Recipe]
|
/// Get either the full image or a thumbnail sized version.
|
||||||
func renameCategory(named: String, to newName: String) async throws
|
/// - Parameters:
|
||||||
func getTags() async throws -> [RecipeKeyword]
|
/// - auth: Server authentication string.
|
||||||
func getRecipesTagged(keyword: String) async throws -> [Recipe]
|
/// - id: The according recipe id.
|
||||||
func searchRecipes(query: String) async throws -> [Recipe]
|
/// - size: The size of the image.
|
||||||
|
/// - Returns: The image of the recipe with the specified id. A NetworkError if the request fails, otherwise nil.
|
||||||
|
static func getImage(
|
||||||
|
auth: String,
|
||||||
|
id: Int,
|
||||||
|
size: RecipeImage.RecipeImageSize
|
||||||
|
) async -> (UIImage?, NetworkError?)
|
||||||
|
|
||||||
|
/// Get all recipes.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - Returns: A list of all recipes.
|
||||||
|
static func getRecipes(
|
||||||
|
auth: String
|
||||||
|
) async -> ([Recipe]?, NetworkError?)
|
||||||
|
|
||||||
|
/// Create a new recipe.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - Returns: A NetworkError if the request fails. Nil otherwise.
|
||||||
|
static func createRecipe(
|
||||||
|
auth: String,
|
||||||
|
recipe: RecipeDetail
|
||||||
|
) async -> (NetworkError?)
|
||||||
|
|
||||||
|
/// Get the recipe with the specified id.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - id: The recipe id.
|
||||||
|
/// - Returns: The recipe if it exists. A NetworkError if the request fails.
|
||||||
|
static func getRecipe(
|
||||||
|
auth: String, id: Int
|
||||||
|
) async -> (RecipeDetail?, NetworkError?)
|
||||||
|
|
||||||
|
/// Update an existing recipe with new entries.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - id: The recipe id.
|
||||||
|
/// - Returns: A NetworkError if the request fails. Nil otherwise.
|
||||||
|
static func updateRecipe(
|
||||||
|
auth: String,
|
||||||
|
recipe: RecipeDetail
|
||||||
|
) async -> (NetworkError?)
|
||||||
|
|
||||||
|
/// Delete the recipe with the specified id.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - id: The recipe id.
|
||||||
|
/// - Returns: A NetworkError if the request fails. Nil otherwise.
|
||||||
|
static func deleteRecipe(
|
||||||
|
auth: String,
|
||||||
|
id: Int
|
||||||
|
) async -> (NetworkError?)
|
||||||
|
|
||||||
|
/// Get all categories.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - Returns: A list of categories. A NetworkError if the request fails.
|
||||||
|
static func getCategories(
|
||||||
|
auth: String
|
||||||
|
) async -> ([Category]?, NetworkError?)
|
||||||
|
|
||||||
|
/// Get all recipes of a specified category.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - categoryName: The category name.
|
||||||
|
/// - Returns: A list of recipes. A NetworkError if the request fails.
|
||||||
|
static func getCategory(
|
||||||
|
auth: String,
|
||||||
|
named categoryName: String
|
||||||
|
) async -> ([Recipe]?, NetworkError?)
|
||||||
|
|
||||||
|
/// Rename an existing category.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - categoryName: The name of the category to be renamed.
|
||||||
|
/// - newName: The new category name.
|
||||||
|
/// - Returns: A NetworkError if the request fails.
|
||||||
|
static func renameCategory(
|
||||||
|
auth: String,
|
||||||
|
named categoryName: String,
|
||||||
|
newName: String
|
||||||
|
) async -> (NetworkError?)
|
||||||
|
|
||||||
|
/// Get all keywords/tags.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - Returns: A list of tag strings. A NetworkError if the request fails.
|
||||||
|
static func getTags(
|
||||||
|
auth: String
|
||||||
|
) async -> ([RecipeKeyword]?, NetworkError?)
|
||||||
|
|
||||||
|
/// Get all recipes tagged with the specified keyword.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - keyword: The keyword.
|
||||||
|
/// - Returns: A list of recipes tagged with the specified keyword. A NetworkError if the request fails.
|
||||||
|
static func getRecipesTagged(
|
||||||
|
auth: String,
|
||||||
|
keyword: String
|
||||||
|
) async -> ([Recipe]?, NetworkError?)
|
||||||
|
|
||||||
|
/// Get the servers api version.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string.
|
||||||
|
/// - Returns: A NetworkError if the request fails.
|
||||||
|
static func getApiVersion(
|
||||||
|
auth: String
|
||||||
|
) async -> (NetworkError?)
|
||||||
|
|
||||||
|
/// Trigger a reindexing action on the server.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string
|
||||||
|
/// - Returns: A NetworkError if the request fails.
|
||||||
|
static func postReindex(
|
||||||
|
auth: String
|
||||||
|
) async -> (NetworkError?)
|
||||||
|
|
||||||
|
/// Get the current configuration of the Cookbook server application.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string
|
||||||
|
/// - Returns: A NetworkError if the request fails.
|
||||||
|
static func getConfig(
|
||||||
|
auth: String
|
||||||
|
) async -> (NetworkError?)
|
||||||
|
|
||||||
|
/// Set the current configuration of the Cookbook server application.
|
||||||
|
/// - Parameters:
|
||||||
|
/// - auth: Server authentication string
|
||||||
|
/// - Returns: A NetworkError if the request fails.
|
||||||
|
static func postConfig(
|
||||||
|
auth: String
|
||||||
|
) async -> (NetworkError?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
enum CookbookApiFactory {
|
|
||||||
static func makeClient(
|
|
||||||
version: CookbookApiVersion = UserSettings.shared.cookbookApiVersion,
|
|
||||||
settings: UserSettings = .shared
|
|
||||||
) -> CookbookApiProtocol {
|
|
||||||
switch version {
|
|
||||||
case .v1:
|
|
||||||
return CookbookApiClient(settings: settings)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,155 +6,212 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
|
|
||||||
final class CookbookApiClient: CookbookApiProtocol {
|
class CookbookApiV1: CookbookApi {
|
||||||
private let basePath = "/index.php/apps/cookbook/api/v1"
|
static let basePath: String = "/index.php/apps/cookbook/api/v1"
|
||||||
private let settings: UserSettings
|
|
||||||
|
static func importRecipe(auth: String, data: Data) async -> (RecipeDetail?, NetworkError?) {
|
||||||
private struct RecipeImportRequest: Codable {
|
let request = ApiRequest(
|
||||||
let url: String
|
path: basePath + "/import",
|
||||||
}
|
method: .POST,
|
||||||
|
|
||||||
init(settings: UserSettings = .shared) {
|
|
||||||
self.settings = settings
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Private helpers
|
|
||||||
|
|
||||||
private var auth: String { settings.authString }
|
|
||||||
|
|
||||||
private func makeRequest(
|
|
||||||
path: String,
|
|
||||||
method: RequestMethod,
|
|
||||||
accept: ContentType = .JSON,
|
|
||||||
contentType: ContentType? = nil,
|
|
||||||
body: Data? = nil
|
|
||||||
) -> ApiRequest {
|
|
||||||
var headers = [
|
|
||||||
HeaderField.ocsRequest(value: true),
|
|
||||||
HeaderField.accept(value: accept)
|
|
||||||
]
|
|
||||||
if let contentType = contentType {
|
|
||||||
headers.append(HeaderField.contentType(value: contentType))
|
|
||||||
}
|
|
||||||
return ApiRequest(
|
|
||||||
path: basePath + path,
|
|
||||||
method: method,
|
|
||||||
authString: auth,
|
authString: auth,
|
||||||
headerFields: headers,
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON), HeaderField.contentType(value: .JSON)]
|
||||||
body: body
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
private func sendAndDecode<T: Decodable>(_ request: ApiRequest) async throws -> T {
|
|
||||||
let (data, error) = await request.send()
|
let (data, error) = await request.send()
|
||||||
if let error = error { throw error }
|
guard let data = data else { return (nil, error) }
|
||||||
guard let data = data else { throw NetworkError.unknownError(detail: "No data received") }
|
return (JSONDecoder.safeDecode(data), nil)
|
||||||
guard let decoded: T = JSONDecoder.safeDecode(data) else {
|
|
||||||
throw NetworkError.decodingFailed(detail: "Failed to decode \(T.self)")
|
|
||||||
}
|
|
||||||
return decoded
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func sendRaw(_ request: ApiRequest) async throws -> Data {
|
static func getImage(auth: String, id: Int, size: RecipeImage.RecipeImageSize) async -> (UIImage?, NetworkError?) {
|
||||||
let (data, error) = await request.send()
|
|
||||||
if let error = error { throw error }
|
|
||||||
guard let data = data else { throw NetworkError.unknownError(detail: "No data received") }
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Protocol implementation
|
|
||||||
|
|
||||||
func importRecipe(url: String) async throws -> RecipeDetail {
|
|
||||||
let importRequest = RecipeImportRequest(url: url)
|
|
||||||
guard let body = JSONEncoder.safeEncode(importRequest) else {
|
|
||||||
throw NetworkError.encodingFailed(detail: "Failed to encode import request")
|
|
||||||
}
|
|
||||||
let request = makeRequest(path: "/import", method: .POST, contentType: .JSON, body: body)
|
|
||||||
return try await sendAndDecode(request)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getImage(id: Int, size: RecipeImage.RecipeImageSize) async throws -> UIImage? {
|
|
||||||
let imageSize = (size == .FULL ? "full" : "thumb")
|
let imageSize = (size == .FULL ? "full" : "thumb")
|
||||||
let request = makeRequest(path: "/recipes/\(id)/image?size=\(imageSize)", method: .GET, accept: .IMAGE)
|
let request = ApiRequest(
|
||||||
let data = try await sendRaw(request)
|
path: basePath + "/recipes/\(id)/image?size=\(imageSize)",
|
||||||
return UIImage(data: data)
|
method: .GET,
|
||||||
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .IMAGE)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (nil, error) }
|
||||||
|
return (UIImage(data: data), error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRecipes() async throws -> [Recipe] {
|
static func getRecipes(auth: String) async -> ([Recipe]?, NetworkError?) {
|
||||||
let request = makeRequest(path: "/recipes", method: .GET)
|
let request = ApiRequest(
|
||||||
return try await sendAndDecode(request)
|
path: basePath + "/recipes",
|
||||||
|
method: .GET,
|
||||||
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (nil, error) }
|
||||||
|
print("\n\nRECIPE: ", String(data: data, encoding: .utf8))
|
||||||
|
return (JSONDecoder.safeDecode(data), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRecipe(_ recipe: RecipeDetail) async throws -> Int {
|
static func createRecipe(auth: String, recipe: RecipeDetail) async -> (NetworkError?) {
|
||||||
guard let body = JSONEncoder.safeEncode(recipe) else {
|
guard let recipeData = JSONEncoder.safeEncode(recipe) else {
|
||||||
throw NetworkError.encodingFailed(detail: "Failed to encode recipe")
|
return .dataError
|
||||||
}
|
}
|
||||||
let request = makeRequest(path: "/recipes", method: .POST, contentType: .JSON, body: body)
|
|
||||||
let data = try await sendRaw(request)
|
let request = ApiRequest(
|
||||||
let json = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
|
path: basePath + "/recipes",
|
||||||
if let id = json as? Int {
|
method: .POST,
|
||||||
return id
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON), HeaderField.contentType(value: .JSON)],
|
||||||
|
body: recipeData
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (error) }
|
||||||
|
do {
|
||||||
|
let json = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
|
||||||
|
if let id = json as? Int {
|
||||||
|
return nil
|
||||||
|
} else if let dict = json as? [String: Any] {
|
||||||
|
return .serverError
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
return .decodingFailed
|
||||||
}
|
}
|
||||||
throw NetworkError.decodingFailed(detail: "Expected recipe ID in response")
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRecipe(id: Int) async throws -> RecipeDetail {
|
static func getRecipe(auth: String, id: Int) async -> (RecipeDetail?, NetworkError?) {
|
||||||
let request = makeRequest(path: "/recipes/\(id)", method: .GET)
|
let request = ApiRequest(
|
||||||
return try await sendAndDecode(request)
|
path: basePath + "/recipes/\(id)",
|
||||||
|
method: .GET,
|
||||||
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (nil, error) }
|
||||||
|
return (JSONDecoder.safeDecode(data), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateRecipe(_ recipe: RecipeDetail) async throws -> Int {
|
static func updateRecipe(auth: String, recipe: RecipeDetail) async -> (NetworkError?) {
|
||||||
guard let body = JSONEncoder.safeEncode(recipe) else {
|
guard let recipeData = JSONEncoder.safeEncode(recipe) else {
|
||||||
throw NetworkError.encodingFailed(detail: "Failed to encode recipe")
|
return .dataError
|
||||||
}
|
}
|
||||||
let request = makeRequest(path: "/recipes/\(recipe.id)", method: .PUT, contentType: .JSON, body: body)
|
let request = ApiRequest(
|
||||||
let data = try await sendRaw(request)
|
path: basePath + "/recipes/\(recipe.id)",
|
||||||
let json = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
|
method: .PUT,
|
||||||
if let id = json as? Int {
|
authString: auth,
|
||||||
return id
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON), HeaderField.contentType(value: .JSON)],
|
||||||
|
body: recipeData
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (error) }
|
||||||
|
do {
|
||||||
|
let json = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
|
||||||
|
if let id = json as? Int {
|
||||||
|
return nil
|
||||||
|
} else if let dict = json as? [String: Any] {
|
||||||
|
return .serverError
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
return .decodingFailed
|
||||||
}
|
}
|
||||||
throw NetworkError.decodingFailed(detail: "Expected recipe ID in response")
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteRecipe(id: Int) async throws {
|
static func deleteRecipe(auth: String, id: Int) async -> (NetworkError?) {
|
||||||
let request = makeRequest(path: "/recipes/\(id)", method: .DELETE)
|
let request = ApiRequest(
|
||||||
let _ = try await sendRaw(request)
|
path: basePath + "/recipes/\(id)",
|
||||||
|
method: .DELETE,
|
||||||
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (error) }
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCategories() async throws -> [Category] {
|
static func getCategories(auth: String) async -> ([Category]?, NetworkError?) {
|
||||||
let request = makeRequest(path: "/categories", method: .GET)
|
let request = ApiRequest(
|
||||||
return try await sendAndDecode(request)
|
path: basePath + "/categories",
|
||||||
|
method: .GET,
|
||||||
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (nil, error) }
|
||||||
|
return (JSONDecoder.safeDecode(data), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCategory(named categoryName: String) async throws -> [Recipe] {
|
static func getCategory(auth: String, named categoryName: String) async -> ([Recipe]?, NetworkError?) {
|
||||||
let request = makeRequest(path: "/category/\(categoryName)", method: .GET)
|
let request = ApiRequest(
|
||||||
return try await sendAndDecode(request)
|
path: basePath + "/category/\(categoryName)",
|
||||||
|
method: .GET,
|
||||||
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (nil, error) }
|
||||||
|
return (JSONDecoder.safeDecode(data), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func renameCategory(named categoryName: String, to newName: String) async throws {
|
static func renameCategory(auth: String, named categoryName: String, newName: String) async -> (NetworkError?) {
|
||||||
guard let body = JSONEncoder.safeEncode(["name": newName]) else {
|
let request = ApiRequest(
|
||||||
throw NetworkError.encodingFailed(detail: "Failed to encode category name")
|
path: basePath + "/category/\(categoryName)",
|
||||||
}
|
method: .PUT,
|
||||||
let request = makeRequest(path: "/category/\(categoryName)", method: .PUT, contentType: .JSON, body: body)
|
authString: auth,
|
||||||
let _ = try await sendRaw(request)
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (error) }
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTags() async throws -> [RecipeKeyword] {
|
static func getTags(auth: String) async -> ([RecipeKeyword]?, NetworkError?) {
|
||||||
let request = makeRequest(path: "/keywords", method: .GET)
|
let request = ApiRequest(
|
||||||
return try await sendAndDecode(request)
|
path: basePath + "/keywords",
|
||||||
|
method: .GET,
|
||||||
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (nil, error) }
|
||||||
|
return (JSONDecoder.safeDecode(data), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRecipesTagged(keyword: String) async throws -> [Recipe] {
|
static func getRecipesTagged(auth: String, keyword: String) async -> ([Recipe]?, NetworkError?) {
|
||||||
let request = makeRequest(path: "/tags/\(keyword)", method: .GET)
|
let request = ApiRequest(
|
||||||
return try await sendAndDecode(request)
|
path: basePath + "/tags/\(keyword)",
|
||||||
|
method: .GET,
|
||||||
|
authString: auth,
|
||||||
|
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)]
|
||||||
|
)
|
||||||
|
|
||||||
|
let (data, error) = await request.send()
|
||||||
|
guard let data = data else { return (nil, error) }
|
||||||
|
return (JSONDecoder.safeDecode(data), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func searchRecipes(query: String) async throws -> [Recipe] {
|
static func getApiVersion(auth: String) async -> (NetworkError?) {
|
||||||
let request = makeRequest(path: "/search/\(query)", method: .GET)
|
return .none
|
||||||
return try await sendAndDecode(request)
|
}
|
||||||
|
|
||||||
|
static func postReindex(auth: String) async -> (NetworkError?) {
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
|
||||||
|
static func getConfig(auth: String) async -> (NetworkError?) {
|
||||||
|
return .none
|
||||||
|
}
|
||||||
|
|
||||||
|
static func postConfig(auth: String) async -> (NetworkError?) {
|
||||||
|
return .none
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,45 +7,17 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public enum NetworkError: Error, LocalizedError {
|
public enum NetworkError: String, Error {
|
||||||
case missingUrl
|
case missingUrl = "Missing URL."
|
||||||
case encodingFailed(detail: String? = nil)
|
case parametersNil = "Parameters are nil."
|
||||||
case decodingFailed(detail: String? = nil)
|
case encodingFailed = "Parameter encoding failed."
|
||||||
case httpError(statusCode: Int, body: String? = nil)
|
case decodingFailed = "Data decoding failed."
|
||||||
case connectionError(underlying: Error? = nil)
|
case redirectionError = "Redirection error"
|
||||||
case invalidRequest
|
case clientError = "Client error"
|
||||||
case unknownError(detail: String? = nil)
|
case serverError = "Server error"
|
||||||
|
case invalidRequest = "Invalid request"
|
||||||
public var errorDescription: String? {
|
case unknownError = "Unknown error"
|
||||||
switch self {
|
case dataError = "Invalid data error."
|
||||||
case .missingUrl:
|
|
||||||
return "Missing URL."
|
|
||||||
case .encodingFailed(let detail):
|
|
||||||
return "Parameter encoding failed." + (detail.map { " \($0)" } ?? "")
|
|
||||||
case .decodingFailed(let detail):
|
|
||||||
return "Data decoding failed." + (detail.map { " \($0)" } ?? "")
|
|
||||||
case .httpError(let statusCode, let body):
|
|
||||||
return "HTTP error \(statusCode)." + (body.map { " \($0)" } ?? "")
|
|
||||||
case .connectionError(let underlying):
|
|
||||||
return "Connection error." + (underlying.map { " \($0.localizedDescription)" } ?? "")
|
|
||||||
case .invalidRequest:
|
|
||||||
return "Invalid request."
|
|
||||||
case .unknownError(let detail):
|
|
||||||
return "Unknown error." + (detail.map { " \($0)" } ?? "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var isClientError: Bool {
|
|
||||||
if case .httpError(let statusCode, _) = self {
|
|
||||||
return (400...499).contains(statusCode)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
var isServerError: Bool {
|
|
||||||
if case .httpError(let statusCode, _) = self {
|
|
||||||
return (500...599).contains(statusCode)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -44,3 +44,7 @@ struct HeaderField {
|
|||||||
return HeaderField(_field: "Content-Type", _value: value.rawValue)
|
return HeaderField(_field: "Content-Type", _value: value.rawValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct RecipeImportRequest: Codable {
|
||||||
|
let url: String
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
/// The `NextcloudApi` class provides functionalities to interact with the Nextcloud API, particularly for user authentication.
|
/// The `NextcloudApi` class provides functionalities to interact with the Nextcloud API, particularly for user authentication.
|
||||||
@@ -34,10 +33,10 @@ class NextcloudApi {
|
|||||||
return (nil, error)
|
return (nil, error)
|
||||||
}
|
}
|
||||||
guard let data = data else {
|
guard let data = data else {
|
||||||
return (nil, NetworkError.encodingFailed())
|
return (nil, NetworkError.dataError)
|
||||||
}
|
}
|
||||||
guard let loginRequest: LoginV2Request = JSONDecoder.safeDecode(data) else {
|
guard let loginRequest: LoginV2Request = JSONDecoder.safeDecode(data) else {
|
||||||
return (nil, NetworkError.decodingFailed())
|
return (nil, NetworkError.decodingFailed)
|
||||||
}
|
}
|
||||||
return (loginRequest, nil)
|
return (loginRequest, nil)
|
||||||
}
|
}
|
||||||
@@ -70,10 +69,10 @@ class NextcloudApi {
|
|||||||
return (nil, error)
|
return (nil, error)
|
||||||
}
|
}
|
||||||
guard let data = data else {
|
guard let data = data else {
|
||||||
return (nil, NetworkError.encodingFailed())
|
return (nil, NetworkError.dataError)
|
||||||
}
|
}
|
||||||
guard let loginResponse: LoginV2Response = JSONDecoder.safeDecode(data) else {
|
guard let loginResponse: LoginV2Response = JSONDecoder.safeDecode(data) else {
|
||||||
return (nil, NetworkError.decodingFailed())
|
return (nil, NetworkError.decodingFailed)
|
||||||
}
|
}
|
||||||
return (loginResponse, nil)
|
return (loginResponse, nil)
|
||||||
}
|
}
|
||||||
@@ -108,11 +107,11 @@ class NextcloudApi {
|
|||||||
userId: data?["userId"] as? String ?? "",
|
userId: data?["userId"] as? String ?? "",
|
||||||
userDisplayName: data?["displayName"] as? String ?? ""
|
userDisplayName: data?["displayName"] as? String ?? ""
|
||||||
)
|
)
|
||||||
Logger.network.debug("Loaded hover card for user \(userData.userId)")
|
print(userData)
|
||||||
return (userData, nil)
|
return (userData, nil)
|
||||||
} catch {
|
} catch {
|
||||||
Logger.network.error("Failed to decode hover card: \(error.localizedDescription)")
|
print(error.localizedDescription)
|
||||||
return (nil, NetworkError.decodingFailed())
|
return (nil, NetworkError.decodingFailed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
128
Nextcloud Cookbook iOS Client/RecipeImport/RecipeScraper.swift
Normal file
128
Nextcloud Cookbook iOS Client/RecipeImport/RecipeScraper.swift
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
//
|
||||||
|
// RecipeScraper.swift
|
||||||
|
// Nextcloud Cookbook iOS Client
|
||||||
|
//
|
||||||
|
// Created by Vincent Meilinger on 09.11.23.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import SwiftSoup
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
|
||||||
|
class RecipeScraper {
|
||||||
|
func scrape(url: String) async throws -> (RecipeDetail?, RecipeImportAlert?) {
|
||||||
|
var contents: String? = nil
|
||||||
|
if let url = URL(string: url) {
|
||||||
|
do {
|
||||||
|
contents = try String(contentsOf: url)
|
||||||
|
} catch {
|
||||||
|
print("ERROR: Could not load url content.")
|
||||||
|
return (nil, .CHECK_CONNECTION)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
print("ERROR: Bad url.")
|
||||||
|
return (nil, .BAD_URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let html = contents else {
|
||||||
|
print("ERROR: no contents")
|
||||||
|
return (nil, .WEBSITE_NOT_SUPPORTED)
|
||||||
|
}
|
||||||
|
let doc = try SwiftSoup.parse(html)
|
||||||
|
|
||||||
|
let elements: Elements = try doc.select("script")
|
||||||
|
for elem in elements.array() {
|
||||||
|
for attr in elem.getAttributes()!.asList() {
|
||||||
|
if attr.getValue() == "application/ld+json" {
|
||||||
|
guard let dict = toDict(elem) else { continue }
|
||||||
|
if let recipe = getRecipe(fromDict: dict) {
|
||||||
|
return (recipe, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (nil, .WEBSITE_NOT_SUPPORTED)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private func toDict(_ elem: Element) -> [String: Any]? {
|
||||||
|
var recipeDict: [String: Any]? = nil
|
||||||
|
do {
|
||||||
|
let jsonString = try elem.html()
|
||||||
|
let json = try JSONSerialization.jsonObject(with: jsonString.data(using: .utf8)!, options: .fragmentsAllowed)
|
||||||
|
if let recipe = json as? [String : Any] {
|
||||||
|
recipeDict = recipe
|
||||||
|
} else if let recipe = (json as! [Any])[0] as? [String : Any] {
|
||||||
|
recipeDict = recipe
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print("Unable to decode json")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let recipeDict = recipeDict else {
|
||||||
|
print("Json is not a dict")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if recipeDict["@type"] as? String ?? "" == "Recipe" {
|
||||||
|
return recipeDict
|
||||||
|
} else if (recipeDict["@type"] as? [String] ?? []).contains("Recipe") {
|
||||||
|
return recipeDict
|
||||||
|
} else {
|
||||||
|
print("Json dict is not a recipe ...")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func getRecipe(fromDict recipe: Dictionary<String, Any>) -> RecipeDetail? {
|
||||||
|
|
||||||
|
var recipeDetail = RecipeDetail()
|
||||||
|
recipeDetail.name = recipe["name"] as? String ?? "New Recipe"
|
||||||
|
recipeDetail.recipeCategory = recipe["recipeCategory"] as? String ?? ""
|
||||||
|
recipeDetail.keywords = joinedStringForKey("keywords", dict: recipe)
|
||||||
|
recipeDetail.description = recipe["description"] as? String ?? ""
|
||||||
|
recipeDetail.dateCreated = recipe["dateCreated"] as? String ?? ""
|
||||||
|
recipeDetail.dateModified = recipe["dateModified"] as? String ?? ""
|
||||||
|
recipeDetail.imageUrl = recipe["imageUrl"] as? String ?? ""
|
||||||
|
recipeDetail.url = recipe["url"] as? String ?? ""
|
||||||
|
recipeDetail.cookTime = recipe["cookTime"] as? String ?? ""
|
||||||
|
recipeDetail.prepTime = recipe["prepTime"] as? String ?? ""
|
||||||
|
recipeDetail.totalTime = recipe["totalTime"] as? String ?? ""
|
||||||
|
recipeDetail.recipeInstructions = stringArrayForKey("recipeInstructions", dict: recipe)
|
||||||
|
recipeDetail.recipeYield = recipe["recipeYield"] as? Int ?? 0
|
||||||
|
recipeDetail.recipeIngredient = recipe["recipeIngredient"] as? [String] ?? []
|
||||||
|
recipeDetail.tool = stringArrayForKey("tool", dict: recipe)
|
||||||
|
recipeDetail.nutrition = recipe["nutrition"] as? [String:String] ?? [:]
|
||||||
|
print(recipeDetail)
|
||||||
|
return recipeDetail
|
||||||
|
}
|
||||||
|
|
||||||
|
private func stringArrayForKey(_ key: String, dict: Dictionary<String, Any>) -> [String] {
|
||||||
|
if let text = dict[key] as? String {
|
||||||
|
return [text]
|
||||||
|
} else if let value = dict[key] as? [String] {
|
||||||
|
return value
|
||||||
|
} else if let orderedList = dict[key] as? [Any] {
|
||||||
|
var entries: [String] = []
|
||||||
|
for dict in orderedList {
|
||||||
|
guard let dict = dict as? [String: Any] else { continue }
|
||||||
|
guard let text = dict["text"] as? String else { continue }
|
||||||
|
entries.append(text)
|
||||||
|
}
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
private func joinedStringForKey(_ key: String, dict: Dictionary<String, Any>) -> String {
|
||||||
|
if let value = dict[key] as? [String] {
|
||||||
|
return value.joined(separator: ",")
|
||||||
|
} else if let value = dict[key] as? String {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import SwiftSoup
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//let url = "https://www.chefkoch.de/rezepte/1385981243676608/Knusprige-Entenbrust.html"
|
||||||
|
let url = "https://www.allrecipes.com/recipe/234620/mascarpone-mashed-potatoes/"
|
||||||
|
|
||||||
|
let scraper = RecipeScaper()
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
|
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
|
||||||
|
<timeline fileName='timeline.xctimeline'/>
|
||||||
|
</playground>
|
||||||
@@ -94,6 +94,33 @@ enum RecipeAlert: UserAlert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum RecipeImportAlert: UserAlert {
|
||||||
|
case BAD_URL,
|
||||||
|
CHECK_CONNECTION,
|
||||||
|
WEBSITE_NOT_SUPPORTED
|
||||||
|
|
||||||
|
var localizedDescription: LocalizedStringKey {
|
||||||
|
switch self {
|
||||||
|
case .BAD_URL: return "Please check the entered URL."
|
||||||
|
case .CHECK_CONNECTION: return "Unable to load website content. Please check your internet connection."
|
||||||
|
case .WEBSITE_NOT_SUPPORTED: return "This website might not be currently supported. If this appears incorrect, you can use the support options in the app settings to raise awareness about this issue."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var localizedTitle: LocalizedStringKey {
|
||||||
|
switch self {
|
||||||
|
case .BAD_URL: return "Bad URL"
|
||||||
|
case .CHECK_CONNECTION: return "Connection error"
|
||||||
|
case .WEBSITE_NOT_SUPPORTED: return "Parsing error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var alertButtons: [AlertButton] {
|
||||||
|
return [.OK]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
enum RequestAlert: UserAlert {
|
enum RequestAlert: UserAlert {
|
||||||
case REQUEST_DELAYED,
|
case REQUEST_DELAYED,
|
||||||
REQUEST_DROPPED,
|
REQUEST_DROPPED,
|
||||||
|
|||||||
@@ -10,40 +10,46 @@ import SwiftUI
|
|||||||
struct MainView: View {
|
struct MainView: View {
|
||||||
@StateObject var appState = AppState()
|
@StateObject var appState = AppState()
|
||||||
@StateObject var groceryList = GroceryList()
|
@StateObject var groceryList = GroceryList()
|
||||||
|
|
||||||
// Tab ViewModels
|
// Tab ViewModels
|
||||||
@StateObject var recipeViewModel = RecipeTabView.ViewModel()
|
@StateObject var recipeViewModel = RecipeTabView.ViewModel()
|
||||||
@StateObject var searchViewModel = SearchTabView.ViewModel()
|
@StateObject var searchViewModel = SearchTabView.ViewModel()
|
||||||
|
|
||||||
@State private var selectedTab: Tab = .recipes
|
|
||||||
|
|
||||||
enum Tab {
|
enum Tab {
|
||||||
case recipes, search, groceryList
|
case recipes, search, groceryList
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
TabView(selection: $selectedTab) {
|
TabView {
|
||||||
SwiftUI.Tab("Recipes", systemImage: "book.closed.fill", value: .recipes) {
|
RecipeTabView()
|
||||||
RecipeTabView()
|
.environmentObject(recipeViewModel)
|
||||||
.environmentObject(recipeViewModel)
|
.environmentObject(appState)
|
||||||
.environmentObject(appState)
|
.environmentObject(groceryList)
|
||||||
.environmentObject(groceryList)
|
.tabItem {
|
||||||
}
|
Label("Recipes", systemImage: "book.closed.fill")
|
||||||
|
}
|
||||||
SwiftUI.Tab("Search", systemImage: "magnifyingglass", value: .search, role: .search) {
|
.tag(Tab.recipes)
|
||||||
SearchTabView()
|
|
||||||
.environmentObject(searchViewModel)
|
SearchTabView()
|
||||||
.environmentObject(appState)
|
.environmentObject(searchViewModel)
|
||||||
.environmentObject(groceryList)
|
.environmentObject(appState)
|
||||||
}
|
.environmentObject(groceryList)
|
||||||
|
.tabItem {
|
||||||
SwiftUI.Tab("Grocery List", systemImage: "storefront", value: .groceryList) {
|
Label("Search", systemImage: "magnifyingglass")
|
||||||
GroceryListTabView()
|
}
|
||||||
.environmentObject(groceryList)
|
.tag(Tab.search)
|
||||||
}
|
|
||||||
|
GroceryListTabView()
|
||||||
|
.environmentObject(groceryList)
|
||||||
|
.tabItem {
|
||||||
|
if #available(iOS 17.0, *) {
|
||||||
|
Label("Grocery List", systemImage: "storefront")
|
||||||
|
} else {
|
||||||
|
Label("Grocery List", systemImage: "heart.text.square")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.tag(Tab.groceryList)
|
||||||
}
|
}
|
||||||
.tabViewStyle(.sidebarAdaptable)
|
|
||||||
.modifier(TabBarMinimizeModifier())
|
|
||||||
.task {
|
.task {
|
||||||
recipeViewModel.presentLoadingIndicator = true
|
recipeViewModel.presentLoadingIndicator = true
|
||||||
await appState.getCategories()
|
await appState.getCategories()
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct OnboardingView: View {
|
struct OnboardingView: View {
|
||||||
@@ -149,7 +148,7 @@ struct BorderedLoginTextField: View {
|
|||||||
.autocorrectionDisabled()
|
.autocorrectionDisabled()
|
||||||
.textInputAutocapitalization(.never)
|
.textInputAutocapitalization(.never)
|
||||||
.foregroundColor(color)
|
.foregroundColor(color)
|
||||||
.tint(color)
|
.accentColor(color)
|
||||||
.padding()
|
.padding()
|
||||||
.background(
|
.background(
|
||||||
RoundedRectangle(cornerRadius: 10)
|
RoundedRectangle(cornerRadius: 10)
|
||||||
@@ -171,7 +170,7 @@ struct LoginTextField: View {
|
|||||||
.autocorrectionDisabled()
|
.autocorrectionDisabled()
|
||||||
.textInputAutocapitalization(.never)
|
.textInputAutocapitalization(.never)
|
||||||
.foregroundColor(color)
|
.foregroundColor(color)
|
||||||
.tint(color)
|
.accentColor(color)
|
||||||
.padding()
|
.padding()
|
||||||
.background(
|
.background(
|
||||||
RoundedRectangle(cornerRadius: 10)
|
RoundedRectangle(cornerRadius: 10)
|
||||||
@@ -204,7 +203,7 @@ struct ServerAddressField: View {
|
|||||||
.tint(.white)
|
.tint(.white)
|
||||||
.font(.headline)
|
.font(.headline)
|
||||||
.onChange(of: serverProtocol) { value in
|
.onChange(of: serverProtocol) { value in
|
||||||
Logger.view.debug("\(value.rawValue)")
|
print(value)
|
||||||
userSettings.serverProtocol = value.rawValue
|
userSettings.serverProtocol = value.rawValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
|
||||||
@@ -73,7 +72,7 @@ struct TokenLoginView: View {
|
|||||||
case .username:
|
case .username:
|
||||||
focusedField = .token
|
focusedField = .token
|
||||||
default:
|
default:
|
||||||
Logger.view.debug("Attempting to log in ...")
|
print("Attempting to log in ...")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,16 +87,21 @@ struct TokenLoginView: View {
|
|||||||
showAlert = true
|
showAlert = true
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
UserSettings.shared.setAuthString()
|
UserSettings.shared.setAuthString()
|
||||||
let client = CookbookApiFactory.makeClient()
|
let (data, error) = await cookbookApi.getCategories(auth: UserSettings.shared.authString)
|
||||||
do {
|
|
||||||
let _ = try await client.getCategories()
|
if let error = error {
|
||||||
return true
|
|
||||||
} catch {
|
|
||||||
alertMessage = "Login failed. Please check your inputs and internet connection."
|
alertMessage = "Login failed. Please check your inputs and internet connection."
|
||||||
showAlert = true
|
showAlert = true
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
guard let data = data else {
|
||||||
|
alertMessage = "Login failed. Please check your inputs."
|
||||||
|
showAlert = true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import WebKit
|
import WebKit
|
||||||
|
|
||||||
@@ -83,7 +82,7 @@ struct V2LoginView: View {
|
|||||||
Task {
|
Task {
|
||||||
let error = await sendLoginV2Request()
|
let error = await sendLoginV2Request()
|
||||||
if let error = error {
|
if let error = error {
|
||||||
alertMessage = "A network error occured (\(error.localizedDescription))."
|
alertMessage = "A network error occured (\(error.rawValue))."
|
||||||
showAlert = true
|
showAlert = true
|
||||||
}
|
}
|
||||||
if let loginRequest = loginRequest {
|
if let loginRequest = loginRequest {
|
||||||
@@ -152,13 +151,13 @@ struct V2LoginView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func fetchLoginV2Response() async -> (LoginV2Response?, NetworkError?) {
|
func fetchLoginV2Response() async -> (LoginV2Response?, NetworkError?) {
|
||||||
guard let loginRequest = loginRequest else { return (nil, .invalidRequest) }
|
guard let loginRequest = loginRequest else { return (nil, .parametersNil) }
|
||||||
return await NextcloudApi.loginV2Response(req: loginRequest)
|
return await NextcloudApi.loginV2Response(req: loginRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkLogin(response: LoginV2Response?, error: NetworkError?) {
|
func checkLogin(response: LoginV2Response?, error: NetworkError?) {
|
||||||
if let error = error {
|
if let error = error {
|
||||||
alertMessage = "Login failed. Please login via the browser and try again. (\(error.localizedDescription))"
|
alertMessage = "Login failed. Please login via the browser and try again. (\(error.rawValue))"
|
||||||
showAlert = true
|
showAlert = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -167,7 +166,7 @@ struct V2LoginView: View {
|
|||||||
showAlert = true
|
showAlert = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Logger.network.debug("Login successful for user \(response.loginName)!")
|
print("Login successful for user \(response.loginName)!")
|
||||||
UserSettings.shared.username = response.loginName
|
UserSettings.shared.username = response.loginName
|
||||||
UserSettings.shared.token = response.appPassword
|
UserSettings.shared.token = response.appPassword
|
||||||
let loginString = "\(UserSettings.shared.username):\(UserSettings.shared.token)"
|
let loginString = "\(UserSettings.shared.username):\(UserSettings.shared.token)"
|
||||||
@@ -188,17 +187,12 @@ struct WebViewSheet: View {
|
|||||||
@State var url: String
|
@State var url: String
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationView {
|
||||||
WebView(url: URL(string: url)!)
|
WebView(url: URL(string: url)!)
|
||||||
.navigationTitle("Nextcloud Login")
|
.navigationBarTitle(Text("Nextcloud Login"), displayMode: .inline)
|
||||||
.navigationBarTitleDisplayMode(.inline)
|
.navigationBarItems(trailing: Button("Done") {
|
||||||
.toolbar {
|
dismiss()
|
||||||
ToolbarItem(placement: .topBarTrailing) {
|
})
|
||||||
Button("Done") {
|
|
||||||
dismiss()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import SwiftUI
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct RecipeListView: View {
|
struct RecipeListView: View {
|
||||||
@EnvironmentObject var appState: AppState
|
@EnvironmentObject var appState: AppState
|
||||||
@EnvironmentObject var groceryList: GroceryList
|
@EnvironmentObject var groceryList: GroceryList
|
||||||
@@ -65,6 +64,7 @@ struct RecipeListView: View {
|
|||||||
.toolbar {
|
.toolbar {
|
||||||
ToolbarItem(placement: .topBarTrailing) {
|
ToolbarItem(placement: .topBarTrailing) {
|
||||||
Button {
|
Button {
|
||||||
|
print("Add new recipe")
|
||||||
showEditView = true
|
showEditView = true
|
||||||
} label: {
|
} label: {
|
||||||
Image(systemName: "plus.circle.fill")
|
Image(systemName: "plus.circle.fill")
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
|
||||||
@@ -292,17 +291,22 @@ extension RecipeView {
|
|||||||
let (scrapedRecipe, error) = await appState.importRecipe(url: url)
|
let (scrapedRecipe, error) = await appState.importRecipe(url: url)
|
||||||
if let scrapedRecipe = scrapedRecipe {
|
if let scrapedRecipe = scrapedRecipe {
|
||||||
viewModel.setupView(recipeDetail: scrapedRecipe)
|
viewModel.setupView(recipeDetail: scrapedRecipe)
|
||||||
// Fetch the image from the server if the import created a recipe with a valid id
|
|
||||||
if let recipeId = Int(scrapedRecipe.id), recipeId > 0 {
|
|
||||||
viewModel.recipeImage = await appState.getImage(
|
|
||||||
id: recipeId,
|
|
||||||
size: .FULL,
|
|
||||||
fetchMode: .onlyServer
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return error
|
|
||||||
|
do {
|
||||||
|
let (scrapedRecipe, error) = try await RecipeScraper().scrape(url: url)
|
||||||
|
if let scrapedRecipe = scrapedRecipe {
|
||||||
|
viewModel.setupView(recipeDetail: scrapedRecipe)
|
||||||
|
}
|
||||||
|
if let error = error {
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
print("Error")
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -367,7 +371,7 @@ struct RecipeViewToolBar: ToolbarContent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Button {
|
Button {
|
||||||
Logger.view.debug("Sharing recipe ...")
|
print("Sharing recipe ...")
|
||||||
viewModel.presentShareSheet = true
|
viewModel.presentShareSheet = true
|
||||||
} label: {
|
} label: {
|
||||||
Label("Share Recipe", systemImage: "square.and.arrow.up")
|
Label("Share Recipe", systemImage: "square.and.arrow.up")
|
||||||
@@ -382,70 +386,34 @@ struct RecipeViewToolBar: ToolbarContent {
|
|||||||
|
|
||||||
func handleUpload() async {
|
func handleUpload() async {
|
||||||
if viewModel.newRecipe {
|
if viewModel.newRecipe {
|
||||||
// Check if the recipe was already created on the server by import
|
print("Uploading new recipe.")
|
||||||
let importedId = Int(viewModel.observableRecipeDetail.id) ?? 0
|
if let recipeValidationError = recipeValid() {
|
||||||
let alreadyCreatedByImport = importedId > 0
|
viewModel.presentAlert(recipeValidationError)
|
||||||
|
return
|
||||||
if alreadyCreatedByImport {
|
}
|
||||||
Logger.view.debug("Uploading changes to imported recipe (id: \(importedId)).")
|
|
||||||
let (_, alert) = await appState.uploadRecipe(recipeDetail: viewModel.observableRecipeDetail.toRecipeDetail(), createNew: false)
|
if let alert = await appState.uploadRecipe(recipeDetail: viewModel.observableRecipeDetail.toRecipeDetail(), createNew: true) {
|
||||||
if let alert {
|
viewModel.presentAlert(alert)
|
||||||
viewModel.presentAlert(alert)
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Logger.view.debug("Uploading new recipe.")
|
|
||||||
if let recipeValidationError = recipeValid() {
|
|
||||||
viewModel.presentAlert(recipeValidationError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let (newId, alert) = await appState.uploadRecipe(recipeDetail: viewModel.observableRecipeDetail.toRecipeDetail(), createNew: true)
|
|
||||||
if let alert {
|
|
||||||
viewModel.presentAlert(alert)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if let newId {
|
|
||||||
viewModel.observableRecipeDetail.id = String(newId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Logger.view.debug("Uploading changed recipe.")
|
print("Uploading changed recipe.")
|
||||||
|
|
||||||
guard let _ = Int(viewModel.observableRecipeDetail.id) else {
|
guard let _ = Int(viewModel.observableRecipeDetail.id) else {
|
||||||
viewModel.presentAlert(RequestAlert.REQUEST_DROPPED)
|
viewModel.presentAlert(RequestAlert.REQUEST_DROPPED)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let (_, alert) = await appState.uploadRecipe(recipeDetail: viewModel.observableRecipeDetail.toRecipeDetail(), createNew: false)
|
if let alert = await appState.uploadRecipe(recipeDetail: viewModel.observableRecipeDetail.toRecipeDetail(), createNew: false) {
|
||||||
if let alert {
|
|
||||||
viewModel.presentAlert(alert)
|
viewModel.presentAlert(alert)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await appState.getCategories()
|
await appState.getCategories()
|
||||||
let newCategory = viewModel.observableRecipeDetail.recipeCategory.isEmpty ? "*" : viewModel.observableRecipeDetail.recipeCategory
|
await appState.getCategory(named: viewModel.observableRecipeDetail.recipeCategory, fetchMode: .preferServer)
|
||||||
let oldCategory = viewModel.recipeDetail.recipeCategory.isEmpty ? "*" : viewModel.recipeDetail.recipeCategory
|
|
||||||
await appState.getCategory(named: newCategory, fetchMode: .preferServer)
|
|
||||||
if oldCategory != newCategory {
|
|
||||||
await appState.getCategory(named: oldCategory, fetchMode: .preferServer)
|
|
||||||
}
|
|
||||||
if let id = Int(viewModel.observableRecipeDetail.id) {
|
if let id = Int(viewModel.observableRecipeDetail.id) {
|
||||||
let _ = await appState.getRecipe(id: id, fetchMode: .onlyServer, save: true)
|
let _ = await appState.getRecipe(id: id, fetchMode: .onlyServer, save: true)
|
||||||
// Fetch the image after upload so it displays in view mode
|
|
||||||
viewModel.recipeImage = await appState.getImage(id: id, size: .FULL, fetchMode: .onlyServer)
|
|
||||||
// Update recipe reference so the view tracks the server-assigned id
|
|
||||||
viewModel.recipe = Recipe(
|
|
||||||
name: viewModel.observableRecipeDetail.name,
|
|
||||||
keywords: viewModel.observableRecipeDetail.keywords.joined(separator: ","),
|
|
||||||
dateCreated: "",
|
|
||||||
dateModified: "",
|
|
||||||
imageUrl: viewModel.observableRecipeDetail.imageUrl,
|
|
||||||
imagePlaceholderUrl: "",
|
|
||||||
recipe_id: id
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
viewModel.newRecipe = false
|
|
||||||
viewModel.editMode = false
|
viewModel.editMode = false
|
||||||
viewModel.presentAlert(RecipeAlert.UPLOAD_SUCCESS)
|
viewModel.presentAlert(RecipeAlert.UPLOAD_SUCCESS)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ struct RecipeListSection: View {
|
|||||||
ForEach(list, id: \.self) { item in
|
ForEach(list, id: \.self) { item in
|
||||||
HStack(alignment: .top) {
|
HStack(alignment: .top) {
|
||||||
Text("\u{2022}")
|
Text("\u{2022}")
|
||||||
Text("\(item)")
|
Text(ObservableRecipeDetail.applyMarkdownStyling(item))
|
||||||
.multilineTextAlignment(.leading)
|
.multilineTextAlignment(.leading)
|
||||||
}
|
}
|
||||||
.padding(4)
|
.padding(4)
|
||||||
@@ -53,7 +53,7 @@ struct EditableText: View {
|
|||||||
.textFieldStyle(.roundedBorder)
|
.textFieldStyle(.roundedBorder)
|
||||||
.lineLimit(lineLimit)
|
.lineLimit(lineLimit)
|
||||||
} else {
|
} else {
|
||||||
Text(text)
|
Text(ObservableRecipeDetail.applyMarkdownStyling(text))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ struct EditableListView: View {
|
|||||||
@State var axis: Axis = .vertical
|
@State var axis: Axis = .vertical
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationView {
|
||||||
ZStack {
|
ZStack {
|
||||||
List {
|
List {
|
||||||
if items.isEmpty {
|
if items.isEmpty {
|
||||||
@@ -83,12 +83,12 @@ struct EditableListView: View {
|
|||||||
.onDelete(perform: deleteItem)
|
.onDelete(perform: deleteItem)
|
||||||
.onMove(perform: moveItem)
|
.onMove(perform: moveItem)
|
||||||
.scrollDismissesKeyboard(.immediately)
|
.scrollDismissesKeyboard(.immediately)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
VStack {
|
VStack {
|
||||||
Spacer()
|
Spacer()
|
||||||
|
|
||||||
Button {
|
Button {
|
||||||
addItem()
|
addItem()
|
||||||
} label: {
|
} label: {
|
||||||
@@ -101,15 +101,12 @@ struct EditableListView: View {
|
|||||||
.padding()
|
.padding()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle(title)
|
.navigationBarTitle(title, displayMode: .inline)
|
||||||
.navigationBarTitleDisplayMode(.inline)
|
.navigationBarItems(
|
||||||
.toolbar {
|
trailing: Button(action: { isPresented = false }) {
|
||||||
ToolbarItem(placement: .topBarTrailing) {
|
Text("Done")
|
||||||
Button(action: { isPresented = false }) {
|
|
||||||
Text("Done")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
.environment(\.editMode, .constant(.active))
|
.environment(\.editMode, .constant(.active))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,11 @@ struct RecipeIngredientSection: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} label: {
|
} label: {
|
||||||
Image(systemName: "storefront")
|
if #available(iOS 17.0, *) {
|
||||||
|
Image(systemName: "storefront")
|
||||||
|
} else {
|
||||||
|
Image(systemName: "heart.text.square")
|
||||||
|
}
|
||||||
}.disabled(viewModel.editMode)
|
}.disabled(viewModel.editMode)
|
||||||
|
|
||||||
SecondaryLabel(text: LocalizedStringKey("Ingredients"))
|
SecondaryLabel(text: LocalizedStringKey("Ingredients"))
|
||||||
@@ -107,8 +111,14 @@ fileprivate struct IngredientListItem: View {
|
|||||||
var body: some View {
|
var body: some View {
|
||||||
HStack(alignment: .top) {
|
HStack(alignment: .top) {
|
||||||
if groceryList.containsItem(at: recipeId, item: ingredient) {
|
if groceryList.containsItem(at: recipeId, item: ingredient) {
|
||||||
Image(systemName: "storefront")
|
if #available(iOS 17.0, *) {
|
||||||
.foregroundStyle(Color.green)
|
Image(systemName: "storefront")
|
||||||
|
.foregroundStyle(Color.green)
|
||||||
|
} else {
|
||||||
|
Image(systemName: "heart.text.square")
|
||||||
|
.foregroundStyle(Color.green)
|
||||||
|
}
|
||||||
|
|
||||||
} else if isSelected {
|
} else if isSelected {
|
||||||
Image(systemName: "checkmark.circle")
|
Image(systemName: "checkmark.circle")
|
||||||
} else {
|
} else {
|
||||||
@@ -119,7 +129,7 @@ fileprivate struct IngredientListItem: View {
|
|||||||
.foregroundStyle(.red)
|
.foregroundStyle(.red)
|
||||||
}
|
}
|
||||||
if unmodified {
|
if unmodified {
|
||||||
Text(ingredient)
|
Text(ObservableRecipeDetail.applyMarkdownStyling(ingredient))
|
||||||
.multilineTextAlignment(.leading)
|
.multilineTextAlignment(.leading)
|
||||||
.lineLimit(5)
|
.lineLimit(5)
|
||||||
} else {
|
} else {
|
||||||
@@ -132,9 +142,9 @@ fileprivate struct IngredientListItem: View {
|
|||||||
}
|
}
|
||||||
.onChange(of: servings) { newServings in
|
.onChange(of: servings) { newServings in
|
||||||
if recipeYield == 0 {
|
if recipeYield == 0 {
|
||||||
modifiedIngredient = ObservableRecipeDetail.adjustIngredient(ingredient, by: newServings)
|
modifiedIngredient = ObservableRecipeDetail.adjustIngredient(ObservableRecipeDetail.applyMarkdownStyling(ingredient), by: newServings)
|
||||||
} else {
|
} else {
|
||||||
modifiedIngredient = ObservableRecipeDetail.adjustIngredient(ingredient, by: newServings/recipeYield)
|
modifiedIngredient = ObservableRecipeDetail.adjustIngredient(ObservableRecipeDetail.applyMarkdownStyling(ingredient), by: newServings/recipeYield)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.foregroundStyle(isSelected ? Color.secondary : Color.primary)
|
.foregroundStyle(isSelected ? Color.secondary : Color.primary)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ fileprivate struct RecipeInstructionListItem: View {
|
|||||||
HStack(alignment: .top) {
|
HStack(alignment: .top) {
|
||||||
Text("\(index)")
|
Text("\(index)")
|
||||||
.monospaced()
|
.monospaced()
|
||||||
Text(instruction)
|
Text(ObservableRecipeDetail.applyMarkdownStyling(instruction))
|
||||||
}.padding(4)
|
}.padding(4)
|
||||||
.foregroundStyle(isSelected ? Color.secondary : Color.primary)
|
.foregroundStyle(isSelected ? Color.secondary : Color.primary)
|
||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
import Combine
|
import Combine
|
||||||
import AVFoundation
|
import AVFoundation
|
||||||
@@ -56,13 +55,8 @@ struct TimerView: View {
|
|||||||
.bold()
|
.bold()
|
||||||
.padding()
|
.padding()
|
||||||
.background {
|
.background {
|
||||||
if #available(iOS 26, *) {
|
RoundedRectangle(cornerRadius: 20)
|
||||||
Color.clear
|
.foregroundStyle(.ultraThickMaterial)
|
||||||
.glassEffect(.regular, in: .rect(cornerRadius: 20))
|
|
||||||
} else {
|
|
||||||
RoundedRectangle(cornerRadius: 20)
|
|
||||||
.foregroundStyle(.ultraThickMaterial)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,7 +147,7 @@ extension RecipeTimer {
|
|||||||
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
|
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
|
||||||
try AVAudioSession.sharedInstance().setActive(true)
|
try AVAudioSession.sharedInstance().setActive(true)
|
||||||
} catch {
|
} catch {
|
||||||
Logger.view.error("Failed to set audio session category. Error: \(error)")
|
print("Failed to set audio session category. Error: \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +158,7 @@ extension RecipeTimer {
|
|||||||
audioPlayer?.prepareToPlay()
|
audioPlayer?.prepareToPlay()
|
||||||
audioPlayer?.numberOfLoops = -1 // Loop indefinitely
|
audioPlayer?.numberOfLoops = -1 // Loop indefinitely
|
||||||
} catch {
|
} catch {
|
||||||
Logger.view.error("Error loading sound file: \(error)")
|
print("Error loading sound file: \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,9 +180,9 @@ extension RecipeTimer {
|
|||||||
func requestNotificationPermissions() {
|
func requestNotificationPermissions() {
|
||||||
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
|
||||||
if granted {
|
if granted {
|
||||||
Logger.view.debug("Notification permission granted.")
|
print("Notification permission granted.")
|
||||||
} else if let error = error {
|
} else if let error = error {
|
||||||
Logger.view.error("Notification permission denied because: \(error.localizedDescription).")
|
print("Notification permission denied because: \(error.localizedDescription).")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -205,7 +199,7 @@ extension RecipeTimer {
|
|||||||
|
|
||||||
UNUserNotificationCenter.current().add(request) { error in
|
UNUserNotificationCenter.current().add(request) { error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
Logger.view.error("Error scheduling notification: \(error)")
|
print("Error scheduling notification: \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
//
|
|
||||||
// LiquidGlassModifiers.swift
|
|
||||||
// Nextcloud Cookbook iOS Client
|
|
||||||
//
|
|
||||||
// Created by Vincent Meilinger on 14.02.26.
|
|
||||||
//
|
|
||||||
|
|
||||||
import SwiftUI
|
|
||||||
|
|
||||||
struct TabBarMinimizeModifier: ViewModifier {
|
|
||||||
func body(content: Content) -> some View {
|
|
||||||
if #available(iOS 26, *) {
|
|
||||||
content.tabBarMinimizeBehavior(.onScrollDown)
|
|
||||||
} else {
|
|
||||||
content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct BackgroundExtensionModifier: ViewModifier {
|
|
||||||
func body(content: Content) -> some View {
|
|
||||||
if #available(iOS 26, *) {
|
|
||||||
content.backgroundExtensionEffect()
|
|
||||||
} else {
|
|
||||||
content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
|
||||||
@@ -136,7 +135,7 @@ struct SettingsView: View {
|
|||||||
|
|
||||||
Section {
|
Section {
|
||||||
Button("Log out") {
|
Button("Log out") {
|
||||||
Logger.view.debug("Log out.")
|
print("Log out.")
|
||||||
viewModel.alertType = .LOG_OUT
|
viewModel.alertType = .LOG_OUT
|
||||||
viewModel.showAlert = true
|
viewModel.showAlert = true
|
||||||
|
|
||||||
@@ -144,7 +143,7 @@ struct SettingsView: View {
|
|||||||
.tint(.red)
|
.tint(.red)
|
||||||
|
|
||||||
Button("Delete local data") {
|
Button("Delete local data") {
|
||||||
Logger.view.debug("Clear cache.")
|
print("Clear cache.")
|
||||||
viewModel.alertType = .DELETE_CACHE
|
viewModel.alertType = .DELETE_CACHE
|
||||||
viewModel.showAlert = true
|
viewModel.showAlert = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,29 +6,51 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
|
||||||
struct GroceryListTabView: View {
|
struct GroceryListTabView: View {
|
||||||
@EnvironmentObject var groceryList: GroceryList
|
@EnvironmentObject var groceryList: GroceryList
|
||||||
|
@State var newGroceries: String = ""
|
||||||
|
@FocusState private var isFocused: Bool
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
if groceryList.groceryDict.isEmpty {
|
if groceryList.groceryDict.isEmpty {
|
||||||
EmptyGroceryListView()
|
EmptyGroceryListView(newGroceries: $newGroceries)
|
||||||
} else {
|
} else {
|
||||||
List {
|
List {
|
||||||
|
HStack(alignment: .top) {
|
||||||
|
TextEditor(text: $newGroceries)
|
||||||
|
.padding(4)
|
||||||
|
.overlay(RoundedRectangle(cornerRadius: 8)
|
||||||
|
.stroke(Color.secondary).opacity(0.5))
|
||||||
|
.focused($isFocused)
|
||||||
|
Button {
|
||||||
|
if !newGroceries.isEmpty {
|
||||||
|
let items = newGroceries
|
||||||
|
.split(separator: "\n")
|
||||||
|
.compactMap { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||||
|
.filter { !$0.isEmpty }
|
||||||
|
groceryList.addItems(items, toRecipe: "Other", recipeName: String(localized: "Other"))
|
||||||
|
}
|
||||||
|
newGroceries = ""
|
||||||
|
|
||||||
|
} label: {
|
||||||
|
Text("Add")
|
||||||
|
}
|
||||||
|
.disabled(newGroceries.isEmpty)
|
||||||
|
.buttonStyle(.borderedProminent)
|
||||||
|
}
|
||||||
|
|
||||||
ForEach(groceryList.groceryDict.keys.sorted(), id: \.self) { key in
|
ForEach(groceryList.groceryDict.keys.sorted(), id: \.self) { key in
|
||||||
Section {
|
Section {
|
||||||
ForEach(groceryList.groceryDict[key]!.items) { item in
|
ForEach(groceryList.groceryDict[key]!.items) { item in
|
||||||
GroceryListItemView(item: item, toggleAction: {
|
GroceryListItemView(item: item, toggleAction: {
|
||||||
groceryList.toggleItemChecked(item)
|
groceryList.toggleItemChecked(item)
|
||||||
groceryList.objectWillChange.send()
|
|
||||||
}, deleteAction: {
|
}, deleteAction: {
|
||||||
groceryList.deleteItem(item.name, fromRecipe: key)
|
|
||||||
withAnimation {
|
withAnimation {
|
||||||
groceryList.objectWillChange.send()
|
groceryList.deleteItem(item.name, fromRecipe: key)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -58,6 +80,9 @@ struct GroceryListTabView: View {
|
|||||||
.foregroundStyle(Color.nextcloudBlue)
|
.foregroundStyle(Color.nextcloudBlue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onTapGesture {
|
||||||
|
isFocused = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -98,6 +123,10 @@ fileprivate struct GroceryListItemView: View {
|
|||||||
|
|
||||||
|
|
||||||
fileprivate struct EmptyGroceryListView: View {
|
fileprivate struct EmptyGroceryListView: View {
|
||||||
|
@EnvironmentObject var groceryList: GroceryList
|
||||||
|
@Binding var newGroceries: String
|
||||||
|
@FocusState private var isFocused: Bool
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
List {
|
List {
|
||||||
Text("You're all set for cooking 🍓")
|
Text("You're all set for cooking 🍓")
|
||||||
@@ -106,8 +135,35 @@ fileprivate struct EmptyGroceryListView: View {
|
|||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
Text("Your grocery list is stored locally and therefore not synchronized across your devices.")
|
Text("Your grocery list is stored locally and therefore not synchronized across your devices.")
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
|
Text("To add grocieries manually, type them in the box below and press the button. To add multiple items at once, separate them by a new line.")
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
HStack(alignment: .top) {
|
||||||
|
TextEditor(text: $newGroceries)
|
||||||
|
.padding(4)
|
||||||
|
.overlay(RoundedRectangle(cornerRadius: 8)
|
||||||
|
.stroke(Color.secondary).opacity(0.5))
|
||||||
|
.focused($isFocused)
|
||||||
|
Button {
|
||||||
|
if !newGroceries.isEmpty {
|
||||||
|
let items = newGroceries
|
||||||
|
.split(separator: "\n")
|
||||||
|
.compactMap { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||||
|
.filter { !$0.isEmpty }
|
||||||
|
groceryList.addItems(items, toRecipe: "Other", recipeName: String(localized: "Other"))
|
||||||
|
}
|
||||||
|
newGroceries = ""
|
||||||
|
} label: {
|
||||||
|
Text("Add")
|
||||||
|
}
|
||||||
|
.disabled(newGroceries.isEmpty)
|
||||||
|
.buttonStyle(.borderedProminent)
|
||||||
|
}
|
||||||
|
.padding(.bottom, 4)
|
||||||
}
|
}
|
||||||
.navigationTitle("Grocery List")
|
.navigationTitle("Grocery List")
|
||||||
|
.onTapGesture {
|
||||||
|
isFocused = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,8 +207,10 @@ class GroceryRecipeItem: Identifiable, Codable {
|
|||||||
|
|
||||||
|
|
||||||
func addItem(_ itemName: String, toRecipe recipeId: String, recipeName: String? = nil, saveGroceryDict: Bool = true) {
|
func addItem(_ itemName: String, toRecipe recipeId: String, recipeName: String? = nil, saveGroceryDict: Bool = true) {
|
||||||
Logger.view.debug("Adding item of recipe \(String(describing: recipeName))")
|
print("Adding item of recipe \(String(describing: recipeName))")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|
||||||
|
|
||||||
if self.groceryDict[recipeId] != nil {
|
if self.groceryDict[recipeId] != nil {
|
||||||
self.groceryDict[recipeId]?.items.append(GroceryRecipeItem(itemName))
|
self.groceryDict[recipeId]?.items.append(GroceryRecipeItem(itemName))
|
||||||
} else {
|
} else {
|
||||||
@@ -161,8 +219,9 @@ class GroceryRecipeItem: Identifiable, Codable {
|
|||||||
}
|
}
|
||||||
if saveGroceryDict {
|
if saveGroceryDict {
|
||||||
self.save()
|
self.save()
|
||||||
self.objectWillChange.send()
|
|
||||||
}
|
}
|
||||||
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,12 +229,12 @@ class GroceryRecipeItem: Identifiable, Codable {
|
|||||||
for item in items {
|
for item in items {
|
||||||
addItem(item, toRecipe: recipeId, recipeName: recipeName, saveGroceryDict: false)
|
addItem(item, toRecipe: recipeId, recipeName: recipeName, saveGroceryDict: false)
|
||||||
}
|
}
|
||||||
save()
|
self.save()
|
||||||
objectWillChange.send()
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteItem(_ itemName: String, fromRecipe recipeId: String) {
|
func deleteItem(_ itemName: String, fromRecipe recipeId: String) {
|
||||||
Logger.view.debug("Deleting item \(itemName)")
|
print("Deleting item \(itemName)")
|
||||||
guard let recipe = groceryDict[recipeId] else { return }
|
guard let recipe = groceryDict[recipeId] else { return }
|
||||||
guard let itemIndex = groceryDict[recipeId]?.items.firstIndex(where: { $0.name == itemName }) else { return }
|
guard let itemIndex = groceryDict[recipeId]?.items.firstIndex(where: { $0.name == itemName }) else { return }
|
||||||
groceryDict[recipeId]?.items.remove(at: itemIndex)
|
groceryDict[recipeId]?.items.remove(at: itemIndex)
|
||||||
@@ -183,26 +242,28 @@ class GroceryRecipeItem: Identifiable, Codable {
|
|||||||
groceryDict.removeValue(forKey: recipeId)
|
groceryDict.removeValue(forKey: recipeId)
|
||||||
}
|
}
|
||||||
save()
|
save()
|
||||||
objectWillChange.send()
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteGroceryRecipe(_ recipeId: String) {
|
func deleteGroceryRecipe(_ recipeId: String) {
|
||||||
Logger.view.debug("Deleting grocery recipe with id \(recipeId)")
|
print("Deleting grocery recipe with id \(recipeId)")
|
||||||
groceryDict.removeValue(forKey: recipeId)
|
groceryDict.removeValue(forKey: recipeId)
|
||||||
save()
|
save()
|
||||||
objectWillChange.send()
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteAll() {
|
func deleteAll() {
|
||||||
Logger.view.debug("Deleting all grocery items")
|
print("Deleting all grocery items")
|
||||||
groceryDict = [:]
|
groceryDict = [:]
|
||||||
save()
|
save()
|
||||||
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
func toggleItemChecked(_ groceryItem: GroceryRecipeItem) {
|
func toggleItemChecked(_ groceryItem: GroceryRecipeItem) {
|
||||||
Logger.view.debug("Item checked: \(groceryItem.name)")
|
print("Item checked: \(groceryItem.name)")
|
||||||
groceryItem.isChecked.toggle()
|
groceryItem.isChecked.toggle()
|
||||||
save()
|
save()
|
||||||
|
self.objectWillChange.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
func containsItem(at recipeId: String, item: String) -> Bool {
|
func containsItem(at recipeId: String, item: String) -> Bool {
|
||||||
@@ -230,7 +291,7 @@ class GroceryRecipeItem: Identifiable, Codable {
|
|||||||
) else { return }
|
) else { return }
|
||||||
self.groceryDict = groceryDict
|
self.groceryDict = groceryDict
|
||||||
} catch {
|
} catch {
|
||||||
Logger.view.error("Unable to load grocery list")
|
print("Unable to load grocery list")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
|
|
||||||
@@ -73,7 +72,7 @@ struct RecipeTabView: View {
|
|||||||
)
|
)
|
||||||
.id(category.id) // Workaround: This is needed to update the detail view when the selection changes
|
.id(category.id) // Workaround: This is needed to update the detail view when the selection changes
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.tint(.nextcloudBlue)
|
.tint(.nextcloudBlue)
|
||||||
@@ -144,7 +143,7 @@ fileprivate struct RecipeTabViewToolBar: ToolbarContent {
|
|||||||
// Server connection indicator
|
// Server connection indicator
|
||||||
ToolbarItem(placement: .topBarTrailing) {
|
ToolbarItem(placement: .topBarTrailing) {
|
||||||
Button {
|
Button {
|
||||||
Logger.view.debug("Check server connection")
|
print("Check server connection")
|
||||||
viewModel.presentConnectionPopover = true
|
viewModel.presentConnectionPopover = true
|
||||||
} label: {
|
} label: {
|
||||||
if viewModel.presentLoadingIndicator {
|
if viewModel.presentLoadingIndicator {
|
||||||
@@ -171,7 +170,7 @@ fileprivate struct RecipeTabViewToolBar: ToolbarContent {
|
|||||||
// Create new recipes
|
// Create new recipes
|
||||||
ToolbarItem(placement: .topBarTrailing) {
|
ToolbarItem(placement: .topBarTrailing) {
|
||||||
Button {
|
Button {
|
||||||
Logger.view.debug("Add new recipe")
|
print("Add new recipe")
|
||||||
viewModel.presentEditView = true
|
viewModel.presentEditView = true
|
||||||
} label: {
|
} label: {
|
||||||
Image(systemName: "plus.circle.fill")
|
Image(systemName: "plus.circle.fill")
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -32,17 +32,15 @@ You can download the app from the AppStore:
|
|||||||
|
|
||||||
- [x] **Version 1.9**: Enhancements to recipe editing for better intuitiveness; user interface design improvements for recipe viewing.
|
- [x] **Version 1.9**: Enhancements to recipe editing for better intuitiveness; user interface design improvements for recipe viewing.
|
||||||
|
|
||||||
- [x] **Version 1.10**: Recipe ingredient calculator: Enables calculation of ingredient quantities based on a specifiable yield number.
|
- [ ] **Version 1.10**: Recipe ingredient calculator: Enables calculation of ingredient quantities based on a specifiable yield number.
|
||||||
|
|
||||||
- [ ] **Version 1.11**: Decoupling of internal recipe representation from the Nextcloud Cookbook recipe representation. This change provides increased flexibility for API updates and enables the introduction of features not currently supported by the Cookbook API, such as uploading images. This update will take some time, but will therefore result in simpler, better maintainable code. Update: I will continue to work on this update in January 2024.
|
- [ ] **Version 1.11**: Decoupling of internal recipe representation from the Nextcloud Cookbook recipe representation. This change provides increased flexibility for API updates and enables the introduction of features not currently supported by the Cookbook API, such as uploading images.
|
||||||
|
|
||||||
- [ ] **Version 1.12 and beyond** (Ideas for the future; integration not guaranteed!):
|
- [ ] **Version 1.12 and beyond** (Ideas for the future; integration not guaranteed!):
|
||||||
|
|
||||||
- Allow adding custom items to the grocery list.
|
|
||||||
|
|
||||||
- Fuzzy search for recipe names and keywords.
|
- Fuzzy search for recipe names and keywords.
|
||||||
|
|
||||||
- An in-app timer for the cook time specified in a recipe.
|
- In-app timer for the cook time specified in a recipe.
|
||||||
|
|
||||||
- Search for recipes based on left-over ingredients.
|
- Search for recipes based on left-over ingredients.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user