KMProfileInfoWindowController.m 35 KB

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