Add premium features and reorganize Settings tab
Premium Features: - Journal notes and photo attachments for mood entries - Data export (CSV and PDF reports) - Privacy lock with Face ID/Touch ID - Apple Health integration for mood correlation - 4 new personality packs (Motivational Coach, Zen Master, Best Friend, Data Analyst) Settings Tab Reorganization: - Combined Customize and Settings into single tab with segmented control - Added upgrade banner with trial countdown above segment - "Why Upgrade?" sheet showing all premium benefits - Subscribe button opens improved StoreKit 2 subscription view UI Improvements: - Enhanced subscription store with feature highlights - Entry detail view for viewing/editing notes and photos - Removed duplicate subscription banners from tab content 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
246
Shared/Services/PhotoManager.swift
Normal file
246
Shared/Services/PhotoManager.swift
Normal file
@@ -0,0 +1,246 @@
|
||||
//
|
||||
// PhotoManager.swift
|
||||
// Feels
|
||||
//
|
||||
// Manages photo storage for mood entries.
|
||||
// Photos are stored as JPEG files in the app group Documents directory.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
class PhotoManager: ObservableObject {
|
||||
|
||||
static let shared = PhotoManager()
|
||||
|
||||
// MARK: - Constants
|
||||
|
||||
private let compressionQuality: CGFloat = 0.8
|
||||
private let thumbnailSize = CGSize(width: 200, height: 200)
|
||||
|
||||
// MARK: - Storage Location
|
||||
|
||||
private var photosDirectory: URL? {
|
||||
guard let containerURL = FileManager.default.containerURL(
|
||||
forSecurityApplicationGroupIdentifier: Constants.currentGroupShareId
|
||||
) else {
|
||||
print("PhotoManager: Failed to get app group container")
|
||||
return nil
|
||||
}
|
||||
|
||||
let photosURL = containerURL.appendingPathComponent("Photos", isDirectory: true)
|
||||
|
||||
// Create directory if it doesn't exist
|
||||
if !FileManager.default.fileExists(atPath: photosURL.path) {
|
||||
do {
|
||||
try FileManager.default.createDirectory(at: photosURL, withIntermediateDirectories: true)
|
||||
} catch {
|
||||
print("PhotoManager: Failed to create photos directory: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return photosURL
|
||||
}
|
||||
|
||||
private var thumbnailsDirectory: URL? {
|
||||
guard let photosDir = photosDirectory else { return nil }
|
||||
|
||||
let thumbnailsURL = photosDir.appendingPathComponent("Thumbnails", isDirectory: true)
|
||||
|
||||
if !FileManager.default.fileExists(atPath: thumbnailsURL.path) {
|
||||
do {
|
||||
try FileManager.default.createDirectory(at: thumbnailsURL, withIntermediateDirectories: true)
|
||||
} catch {
|
||||
print("PhotoManager: Failed to create thumbnails directory: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return thumbnailsURL
|
||||
}
|
||||
|
||||
// MARK: - Save Photo
|
||||
|
||||
func savePhoto(_ image: UIImage) -> UUID? {
|
||||
guard let photosDir = photosDirectory,
|
||||
let thumbnailsDir = thumbnailsDirectory else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let photoID = UUID()
|
||||
let filename = "\(photoID.uuidString).jpg"
|
||||
|
||||
// Save full resolution
|
||||
let fullURL = photosDir.appendingPathComponent(filename)
|
||||
guard let fullData = image.jpegData(compressionQuality: compressionQuality) else {
|
||||
print("PhotoManager: Failed to create JPEG data")
|
||||
return nil
|
||||
}
|
||||
|
||||
do {
|
||||
try fullData.write(to: fullURL)
|
||||
} catch {
|
||||
print("PhotoManager: Failed to save photo: \(error)")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Save thumbnail
|
||||
let thumbnailURL = thumbnailsDir.appendingPathComponent(filename)
|
||||
if let thumbnail = createThumbnail(from: image),
|
||||
let thumbnailData = thumbnail.jpegData(compressionQuality: 0.6) {
|
||||
try? thumbnailData.write(to: thumbnailURL)
|
||||
}
|
||||
|
||||
EventLogger.log(event: "photo_saved")
|
||||
return photoID
|
||||
}
|
||||
|
||||
// MARK: - Load Photo
|
||||
|
||||
func loadPhoto(id: UUID) -> UIImage? {
|
||||
guard let photosDir = photosDirectory else { return nil }
|
||||
|
||||
let filename = "\(id.uuidString).jpg"
|
||||
let fullURL = photosDir.appendingPathComponent(filename)
|
||||
|
||||
guard FileManager.default.fileExists(atPath: fullURL.path),
|
||||
let data = try? Data(contentsOf: fullURL),
|
||||
let image = UIImage(data: data) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return image
|
||||
}
|
||||
|
||||
func loadThumbnail(id: UUID) -> UIImage? {
|
||||
guard let thumbnailsDir = thumbnailsDirectory else { return nil }
|
||||
|
||||
let filename = "\(id.uuidString).jpg"
|
||||
let thumbnailURL = thumbnailsDir.appendingPathComponent(filename)
|
||||
|
||||
// Try thumbnail first
|
||||
if FileManager.default.fileExists(atPath: thumbnailURL.path),
|
||||
let data = try? Data(contentsOf: thumbnailURL),
|
||||
let image = UIImage(data: data) {
|
||||
return image
|
||||
}
|
||||
|
||||
// Fall back to full image if thumbnail doesn't exist
|
||||
return loadPhoto(id: id)
|
||||
}
|
||||
|
||||
// MARK: - Delete Photo
|
||||
|
||||
func deletePhoto(id: UUID) -> Bool {
|
||||
guard let photosDir = photosDirectory,
|
||||
let thumbnailsDir = thumbnailsDirectory else {
|
||||
return false
|
||||
}
|
||||
|
||||
let filename = "\(id.uuidString).jpg"
|
||||
let fullURL = photosDir.appendingPathComponent(filename)
|
||||
let thumbnailURL = thumbnailsDir.appendingPathComponent(filename)
|
||||
|
||||
var success = true
|
||||
|
||||
// Delete full image
|
||||
if FileManager.default.fileExists(atPath: fullURL.path) {
|
||||
do {
|
||||
try FileManager.default.removeItem(at: fullURL)
|
||||
} catch {
|
||||
print("PhotoManager: Failed to delete photo: \(error)")
|
||||
success = false
|
||||
}
|
||||
}
|
||||
|
||||
// Delete thumbnail
|
||||
if FileManager.default.fileExists(atPath: thumbnailURL.path) {
|
||||
try? FileManager.default.removeItem(at: thumbnailURL)
|
||||
}
|
||||
|
||||
if success {
|
||||
EventLogger.log(event: "photo_deleted")
|
||||
}
|
||||
|
||||
return success
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func createThumbnail(from image: UIImage) -> UIImage? {
|
||||
let size = thumbnailSize
|
||||
let aspectRatio = image.size.width / image.size.height
|
||||
|
||||
var targetSize: CGSize
|
||||
if aspectRatio > 1 {
|
||||
// Landscape
|
||||
targetSize = CGSize(width: size.width, height: size.width / aspectRatio)
|
||||
} else {
|
||||
// Portrait or square
|
||||
targetSize = CGSize(width: size.height * aspectRatio, height: size.height)
|
||||
}
|
||||
|
||||
UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
|
||||
image.draw(in: CGRect(origin: .zero, size: targetSize))
|
||||
let thumbnail = UIGraphicsGetImageFromCurrentImageContext()
|
||||
UIGraphicsEndImageContext()
|
||||
|
||||
return thumbnail
|
||||
}
|
||||
|
||||
// MARK: - Storage Info
|
||||
|
||||
var totalPhotoCount: Int {
|
||||
guard let photosDir = photosDirectory else { return 0 }
|
||||
|
||||
let files = try? FileManager.default.contentsOfDirectory(atPath: photosDir.path)
|
||||
return files?.filter { $0.hasSuffix(".jpg") }.count ?? 0
|
||||
}
|
||||
|
||||
var totalStorageUsed: Int64 {
|
||||
guard let photosDir = photosDirectory else { return 0 }
|
||||
|
||||
var totalSize: Int64 = 0
|
||||
let fileManager = FileManager.default
|
||||
|
||||
if let enumerator = fileManager.enumerator(at: photosDir, includingPropertiesForKeys: [.fileSizeKey]) {
|
||||
for case let fileURL as URL in enumerator {
|
||||
if let fileSize = try? fileURL.resourceValues(forKeys: [.fileSizeKey]).fileSize {
|
||||
totalSize += Int64(fileSize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return totalSize
|
||||
}
|
||||
|
||||
var formattedStorageUsed: String {
|
||||
let bytes = totalStorageUsed
|
||||
let formatter = ByteCountFormatter()
|
||||
formatter.countStyle = .file
|
||||
return formatter.string(fromByteCount: bytes)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI Image Loading
|
||||
|
||||
extension PhotoManager {
|
||||
func image(for id: UUID?) -> Image? {
|
||||
guard let id = id,
|
||||
let uiImage = loadPhoto(id: id) else {
|
||||
return nil
|
||||
}
|
||||
return Image(uiImage: uiImage)
|
||||
}
|
||||
|
||||
func thumbnail(for id: UUID?) -> Image? {
|
||||
guard let id = id,
|
||||
let uiImage = loadThumbnail(id: id) else {
|
||||
return nil
|
||||
}
|
||||
return Image(uiImage: uiImage)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user