StatusComparisonViewModel.swift 3.63 KB
//
//  StatusComparisonViewModel.swift
//  hippo-watch Watch App
//
//  Created by shihao on 2025/7/8.
//

import Foundation
import SwiftUI
import ClockKit
//import SDWebImage
import Combine

class StatusComparisonViewModel: ObservableObject {
    
    @Published var currentInteractionAction: InteractionActionType?
    @Published var myWatchTheme: WatchLocalTheme?
    @Published var otherWatchTheme: WatchLocalTheme?
    
    init() {
        refreshTheme()
    }
    
    func onInteractionClick() {
        Task {
            do {
                setupCurrerntInteractionAnimation()
                let _ = try await InteractionService().sendInteraction(actionType: WatchUserinfoManager.share.interactionActionType)
            } catch {
                print(error.getToastErrorDescription())
            }
        }
    }
    
    private func setupCurrerntInteractionAnimation() {
        currentInteractionAction = WatchUserinfoManager.share.interactionActionType
        Task {
            try? await Task.sleep(nanoseconds: 4 * 1_000_000_000)
            await MainActor.run {
                self.currentInteractionAction = nil
            }
        }
    }
    
    func refreshTheme() {
        getMyWatchTheme()
        getOtherWatchTheme()
    }
    
    private func getMyWatchTheme() {
        let defaults = AppGroupConstants.defaults
        if let data = defaults?.data(forKey: AppGroupConstants.Key.myWatchTheme),
           let cachedTheme = try? JSONDecoder().decode(WatchLocalTheme.self, from: data) {
            myWatchTheme = cachedTheme
        }
        
        Task {
            if let result = try? await ThemeService().getCurrentTheme(isOther: false) {
                let localTheme = await WatchThemeLocalBuilder.makeLocalTheme(from: result)
                await MainActor.run {
                    self.myWatchTheme = localTheme
                }
                await MainActor.run {
                    reloadAllComplications()
                }
                
                do {
                    let data = try JSONEncoder().encode(localTheme)
                    defaults?.set(data, forKey: AppGroupConstants.Key.myWatchTheme)
                }catch {
                    print(error.getToastErrorDescription())
                }
                
            }
        }
    }
    
    private func getOtherWatchTheme() {
        guard WatchUserinfoManager.share.myUserinfo?.isPaired == true else { return }
        
        let defaults = AppGroupConstants.defaults
        if let data = defaults?.data(forKey: AppGroupConstants.Key.otherWatchTheme),
           let cachedTheme = try? JSONDecoder().decode(WatchLocalTheme.self, from: data) {
            otherWatchTheme = cachedTheme
        }
        
        Task {
            if let result = try? await ThemeService().getCurrentTheme(isOther: true) {
                let localTheme = await WatchThemeLocalBuilder.makeLocalTheme(from: result)
                await MainActor.run {
                    self.otherWatchTheme = localTheme
                }
                
                do {
                    let data = try JSONEncoder().encode(localTheme)
                    defaults?.set(data, forKey: AppGroupConstants.Key.otherWatchTheme)
                }catch {
                    print(error.getToastErrorDescription())
                }
            }
        }
    }
    
    private func reloadAllComplications() {
#if os(watchOS)
        let server = CLKComplicationServer.sharedInstance()
        if let complications = server.activeComplications {
            for complication in complications {
                server.reloadTimeline(for: complication)
            }
        }
#endif
    }
    
}