简单封装

This commit is contained in:
2026-01-30 13:26:02 +08:00
parent 36774a8a2c
commit 3705db4aab
4 changed files with 527 additions and 293 deletions

95
CustomKeyboard/VM/KBVM.h Normal file
View File

@@ -0,0 +1,95 @@
//
// KBVM.h
// CustomKeyboard
//
// 键盘扩展的 ViewModel封装网络请求逻辑
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
/// 聊天响应模型
@interface KBChatResponse : NSObject
@property (nonatomic, copy, nullable) NSString *text;
@property (nonatomic, copy, nullable) NSString *audioId;
@property (nonatomic, copy, nullable) NSString *errorMessage;
@property (nonatomic, assign) BOOL success;
@end
/// 音频响应模型
@interface KBAudioResponse : NSObject
@property (nonatomic, copy, nullable) NSString *audioURL;
@property (nonatomic, strong, nullable) NSData *audioData;
@property (nonatomic, assign) NSTimeInterval duration;
@property (nonatomic, copy, nullable) NSString *errorMessage;
@property (nonatomic, assign) BOOL success;
@end
/// 聊天请求回调
typedef void(^KBChatCompletion)(KBChatResponse *response);
/// 音频 URL 回调
typedef void(^KBAudioURLCompletion)(KBAudioResponse *response);
/// 音频数据回调
typedef void(^KBAudioDataCompletion)(KBAudioResponse *response);
/// 头像回调
typedef void(^KBAvatarCompletion)(UIImage * _Nullable image, NSError * _Nullable error);
@interface KBVM : NSObject
+ (instancetype)shared;
#pragma mark - Chat API
/// 发送聊天消息
/// @param content 消息内容
/// @param companionId 人设 ID
/// @param completion 回调
- (void)sendChatMessageWithContent:(NSString *)content
companionId:(NSInteger)companionId
completion:(KBChatCompletion)completion;
#pragma mark - Audio API
/// 获取音频 URL单次请求
/// @param audioId 音频 ID
/// @param completion 回调
- (void)fetchAudioURLWithAudioId:(NSString *)audioId
completion:(KBAudioURLCompletion)completion;
/// 轮询获取音频 URL自动重试
/// @param audioId 音频 ID
/// @param maxRetries 最大重试次数
/// @param interval 重试间隔(秒)
/// @param completion 回调
- (void)pollAudioURLWithAudioId:(NSString *)audioId
maxRetries:(NSInteger)maxRetries
interval:(NSTimeInterval)interval
completion:(KBAudioURLCompletion)completion;
/// 下载音频数据
/// @param urlString 音频 URL
/// @param completion 回调
- (void)downloadAudioFromURL:(NSString *)urlString
completion:(KBAudioDataCompletion)completion;
#pragma mark - Avatar API
/// 下载头像图片
/// @param urlString 头像 URL
/// @param completion 回调
- (void)downloadAvatarFromURL:(NSString *)urlString
completion:(KBAvatarCompletion)completion;
#pragma mark - Helper
/// 从 AppGroup 获取选中的 persona companionId
- (NSInteger)selectedCompanionIdFromAppGroup;
/// 从 AppGroup 获取选中的 persona 信息
- (nullable NSDictionary *)selectedPersonaFromAppGroup;
@end
NS_ASSUME_NONNULL_END