forked from Atelier-Shiori/hachidori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixSearchDialog.m
161 lines (150 loc) · 5.73 KB
/
FixSearchDialog.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// FixSearchDialog.m
// Hachidori
//
// Created by 高町なのは on 2014/11/15.
// Copyright (c) 2014年 MAL Updater OS X Group and James Moy All rights reserved.
//
#import "FixSearchDialog.h"
#import "AtarashiiAPIListFormatKitsu.h"
#import "AtarashiiAPIListFormatAniList.h"
#import "AniListConstants.h"
#import <AFNetworking/AFNetworking.h>
#import "Utility.h"
@interface FixSearchDialog ()
@property (strong) AFHTTPSessionManager *searchmanager;
@end
@implementation FixSearchDialog
@synthesize arraycontroller;
@synthesize search;
@synthesize deleteoncorrection;
@synthesize onetimecorrection;
@synthesize tb;
@synthesize selectedtitle;
@synthesize selectedaniid;
@synthesize selectedtotalepisodes;
@synthesize searchquery;
@synthesize correction;
@synthesize allowdelete;
- (instancetype)init{
self = [super initWithWindowNibName:@"FixSearchDialog"];
if (!self) {
return nil;
}
// Init AFNetworking
_searchmanager = [AFHTTPSessionManager manager];
_searchmanager.requestSerializer = [AFJSONRequestSerializer serializer];
_searchmanager.responseSerializer = [AFJSONResponseSerializer serializer];
_searchmanager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"application/vnd.api+json", @"text/javascript", @"text/html", @"text/plain", nil];
[_searchmanager.requestSerializer setValue:@"application/vnd.api+json" forHTTPHeaderField:@"Content-Type"];
return self;
}
- (long)currentservice {
return [NSUserDefaults.standardUserDefaults integerForKey:@"currentservice"];
}
- (void)windowDidLoad {
if (correction) {
if (allowdelete) {
[deleteoncorrection setHidden:NO];
deleteoncorrection.state = NSOnState;
}
[onetimecorrection setHidden:NO];
}
else {
deleteoncorrection.state = 0;
}
[super windowDidLoad];
if (searchquery.length>0) {
search.stringValue = searchquery;
[self search:nil];
}
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
- (IBAction)closesearch:(id)sender {
[self.window orderOut:self];
[NSApp endSheet:self.window returnCode:0];
}
- (IBAction)updatesearch:(id)sender {
NSDictionary * d = arraycontroller.selectedObjects[0];
if (correction) {
// Set Up Prompt Message Window
NSAlert * alert = [[NSAlert alloc] init] ;
[alert addButtonWithTitle:NSLocalizedString(@"Yes",nil)];
[alert addButtonWithTitle:NSLocalizedString(@"No",nil)];
alert.messageText = [NSString stringWithFormat:NSLocalizedString(@"Do you want to correct this title as %@?",nil),d[@"title"]];
[alert setInformativeText:NSLocalizedString(@"Once done, you cannot undo this action.",nil)];
// Set Message type to Warning
alert.alertStyle = NSWarningAlertStyle;
if ([alert runModal]== NSAlertFirstButtonReturn) {
[self finish:d];
}
else {
return;
}
}
else {
[self finish:d];
}
}
- (void)finish:(NSDictionary *)d{
selectedtitle = d[@"title"];
selectedaniid = (NSNumber *)d[@"id"];
if (d[@"episodes"] != [NSNull null]) {
selectedtotalepisodes = ((NSNumber *)d[@"episodes"]).intValue;
}
else {
selectedtotalepisodes = 0;
}
[self.window orderOut:self];
[NSApp endSheet:self.window returnCode:1];
}
- (IBAction)search:(id)sender{
if (search.stringValue.length> 0) {
NSString *searchterm = [Utility urlEncodeString:search.stringValue];
switch (self.currentservice) {
case 0: {
[_searchmanager GET:[NSString stringWithFormat:@"https://kitsu.io/api/edge/anime?filter[text]=%@", searchterm] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self populateData:[AtarashiiAPIListFormatKitsu KitsuAnimeSearchtoAtarashii:responseObject]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[[arraycontroller mutableArrayValueForKey:@"content"] removeAllObjects];
}];
break;
}
case 1: {
[_searchmanager POST:@"https://graphql.anilist.co" parameters:@{@"query" : kAnilisttitlesearch, @"variables" : @{@"query" : search.stringValue, @"type" : @"ANIME"}} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[self populateData:[AtarashiiAPIListFormatAniList AniListAnimeSearchtoAtarashii:responseObject]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[[arraycontroller mutableArrayValueForKey:@"content"] removeAllObjects];
}];
break;
}
default: {
break;
}
}
}
}
- (IBAction)getHelp:(id)sender{
//Show Help
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/chikorita157/hachidori/wiki/Correction-Exception-Help"]];
}
- (void)populateData:(id)data{
//Remove all existing Data
[[arraycontroller mutableArrayValueForKey:@"content"] removeAllObjects];
//Translate the Kitsu search data to old format
NSMutableArray *searchdata = [NSMutableArray new];
[searchdata addObjectsFromArray:data];
//Add it to the array controller
[arraycontroller addObjects:searchdata];
//Show on tableview
[tb reloadData];
//Deselect Selection
[tb deselectAll:self];
}
- (bool)getdeleteTitleonCorrection{
return (bool) deleteoncorrection.state;
}
- (bool)getcorrectonce{
return (bool) onetimecorrection.state;
}
@end