KMProfileInfoWindowController.m 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. //
  2. // KMProfileInfoWindowController.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 蒋志鹏 on 2019-07-19.
  6. //
  7. #import "KMProfileInfoWindowController.h"
  8. //#import "NSWindowController_SKExtensions.h"
  9. //#import "KMProfileCellView.h"
  10. //#import "KMProfileTitleCellView.h"
  11. //#import "NSButton+TitleColor.h"
  12. #import <PDF_Master-Swift.h>
  13. static NSString *KMProfileUnarchiveKey = @"KMProfileUnarchiveKeyTemp1";
  14. static NSString *KMProfileSaveInfoFullNameKey = @"KMProfileSaveInfoFullNameKey";
  15. static NSString *KMProfileSaveInfoFirstNameKey = @"KMProfileSaveInfoFirstNameKey";
  16. static NSString *KMProfileSaveInfoMiddleNameKey = @"KMProfileSaveInfoMiddleNameKey";
  17. static NSString *KMProfileSaveInfoLastNameKey = @"KMProfileSaveInfoLastNameKey";
  18. static NSString *KMProfileSaveInfoStreet1Key = @"KMProfileSaveInfoStreet1Key";
  19. static NSString *KMProfileSaveInfoStreet2Key = @"KMProfileSaveInfoStreet2Key";
  20. static NSString *KMProfileSaveInfoCityKey = @"KMProfileSaveInfoCityKey";
  21. static NSString *KMProfileSaveInfoStateKey = @"KMProfileSaveInfoStateKey";
  22. static NSString *KMProfileSaveInfoZipKey = @"KMProfileSaveInfoZipKey";
  23. static NSString *KMProfileSaveInfoCountryKey = @"KMProfileSaveInfoCountryKey";
  24. static NSString *KMProfileSaveInfoEmailKey = @"KMProfileSaveInfoEmailKey";
  25. static NSString *KMProfileSaveInfoTelKey = @"KMProfileSaveInfoTelKey";
  26. static NSString *KMProfileSaveInfoBirthDateKey = @"KMProfileSaveInfoBirthDateKey";
  27. static NSString *KMProfileSaveInfoCustomInfoKey = @"KMProfileSaveInfoCustomInfoKey";
  28. @interface KMProfileInfo : NSObject <NSCoding>
  29. @property (nonatomic, retain) NSString *fullName;
  30. @property (nonatomic, retain) NSString *firstName;
  31. @property (nonatomic, retain) NSString *middleName;
  32. @property (nonatomic, retain) NSString *lastName;
  33. @property (nonatomic, retain) NSString *street1;
  34. @property (nonatomic, retain) NSString *street2;
  35. @property (nonatomic, retain) NSString *city;
  36. @property (nonatomic, retain) NSString *state;
  37. @property (nonatomic, retain) NSString *zip;
  38. @property (nonatomic, retain) NSString *country;
  39. @property (nonatomic, retain) NSString *email;
  40. @property (nonatomic, retain) NSString *tel;
  41. @property (nonatomic, retain) NSString *birthDate;
  42. @property (nonatomic, retain) NSMutableArray *customInfoArray;
  43. @end
  44. @implementation KMProfileInfo
  45. #pragma mark - Getter & setter
  46. - (void)setFullName:(NSString *)fullName {
  47. // if (![fullName isEqualToString:_fullName]) {
  48. // [_fullName release];
  49. // _fullName = [fullName retain];
  50. _fullName = fullName;
  51. [self saveInfo];
  52. // }
  53. }
  54. - (void)setFirstName:(NSString *)firstName {
  55. // if (![_firstName isEqualToString:firstName]) {
  56. // [_firstName release];
  57. // _firstName = [firstName retain];
  58. _firstName = firstName;
  59. [self saveInfo];
  60. // }
  61. }
  62. - (void)setMiddleName:(NSString *)middleName {
  63. // if (![_middleName isEqualToString:middleName]) {
  64. // [_middleName release];
  65. // _middleName = [middleName retain];
  66. _middleName = middleName;
  67. [self saveInfo];
  68. // }
  69. }
  70. - (void)setLastName:(NSString *)lastName {
  71. // if (![_lastName isEqualToString:lastName]) {
  72. // [_lastName release];
  73. // _lastName = [lastName retain];
  74. _lastName = lastName;
  75. [self saveInfo];
  76. // }
  77. }
  78. - (void)setStreet1:(NSString *)street1 {
  79. // if (![_street1 isEqualToString:street1]) {
  80. // [_street1 release];
  81. // _street1 = [street1 retain];
  82. _street1 = street1;
  83. [self saveInfo];
  84. // }
  85. }
  86. - (void)setStreet2:(NSString *)street2 {
  87. // if (![_street2 isEqualToString:street2]) {
  88. // [_street2 release];
  89. // _street2 = [street2 retain];
  90. _street2 = street2;
  91. [self saveInfo];
  92. // }
  93. }
  94. - (void)setCity:(NSString *)city {
  95. // if (![_city isEqualToString:city]) {
  96. // [_city release];
  97. // _city = [city retain];
  98. _city = city;
  99. [self saveInfo];
  100. // }
  101. }
  102. - (void)setState:(NSString *)state {
  103. // if (![_state isEqualToString:state]) {
  104. // [_state release];
  105. // _state = [state retain];
  106. _state = state;
  107. [self saveInfo];
  108. // }
  109. }
  110. - (void)setZip:(NSString *)zip {
  111. // if (![_zip isEqualToString:zip]) {
  112. // [_zip release];
  113. // _zip = [zip retain];
  114. _zip = zip;
  115. [self saveInfo];
  116. // }
  117. }
  118. - (void)setCountry:(NSString *)country {
  119. // if (![_country isEqualToString:country]) {
  120. // [_country release];
  121. // _country = [country retain];
  122. _country = country;
  123. [self saveInfo];
  124. // }
  125. }
  126. - (void)setEmail:(NSString *)email {
  127. // if (![_email isEqualToString:email]) {
  128. // [_email release];
  129. // _email = [email retain];
  130. _email = email;
  131. [self saveInfo];
  132. // }
  133. }
  134. - (void)setTel:(NSString *)tel {
  135. // if (![_tel isEqualToString:tel]) {
  136. // [_tel release];
  137. // _tel = [tel retain];
  138. _tel = tel;
  139. [self saveInfo];
  140. // }
  141. }
  142. - (void)setBirthDate:(NSString *)birthDate {
  143. // if (![_birthDate isEqualToString:birthDate]) {
  144. // [_birthDate release];
  145. // _birthDate = [birthDate retain];
  146. _birthDate = birthDate;
  147. [self saveInfo];
  148. // }
  149. }
  150. - (void)setCustomInfoArray:(NSMutableArray *)customInfoArray {
  151. // if (![customInfoArray isEqual:customInfoArray]) {
  152. // [customInfoArray release];
  153. // _customInfoArray = [customInfoArray retain];
  154. _customInfoArray = customInfoArray;
  155. [self saveInfo];
  156. // }
  157. }
  158. #pragma mark - Init Methods
  159. + (instancetype)sharedInstance {
  160. static dispatch_once_t onceToken;
  161. static KMProfileInfo *manager = nil;
  162. dispatch_once(&onceToken, ^{
  163. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  164. NSData *encodedObject = [defaults objectForKey:KMProfileUnarchiveKey];
  165. if (!encodedObject) {
  166. manager = [[KMProfileInfo alloc] init];
  167. [manager setBaseInfo];
  168. [manager saveInfo];
  169. } else {
  170. manager = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
  171. }
  172. });
  173. return manager;
  174. }
  175. #pragma mark - Private Methods
  176. - (void)saveInfo {
  177. NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self];
  178. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  179. [defaults setObject:encodedObject forKey:KMProfileUnarchiveKey];
  180. [defaults synchronize];
  181. }
  182. - (void)setBaseInfo {
  183. _fullName = @"";
  184. _firstName = @"";
  185. _middleName = @"";
  186. _lastName = @"";
  187. _street1 = @"";
  188. _street2 = @"";
  189. _city = @"";
  190. _state = @"";
  191. _zip = @"";
  192. _country = @"";
  193. _email = @"";
  194. _tel = @"";
  195. _birthDate = @"";
  196. _customInfoArray = [NSMutableArray array];
  197. }
  198. - (void)replaceObjectInCustomInfoArrayAtIndex:(NSInteger)index withObject:(id)object {
  199. [self.customInfoArray replaceObjectAtIndex:index withObject:object];
  200. [self saveInfo];
  201. }
  202. - (void)removeObjectFromCustomInfoArrayAtIndex:(NSUInteger)index {
  203. [self.customInfoArray removeObjectAtIndex:index];
  204. [self saveInfo];
  205. }
  206. - (void)insertObject:(id)object inCustomInfoArrayAtIndex:(NSInteger)index {
  207. [self.customInfoArray insertObject:object atIndex:index];
  208. [self saveInfo];
  209. }
  210. #pragma mark - NSCoding
  211. - (void)encodeWithCoder:(NSCoder *)aCoder {
  212. [aCoder encodeObject:self.fullName forKey:KMProfileSaveInfoFullNameKey];
  213. [aCoder encodeObject:self.firstName forKey:KMProfileSaveInfoFirstNameKey];
  214. [aCoder encodeObject:self.middleName forKey:KMProfileSaveInfoMiddleNameKey];
  215. [aCoder encodeObject:self.lastName forKey:KMProfileSaveInfoLastNameKey];
  216. [aCoder encodeObject:self.street1 forKey:KMProfileSaveInfoStreet1Key];
  217. [aCoder encodeObject:self.street2 forKey:KMProfileSaveInfoStreet2Key];
  218. [aCoder encodeObject:self.city forKey:KMProfileSaveInfoCityKey];
  219. [aCoder encodeObject:self.state forKey:KMProfileSaveInfoStateKey];
  220. [aCoder encodeObject:self.zip forKey:KMProfileSaveInfoZipKey];
  221. [aCoder encodeObject:self.country forKey:KMProfileSaveInfoCountryKey];
  222. [aCoder encodeObject:self.email forKey:KMProfileSaveInfoEmailKey];
  223. [aCoder encodeObject:self.tel forKey:KMProfileSaveInfoTelKey];
  224. [aCoder encodeObject:self.birthDate forKey:KMProfileSaveInfoBirthDateKey];
  225. [aCoder encodeObject:self.customInfoArray forKey:KMProfileSaveInfoCustomInfoKey];
  226. }
  227. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  228. if (self = [super init]) {
  229. _fullName = [aDecoder decodeObjectForKey:KMProfileSaveInfoFullNameKey] ?:@"";
  230. _firstName = [aDecoder decodeObjectForKey:KMProfileSaveInfoFirstNameKey] ?:@"";
  231. _middleName = [aDecoder decodeObjectForKey:KMProfileSaveInfoMiddleNameKey] ?:@"";
  232. _lastName = [aDecoder decodeObjectForKey:KMProfileSaveInfoLastNameKey] ?:@"";
  233. _street1 = [aDecoder decodeObjectForKey:KMProfileSaveInfoStreet1Key] ?:@"";
  234. _street2 = [aDecoder decodeObjectForKey:KMProfileSaveInfoStreet2Key] ?:@"";
  235. _city = [aDecoder decodeObjectForKey:KMProfileSaveInfoCityKey] ?:@"";
  236. _state = [aDecoder decodeObjectForKey:KMProfileSaveInfoStateKey] ?:@"";
  237. _zip = [aDecoder decodeObjectForKey:KMProfileSaveInfoZipKey] ?:@"";
  238. _country = [aDecoder decodeObjectForKey:KMProfileSaveInfoCountryKey] ?:@"";
  239. _email = [aDecoder decodeObjectForKey:KMProfileSaveInfoEmailKey] ?:@"";
  240. _tel = [aDecoder decodeObjectForKey:KMProfileSaveInfoTelKey] ?:@"";
  241. _birthDate = [aDecoder decodeObjectForKey:KMProfileSaveInfoBirthDateKey] ?:@"";
  242. _customInfoArray = [aDecoder decodeObjectForKey:KMProfileSaveInfoCustomInfoKey]?: [NSMutableArray array];
  243. }
  244. return self;
  245. }
  246. - (NSString *)description {
  247. return [NSString stringWithFormat:@"fullName:%@,firstName:%@,middleName:%@,lastName:%@,street1:%@,street2:%@,city:%@,zip:%@,country:%@,email:%@,tel:%@,birthdate:%@,customInfo:%@",self.fullName,self.firstName,self.middleName,self.lastName,self.street1,self.street2,self.city,self.zip,self.country,self.email,self.tel,self.birthDate,self.customInfoArray];
  248. }
  249. @end
  250. static NSString *TitleCellIdentifier = @"TitleCell";
  251. static NSString *NormalCellIdentifier = @"Normal";
  252. @interface KMProfileInfoWindowController () <NSTableViewDelegate, NSTableViewDataSource, NSTextFieldDelegate>
  253. @property (assign) IBOutlet NSTextField *titleLabel;
  254. @property (assign) IBOutlet NSTableView *tableView;
  255. @property (nonatomic, retain) NSMutableArray *tableContents;
  256. @property (assign) IBOutlet NSButton *backButton;
  257. @property (assign) IBOutlet NSButton *addButton;
  258. @property (assign) IBOutlet NSButton *addNewInfoButton;
  259. @property (assign) IBOutlet NSButton *removeNewInfoButton;
  260. @end
  261. @implementation KMProfileInfoWindowController
  262. - (void)dealloc {
  263. // [_tableContents release];
  264. [NSDistributedNotificationCenter.defaultCenter removeObserver:self];
  265. // [super dealloc];
  266. }
  267. #pragma mark - Life Cycle
  268. - (void)windowDidLoad {
  269. [super windowDidLoad];
  270. [self configuViews];
  271. [self fetchDisplayKeys];
  272. [self.tableView reloadData];
  273. [NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];
  274. }
  275. #pragma mark - Private Methods
  276. - (void)configuViews {
  277. self.titleLabel.stringValue = NSLocalizedString(@"My Profile", nil);
  278. //Button Part
  279. self.backButton.title = NSLocalizedString(@"Cancel", nil);
  280. self.addButton.title = NSLocalizedString(@"Add to Doc", nil);
  281. self.addButton.enabled = NO;
  282. self.removeNewInfoButton.enabled = NO;
  283. //NSMenu Part
  284. NSMenuItem *editItem = [self.tableView.menu itemAtIndex:0];
  285. NSMenuItem *addItem = [self.tableView.menu itemAtIndex:1];
  286. NSMenuItem *deleteItem = [self.tableView.menu itemAtIndex:2];
  287. editItem.title = NSLocalizedString(@"Edit", nil);
  288. addItem.title = NSLocalizedString(@"Add to Doc", nil);
  289. deleteItem.title = NSLocalizedString(@"Delete", nil);
  290. }
  291. - (void)fetchDisplayKeys {
  292. _tableContents = [NSMutableArray array];
  293. [_tableContents addObject:NSLocalizedString(@"Name", nil)];
  294. [_tableContents addObject:NSLocalizedString(@"Full Name", nil)];
  295. [_tableContents addObject:NSLocalizedString(@"First Name", nil)];
  296. [_tableContents addObject:NSLocalizedString(@"Middle Name", nil)];
  297. [_tableContents addObject:NSLocalizedString(@"Last Name", nil)];
  298. [_tableContents addObject:NSLocalizedString(@"Address", nil)];
  299. [_tableContents addObject:NSLocalizedString(@"Street 1", nil)];
  300. [_tableContents addObject:NSLocalizedString(@"Street 2", nil)];
  301. [_tableContents addObject:NSLocalizedString(@"City", nil)];
  302. [_tableContents addObject:NSLocalizedString(@"State ", nil)];
  303. [_tableContents addObject:NSLocalizedString(@"Zip", nil)];
  304. [_tableContents addObject:NSLocalizedString(@"Country", nil)];
  305. [_tableContents addObject:NSLocalizedString(@"Contact Information", nil)];
  306. [_tableContents addObject:NSLocalizedString(@"Email", nil)];
  307. [_tableContents addObject:NSLocalizedString(@"Tel", nil)];
  308. [_tableContents addObject:NSLocalizedString(@"Dates", nil)];
  309. [_tableContents addObject:NSLocalizedString(@"Date", nil)];
  310. [_tableContents addObject:NSLocalizedString(@"Birth Date", nil)];
  311. if ([KMProfileInfo sharedInstance].customInfoArray.count > 0) {
  312. [_tableContents addObject:NSLocalizedString(@"Custom", nil)];
  313. for (NSUInteger i = 0 ; i < [KMProfileInfo sharedInstance].customInfoArray.count; i ++) {
  314. NSDictionary *dict = [[KMProfileInfo sharedInstance].customInfoArray objectAtIndex:i];
  315. [_tableContents addObject:dict.allKeys.firstObject];
  316. }
  317. }
  318. }
  319. //- (void)configuTextField:(KMProfileCellView *)cellView {
  320. // NSTextField * textField = cellView.detailtextField;
  321. // NSInteger tag = textField.tag;
  322. // NSString *placeHoldString = @"";
  323. // NSString *nameString = @"";
  324. //
  325. // if (tag == 1) {
  326. // placeHoldString = NSLocalizedString(@"Add Full Name", nil);
  327. // nameString = [KMProfileInfo sharedInstance].fullName;
  328. // } else if (tag == 2) {
  329. // placeHoldString = NSLocalizedString(@"Add First Name", nil);
  330. // nameString = [KMProfileInfo sharedInstance].firstName;
  331. // } else if (tag == 3) {
  332. // placeHoldString = NSLocalizedString(@"Add Middle Name", nil);
  333. // nameString = [KMProfileInfo sharedInstance].middleName;
  334. // } else if (tag == 4) {
  335. // placeHoldString = NSLocalizedString(@"Add Last Name", nil);
  336. // nameString = [KMProfileInfo sharedInstance].lastName;
  337. // } else if (tag == 6) {
  338. // placeHoldString = NSLocalizedString(@"Add Street 1", nil);
  339. // nameString = [KMProfileInfo sharedInstance].street1;
  340. // } else if (tag == 7) {
  341. // placeHoldString = NSLocalizedString(@"Add Street 2", nil);
  342. // nameString = [KMProfileInfo sharedInstance].street2;
  343. // } else if (tag == 8) {
  344. //
  345. // placeHoldString = NSLocalizedString(@"Add City", nil);
  346. // nameString = [KMProfileInfo sharedInstance].city;
  347. // } else if (tag == 9) {
  348. // placeHoldString = NSLocalizedString(@"Add State", nil);
  349. // nameString = [KMProfileInfo sharedInstance].state;
  350. // } else if (tag == 10) {
  351. // placeHoldString = NSLocalizedString(@"Add Zip", nil);
  352. // nameString = [KMProfileInfo sharedInstance].zip;
  353. // } else if (tag == 11) {
  354. // placeHoldString = NSLocalizedString(@"Add Country", nil);
  355. // nameString = [KMProfileInfo sharedInstance].country;
  356. // } else if (tag == 13) {
  357. // placeHoldString = NSLocalizedString(@"Add Email", nil);
  358. // nameString = [KMProfileInfo sharedInstance].email;
  359. // } else if (tag == 14) {
  360. // placeHoldString = NSLocalizedString(@"Add Tel", nil);
  361. // nameString = [KMProfileInfo sharedInstance].tel;
  362. // } else if (tag == 16) {
  363. // placeHoldString = NSLocalizedString(@"Add Date", nil);
  364. // NSDate *date = [NSDate date];
  365. // NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
  366. // [formatter setDateFormat:@"YYYY/MM/dd"];
  367. // nameString = [formatter stringFromDate:date];
  368. // } else if (tag == 17) {
  369. // placeHoldString = NSLocalizedString(@"Add Birth Date", nil);
  370. // nameString = [KMProfileInfo sharedInstance].birthDate;
  371. // }
  372. // textField.stringValue = nameString;
  373. // if (nameString.length > 0) {
  374. // textField.hidden = NO;
  375. // cellView.addButton.hidden = YES;
  376. // } else {
  377. // cellView.addButton.title = placeHoldString;
  378. // textField.placeholderString = placeHoldString;
  379. // textField.hidden = YES;
  380. // cellView.addButton.tag = tag;
  381. // cellView.addButton.target = self;
  382. // [cellView.addButton setAction:@selector(addFileName_Add:)];
  383. // [cellView.addButton setTitleColor:[NSColor systemBlueColor]];
  384. // cellView.addButton.hidden = NO;
  385. //
  386. // }
  387. //}
  388. #pragma mark - NSTableViewDelegate / NSTableViewDataSources
  389. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
  390. return self.tableContents.count;
  391. }
  392. - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
  393. NSString *addingString = [self.tableContents objectAtIndex:row];
  394. NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
  395. if (([addingString isEqualToString:NSLocalizedString(@"Name", nil)] ||
  396. [addingString isEqualToString:NSLocalizedString(@"Address", nil)] ||
  397. [addingString isEqualToString:NSLocalizedString(@"Contact Information", nil)] ||
  398. [addingString isEqualToString:NSLocalizedString(@"Dates", nil)] ||
  399. [addingString isEqualToString:NSLocalizedString(@"Custom", nil)])
  400. && row <= customIndex) {
  401. return 24.0;
  402. } else {
  403. return 36.0;
  404. }
  405. }
  406. - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
  407. NSString *addingString = [self.tableContents objectAtIndex:row];
  408. NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
  409. NSTableCellView *tableCellView = nil;
  410. if (([addingString isEqualToString:NSLocalizedString(@"Name", nil)] ||
  411. [addingString isEqualToString:NSLocalizedString(@"Address", nil)] ||
  412. [addingString isEqualToString:NSLocalizedString(@"Contact Information", nil)] ||
  413. [addingString isEqualToString:NSLocalizedString(@"Dates", nil)] ||
  414. [addingString isEqualToString:NSLocalizedString(@"Custom", nil)])
  415. && row <= customIndex) {
  416. tableCellView = [tableView makeViewWithIdentifier:TitleCellIdentifier owner:tableColumn];
  417. // KMProfileTitleCellView *cellView = (KMProfileTitleCellView *)tableCellView;
  418. // cellView.textField.stringValue = addingString;
  419. // cellView.textField.editable = NO;
  420. // cellView.wantsLayer = YES;
  421. // if (@available(macOS 10.14, *)) {
  422. // if ([KMAppearance isDarkMode]) {
  423. // cellView.layer.backgroundColor = [NSColor colorWithRed:73/255.0 green:75/255.0 blue:77/255.0 alpha:1.0].CGColor;
  424. // } else {
  425. // cellView.layer.backgroundColor = [NSColor colorWithRed:219/255.0 green:219/255.0 blue:219/255.0 alpha:1.0].CGColor;
  426. // }
  427. // } else {
  428. // cellView.layer.backgroundColor = [NSColor colorWithRed:235.0/255.0 green:235.0/255.0 blue:235.0/255.0 alpha:1.0].CGColor;
  429. // }
  430. } else {
  431. tableCellView = [tableView makeViewWithIdentifier:NormalCellIdentifier owner:tableColumn];
  432. // KMProfileCellView *cellView = (KMProfileCellView *)tableCellView;
  433. // cellView.textField.stringValue = addingString;
  434. // if([cellView isKindOfClass:[KMProfileCellView class]]){
  435. // cellView.detailtextField.hidden = NO;
  436. // }
  437. // cellView.textField.textColor = [KMAppearance KMColor_Layout_H0];
  438. // if (customIndex > row) {
  439. // cellView.detailtextField.tag = row;
  440. // [self configuTextField:cellView];
  441. // cellView.textField.editable = NO;
  442. // cellView.textField.selectable = NO;
  443. // if ([addingString isEqualToString:NSLocalizedString(@"Date", nil)]) {
  444. // cellView.detailtextField.editable = NO;
  445. // cellView.detailtextField.selectable = NO;
  446. // }
  447. //
  448. // cellView.detailtextField.delegate = self;
  449. // } else {
  450. // NSInteger index = row - customIndex - 1;
  451. // NSDictionary *dict = [[KMProfileInfo sharedInstance].customInfoArray objectAtIndex:index];
  452. // cellView.addButton.hidden = YES;
  453. // cellView.detailtextField.stringValue = dict.allValues.firstObject;
  454. // cellView.detailtextField.placeholderString = NSLocalizedString(@"Type Value Here", nil);
  455. // cellView.textField.placeholderString = NSLocalizedString(@"Type Label Here", nil);
  456. // cellView.textField.editable = YES;
  457. // cellView.textField.selectable = YES;
  458. // cellView.detailtextField.editable = YES;
  459. // cellView.detailtextField.selectable = YES;
  460. // cellView.textField.delegate = self;
  461. // cellView.detailtextField.delegate = self;
  462. //
  463. // }
  464. }
  465. return tableCellView;
  466. }
  467. - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row {
  468. NSString *addingString = [self.tableContents objectAtIndex:row];
  469. NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
  470. if (([addingString isEqualToString:NSLocalizedString(@"Name", nil)] ||
  471. [addingString isEqualToString:NSLocalizedString(@"Address", nil)] ||
  472. [addingString isEqualToString:NSLocalizedString(@"Contact Information", nil)] ||
  473. [addingString isEqualToString:NSLocalizedString(@"Dates", nil)] ||
  474. [addingString isEqualToString:NSLocalizedString(@"Custom", nil)])
  475. && row <= customIndex) {
  476. return NO;
  477. }
  478. return YES;
  479. }
  480. #pragma mark - NSTextFieldDelegate
  481. - (void)controlTextDidChange:(NSNotification *)obj {
  482. NSInteger currentRow = self.tableView.selectedRow;
  483. if (currentRow == -1) {
  484. return;
  485. }
  486. NSTextField *textField = (NSTextField *)obj.object;
  487. // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:currentRow makeIfNecessary:NO];
  488. if (currentRow == 1) {
  489. [KMProfileInfo sharedInstance].fullName = textField.stringValue;
  490. } else if (currentRow == 2) {
  491. [KMProfileInfo sharedInstance].firstName = textField.stringValue;
  492. } else if (currentRow == 3) {
  493. [KMProfileInfo sharedInstance].middleName = textField.stringValue;
  494. } else if (currentRow == 4) {
  495. [KMProfileInfo sharedInstance].lastName = textField.stringValue;
  496. } else if (currentRow == 6) {
  497. [KMProfileInfo sharedInstance].street1 = textField.stringValue;
  498. } else if (currentRow == 7) {
  499. [KMProfileInfo sharedInstance].street2 = textField.stringValue;
  500. } else if (currentRow == 8) {
  501. [KMProfileInfo sharedInstance].city = textField.stringValue;
  502. } else if (currentRow == 9) {
  503. [KMProfileInfo sharedInstance].state = textField.stringValue;
  504. } else if (currentRow == 10) {
  505. [KMProfileInfo sharedInstance].zip = textField.stringValue;
  506. } else if (currentRow == 11) {
  507. [KMProfileInfo sharedInstance].country = textField.stringValue;
  508. } else if (currentRow == 13) {
  509. [KMProfileInfo sharedInstance].email = textField.stringValue;
  510. } else if (currentRow == 14) {
  511. [KMProfileInfo sharedInstance].tel = textField.stringValue;
  512. } else if (currentRow == 16) {
  513. // [KMProfileInfo sharedInstance].date = textField.stringValue;
  514. } else if (currentRow == 17) {
  515. [KMProfileInfo sharedInstance].birthDate = textField.stringValue;
  516. } else if (currentRow >= 19 ) {
  517. NSInteger customIndex = currentRow - 19;
  518. // if (cellView) {
  519. // NSDictionary *dict = [[KMProfileInfo sharedInstance].customInfoArray objectAtIndex:customIndex];
  520. // NSString *originalKey = dict.allKeys.firstObject;
  521. // NSString *originalValue = dict.allValues.firstObject;
  522. // if ([textField isEqual:cellView.textField]) {
  523. // NSDictionary *newDict = @{textField.stringValue : originalValue};
  524. // [[KMProfileInfo sharedInstance] replaceObjectInCustomInfoArrayAtIndex:customIndex withObject:newDict];
  525. // } else if ([textField isEqual:cellView.detailtextField]){
  526. // NSDictionary *newDict = @{originalKey:textField.stringValue};
  527. // [[KMProfileInfo sharedInstance] replaceObjectInCustomInfoArrayAtIndex:customIndex withObject:newDict];
  528. // }
  529. // }
  530. }
  531. // if (cellView.detailtextField.stringValue.length > 0 ) {
  532. // self.addButton.enabled = YES;
  533. // } else {
  534. // self.addButton.enabled = NO;
  535. // }
  536. }
  537. - (void)controlTextDidBeginEditing:(NSNotification *)obj {
  538. NSTextField *textField = obj.object;
  539. BOOL isDarkMode = NO;
  540. if (@available(macOS 10.14, *)) {
  541. if ([[NSApplication sharedApplication].effectiveAppearance isEqual: [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]]) {
  542. isDarkMode =YES;
  543. }
  544. }
  545. textField.textColor = [NSColor labelColor];
  546. }
  547. - (void)controlTextDidEndEditing:(NSNotification *)obj;
  548. {
  549. NSTextField *textField = obj.object;
  550. NSInteger tag = textField.tag;
  551. // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:textField.tag makeIfNecessary:NO];
  552. // if ([cellView isKindOfClass:[KMProfileCellView class]] && tag > 0) {
  553. // NSString *placeHoldString = @"";
  554. //
  555. // if (tag == 1) {
  556. // placeHoldString = NSLocalizedString(@"Add Full Name", nil);
  557. // } else if (tag == 2) {
  558. // placeHoldString = NSLocalizedString(@"Add First Name", nil);
  559. // } else if (tag == 3) {
  560. // placeHoldString = NSLocalizedString(@"Add Middle Name", nil);
  561. // } else if (tag == 4) {
  562. // placeHoldString = NSLocalizedString(@"Add Last Name", nil);
  563. // } else if (tag == 6) {
  564. // placeHoldString = NSLocalizedString(@"Add Street 1", nil);
  565. // } else if (tag == 7) {
  566. // placeHoldString = NSLocalizedString(@"Add Street 2", nil);
  567. // } else if (tag == 8) {
  568. // placeHoldString = NSLocalizedString(@"Add City", nil);
  569. // } else if (tag == 9) {
  570. // placeHoldString = NSLocalizedString(@"Add State", nil);
  571. // } else if (tag == 10) {
  572. // placeHoldString = NSLocalizedString(@"Add Zip", nil);
  573. // } else if (tag == 11) {
  574. // placeHoldString = NSLocalizedString(@"Add Country", nil);
  575. // } else if (tag == 13) {
  576. // placeHoldString = NSLocalizedString(@"Add Email", nil);
  577. // } else if (tag == 14) {
  578. // placeHoldString = NSLocalizedString(@"Add Tel", nil);
  579. // } else if (tag == 16) {
  580. // placeHoldString = NSLocalizedString(@"Add Date", nil);
  581. // } else if (tag == 17) {
  582. // placeHoldString = NSLocalizedString(@"Add Birth Date", nil);
  583. // }
  584. //
  585. // if (textField.stringValue.length > 0) {
  586. // textField.textColor = [NSColor secondaryLabelColor];
  587. // textField.hidden = NO;
  588. // cellView.addButton.hidden = YES;
  589. // } else {
  590. //// textField.hidden = YES; //自定义新增,输入Label后,value为空时推出编辑,就无法填写了
  591. // textField.placeholderString = @"";
  592. // cellView.addButton.hidden = NO;
  593. // cellView.addButton.tag = tag;
  594. // cellView.addButton.target = self;
  595. // [cellView.addButton setAction:@selector(addFileName_Add:)];
  596. // cellView.addButton.title = placeHoldString;
  597. // [cellView.addButton setTitleColor:[NSColor systemBlueColor]];
  598. // }
  599. // }
  600. }
  601. - (void)tableViewSelectionDidChange:(NSNotification *)notification {
  602. if (self.tableView.selectedRow == NSNotFound || self.tableView.selectedRow == -1) {
  603. return;
  604. }
  605. // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.selectedRow makeIfNecessary:NO];
  606. // self.addButton.enabled = cellView.detailtextField.stringValue.length > 0;
  607. //
  608. // for (NSInteger i = 0; i < self.tableView.numberOfRows; i++) {
  609. // NSTableCellView *cellView = [self.tableView viewAtColumn:0 row:i makeIfNecessary:NO];
  610. // if ([cellView isKindOfClass:[KMProfileCellView class]]) {
  611. // KMProfileCellView *cellView1 = (KMProfileCellView *)cellView;
  612. // if (i == self.tableView.selectedRow) {
  613. // cellView1.detailtextField.textColor = [NSColor whiteColor];
  614. // [cellView1.addButton setTitleColor:[NSColor whiteColor]];
  615. // } else {
  616. // cellView1.detailtextField.textColor = [NSColor secondaryLabelColor];
  617. // [cellView1.addButton setTitleColor:[NSColor systemBlueColor]];
  618. // }
  619. // }
  620. // }
  621. NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
  622. if (self.tableView.selectedRow > customIndex) {
  623. self.removeNewInfoButton.enabled = YES;
  624. } else {
  625. self.removeNewInfoButton.enabled = NO;
  626. }
  627. }
  628. #pragma mark - Button Action
  629. - (IBAction)buttonClicked_back:(id)sender {
  630. // [self dismissSheet:sender];
  631. }
  632. - (IBAction)buttonClicked_add:(id)sender {
  633. NSInteger selectRow =self.tableView.selectedRow;
  634. if (selectRow >= 0) {
  635. if (self.callBack) {
  636. // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.selectedRow makeIfNecessary:NO];
  637. // self.callBack(cellView.detailtextField.stringValue);
  638. // [self dismissSheet:sender];
  639. }
  640. }
  641. }
  642. - (IBAction)buttonClicked_addNewInfo:(id)sender {
  643. NSDictionary *dict = @{@"":@""};
  644. [[KMProfileInfo sharedInstance] insertObject:dict inCustomInfoArrayAtIndex:[KMProfileInfo sharedInstance].customInfoArray.count];
  645. [self fetchDisplayKeys];
  646. [self.tableView reloadData];
  647. if (self.tableView.numberOfRows > 1) {
  648. [self.tableView scrollRowToVisible:self.tableView.numberOfRows - 1];
  649. NSIndexSet *set = [NSIndexSet indexSetWithIndex:self.tableView.numberOfRows - 1];
  650. [self.tableView selectRowIndexes:set byExtendingSelection:NO];
  651. // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.selectedRow makeIfNecessary:NO];
  652. // cellView.addButton.hidden = YES;
  653. // [cellView.textField becomeFirstResponder];
  654. };
  655. }
  656. - (IBAction)buttonClicked_RemoveNewInfo:(id)sender
  657. {
  658. NSInteger editIndex = self.tableView.selectedRow;
  659. NSUInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
  660. if (editIndex - customIndex - 1 < [KMProfileInfo sharedInstance].customInfoArray.count) {
  661. [[KMProfileInfo sharedInstance] removeObjectFromCustomInfoArrayAtIndex:editIndex - customIndex - 1];
  662. [self fetchDisplayKeys];
  663. [self.tableView reloadData];
  664. self.removeNewInfoButton.enabled = NO;
  665. }
  666. }
  667. - (IBAction)addFileName_Add:(NSButton *)sender
  668. {
  669. [self.window makeFirstResponder:nil];
  670. NSInteger tag = sender.tag;
  671. // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:tag makeIfNecessary:NO];
  672. // if ([cellView isKindOfClass:[KMProfileCellView class]]) {
  673. // cellView.addButton.hidden = YES;
  674. // cellView.detailtextField.hidden = NO;
  675. // [cellView.detailtextField becomeFirstResponder];
  676. // }
  677. }
  678. #pragma mark - NSMenu Action
  679. - (IBAction)menuItemClicked_Edit:(id)sender {
  680. // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.clickedRow makeIfNecessary:NO];
  681. // NSIndexSet *set = [NSIndexSet indexSetWithIndex:self.tableView.clickedRow];
  682. // [self.tableView selectRowIndexes:set byExtendingSelection:NO];
  683. // if (cellView.textField.editable) {
  684. // if (cellView.textField.stringValue.length == 0) {
  685. // [cellView.textField becomeFirstResponder];
  686. // }else {
  687. // [cellView.detailtextField becomeFirstResponder];
  688. // }
  689. // } else {
  690. // [cellView.detailtextField becomeFirstResponder];
  691. // }
  692. }
  693. - (IBAction)menuItemClicked_Add:(id)sender {
  694. // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.clickedRow makeIfNecessary:NO];
  695. // if (self.callBack) {
  696. // self.callBack(cellView.detailtextField.stringValue);
  697. // [self dismissSheet:sender];
  698. // }
  699. }
  700. - (IBAction)menuItemClicked_Delete:(id)sender {
  701. NSInteger editIndex = self.tableView.clickedRow;
  702. NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
  703. if (editIndex < customIndex ) {
  704. switch (editIndex) {
  705. case 1:
  706. [KMProfileInfo sharedInstance].fullName = @"";
  707. break;
  708. case 2:
  709. [KMProfileInfo sharedInstance].firstName = @"";
  710. break;
  711. case 3:
  712. [KMProfileInfo sharedInstance].middleName = @"";
  713. break;
  714. case 4:
  715. [KMProfileInfo sharedInstance].lastName = @"";
  716. break;
  717. case 6:
  718. [KMProfileInfo sharedInstance].street1 = @"";
  719. break;
  720. case 7:
  721. [KMProfileInfo sharedInstance].street2 = @"";
  722. break;
  723. case 8:
  724. [KMProfileInfo sharedInstance].city = @"";
  725. break;
  726. case 9:
  727. [KMProfileInfo sharedInstance].state = @"";
  728. break;
  729. case 10:
  730. [KMProfileInfo sharedInstance].zip = @"";
  731. break;
  732. case 11:
  733. [KMProfileInfo sharedInstance].country = @"";
  734. break;
  735. case 13:
  736. [KMProfileInfo sharedInstance].email = @"";
  737. break;
  738. case 14:
  739. [KMProfileInfo sharedInstance].tel = @"";
  740. break;
  741. case 16:
  742. // [KMProfileInfo sharedInstance].date = @"";
  743. break;
  744. case 17:
  745. [KMProfileInfo sharedInstance].birthDate = @"";
  746. break;
  747. default:
  748. break;
  749. }
  750. } else {
  751. [[KMProfileInfo sharedInstance] removeObjectFromCustomInfoArrayAtIndex:editIndex - customIndex - 1];
  752. }
  753. [self fetchDisplayKeys];
  754. [self.tableView reloadData];
  755. }
  756. - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
  757. // NSTableCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.clickedRow makeIfNecessary:NO];
  758. // if (menuItem.action == @selector(menuItemClicked_Edit:)) {
  759. // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) {
  760. // return NO;
  761. // } else {
  762. // KMProfileCellView *cellView1 = (KMProfileCellView *)cellView;
  763. // if ([cellView1.textField.stringValue isEqualToString:NSLocalizedString(@"Date", nil)]) {
  764. // return NO;
  765. // }
  766. // return YES;
  767. // }
  768. //
  769. // } else if (menuItem.action == @selector(menuItemClicked_Add:)) {
  770. // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) {
  771. // return NO;
  772. // } else {
  773. // KMProfileCellView *cell = (KMProfileCellView *)cellView;
  774. // return cell.detailtextField.stringValue.length > 0;
  775. // }
  776. // } else if (menuItem.action == @selector(menuItemClicked_Delete:)) {
  777. // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) {
  778. // return NO;
  779. // } else {
  780. // KMProfileCellView *cellView1 = (KMProfileCellView *)cellView;
  781. // if ([cellView1.textField.stringValue isEqualToString:NSLocalizedString(@"Date", nil)]) {
  782. // return NO;
  783. // }
  784. // return YES;
  785. // }
  786. // }
  787. return NO;
  788. }
  789. - (void)themeChanged:(NSNotification *)notification {
  790. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  791. [self updateViewColor];
  792. });
  793. }
  794. - (void)updateViewColor {
  795. [self.tableView reloadData];
  796. }
  797. @end