新增搜索
This commit is contained in:
77
keyBoard/Class/Search/VM/KBSearchVM.m
Normal file
77
keyBoard/Class/Search/VM/KBSearchVM.m
Normal file
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// KBSearchVM.m
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Mac on 2025/12/17.
|
||||
//
|
||||
|
||||
#import "KBSearchVM.h"
|
||||
#import "KBShopVM.h"
|
||||
#import "KBNetworkManager.h"
|
||||
#import "KBAPI.h"
|
||||
#import "KBBizCode.h"
|
||||
#import "KBSearchThemeModel.h"
|
||||
#import <MJExtension/MJExtension.h>
|
||||
|
||||
@interface KBSearchVM ()
|
||||
@property (nonatomic, strong) KBShopVM *shopVM;
|
||||
@end
|
||||
|
||||
@implementation KBSearchVM
|
||||
|
||||
- (NSError *)kb_invalidResponseError {
|
||||
return [NSError errorWithDomain:KBNetworkErrorDomain
|
||||
code:KBNetworkErrorInvalidResponse
|
||||
userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid response")}];
|
||||
}
|
||||
|
||||
- (NSError *)kb_invalidParameterError {
|
||||
return [NSError errorWithDomain:KBNetworkErrorDomain
|
||||
code:KBNetworkErrorInvalidResponse
|
||||
userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid parameter")}];
|
||||
}
|
||||
|
||||
- (void)fetchRecommendedThemesWithCompletion:(KBSearchRecommendedCompletion)completion {
|
||||
[self.shopVM fetchRecommendedThemesWithCompletion:^(NSArray<KBShopThemeModel *> * _Nullable themes, NSError * _Nullable error) {
|
||||
if (completion) completion(themes, error);
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)searchThemesWithName:(NSString *)themeName
|
||||
completion:(KBSearchThemesCompletion)completion {
|
||||
NSString *trim = [themeName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
if (trim.length == 0) {
|
||||
if (completion) completion(nil, [self kb_invalidParameterError]);
|
||||
return;
|
||||
}
|
||||
NSDictionary *params = @{@"themeName": trim};
|
||||
[[KBNetworkManager shared] GET:API_THEME_SEARCH
|
||||
parameters:params
|
||||
headers:nil
|
||||
autoShowBusinessError:NO
|
||||
completion:^(NSDictionary * _Nullable json,
|
||||
NSURLResponse * _Nullable response,
|
||||
NSError * _Nullable error) {
|
||||
if (error) {
|
||||
if (completion) completion(nil, error);
|
||||
return;
|
||||
}
|
||||
id dataObj = json[KBData] ?: json[@"data"];
|
||||
if (![dataObj isKindOfClass:[NSArray class]]) {
|
||||
if (completion) completion(nil, [self kb_invalidResponseError]);
|
||||
return;
|
||||
}
|
||||
NSArray<KBSearchThemeModel *> *list = [KBSearchThemeModel mj_objectArrayWithKeyValuesArray:(NSArray *)dataObj];
|
||||
if (completion) completion(list, nil);
|
||||
}];
|
||||
}
|
||||
|
||||
- (KBShopVM *)shopVM {
|
||||
if (!_shopVM) {
|
||||
_shopVM = [[KBShopVM alloc] init];
|
||||
}
|
||||
return _shopVM;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user