123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919 |
- //
- // KMProfileInfoWindowController.m
- // PDF Reader Pro Edition
- //
- // Created by 蒋志鹏 on 2019-07-19.
- //
- #import "KMProfileInfoWindowController.h"
- //#import "NSWindowController_SKExtensions.h"
- //#import "KMProfileCellView.h"
- //#import "KMProfileTitleCellView.h"
- //#import "NSButton+TitleColor.h"
- #import <PDF_Master-Swift.h>
- static NSString *KMProfileUnarchiveKey = @"KMProfileUnarchiveKeyTemp1";
- static NSString *KMProfileSaveInfoFullNameKey = @"KMProfileSaveInfoFullNameKey";
- static NSString *KMProfileSaveInfoFirstNameKey = @"KMProfileSaveInfoFirstNameKey";
- static NSString *KMProfileSaveInfoMiddleNameKey = @"KMProfileSaveInfoMiddleNameKey";
- static NSString *KMProfileSaveInfoLastNameKey = @"KMProfileSaveInfoLastNameKey";
- static NSString *KMProfileSaveInfoStreet1Key = @"KMProfileSaveInfoStreet1Key";
- static NSString *KMProfileSaveInfoStreet2Key = @"KMProfileSaveInfoStreet2Key";
- static NSString *KMProfileSaveInfoCityKey = @"KMProfileSaveInfoCityKey";
- static NSString *KMProfileSaveInfoStateKey = @"KMProfileSaveInfoStateKey";
- static NSString *KMProfileSaveInfoZipKey = @"KMProfileSaveInfoZipKey";
- static NSString *KMProfileSaveInfoCountryKey = @"KMProfileSaveInfoCountryKey";
- static NSString *KMProfileSaveInfoEmailKey = @"KMProfileSaveInfoEmailKey";
- static NSString *KMProfileSaveInfoTelKey = @"KMProfileSaveInfoTelKey";
- static NSString *KMProfileSaveInfoBirthDateKey = @"KMProfileSaveInfoBirthDateKey";
- static NSString *KMProfileSaveInfoCustomInfoKey = @"KMProfileSaveInfoCustomInfoKey";
- @interface KMProfileInfo : NSObject <NSCoding>
- @property (nonatomic, retain) NSString *fullName;
- @property (nonatomic, retain) NSString *firstName;
- @property (nonatomic, retain) NSString *middleName;
- @property (nonatomic, retain) NSString *lastName;
- @property (nonatomic, retain) NSString *street1;
- @property (nonatomic, retain) NSString *street2;
- @property (nonatomic, retain) NSString *city;
- @property (nonatomic, retain) NSString *state;
- @property (nonatomic, retain) NSString *zip;
- @property (nonatomic, retain) NSString *country;
- @property (nonatomic, retain) NSString *email;
- @property (nonatomic, retain) NSString *tel;
- @property (nonatomic, retain) NSString *birthDate;
- @property (nonatomic, retain) NSMutableArray *customInfoArray;
- @end
- @implementation KMProfileInfo
- #pragma mark - Getter & setter
- - (void)setFullName:(NSString *)fullName {
- // if (![fullName isEqualToString:_fullName]) {
- // [_fullName release];
- // _fullName = [fullName retain];
-
- _fullName = fullName;
- [self saveInfo];
- // }
- }
- - (void)setFirstName:(NSString *)firstName {
- // if (![_firstName isEqualToString:firstName]) {
- // [_firstName release];
- // _firstName = [firstName retain];
-
- _firstName = firstName;
- [self saveInfo];
- // }
- }
- - (void)setMiddleName:(NSString *)middleName {
- // if (![_middleName isEqualToString:middleName]) {
- // [_middleName release];
- // _middleName = [middleName retain];
-
- _middleName = middleName;
- [self saveInfo];
- // }
- }
- - (void)setLastName:(NSString *)lastName {
- // if (![_lastName isEqualToString:lastName]) {
- // [_lastName release];
- // _lastName = [lastName retain];
- _lastName = lastName;
- [self saveInfo];
- // }
- }
- - (void)setStreet1:(NSString *)street1 {
- // if (![_street1 isEqualToString:street1]) {
- // [_street1 release];
- // _street1 = [street1 retain];
-
- _street1 = street1;
- [self saveInfo];
- // }
- }
- - (void)setStreet2:(NSString *)street2 {
- // if (![_street2 isEqualToString:street2]) {
- // [_street2 release];
- // _street2 = [street2 retain];
-
- _street2 = street2;
- [self saveInfo];
- // }
- }
- - (void)setCity:(NSString *)city {
- // if (![_city isEqualToString:city]) {
- // [_city release];
- // _city = [city retain];
-
- _city = city;
- [self saveInfo];
- // }
- }
- - (void)setState:(NSString *)state {
- // if (![_state isEqualToString:state]) {
- // [_state release];
- // _state = [state retain];
-
- _state = state;
- [self saveInfo];
- // }
- }
- - (void)setZip:(NSString *)zip {
- // if (![_zip isEqualToString:zip]) {
- // [_zip release];
- // _zip = [zip retain];
-
- _zip = zip;
- [self saveInfo];
- // }
- }
- - (void)setCountry:(NSString *)country {
- // if (![_country isEqualToString:country]) {
- // [_country release];
- // _country = [country retain];
-
- _country = country;
- [self saveInfo];
- // }
- }
- - (void)setEmail:(NSString *)email {
- // if (![_email isEqualToString:email]) {
- // [_email release];
- // _email = [email retain];
-
- _email = email;
- [self saveInfo];
- // }
- }
- - (void)setTel:(NSString *)tel {
- // if (![_tel isEqualToString:tel]) {
- // [_tel release];
- // _tel = [tel retain];
- _tel = tel;
- [self saveInfo];
- // }
- }
- - (void)setBirthDate:(NSString *)birthDate {
- // if (![_birthDate isEqualToString:birthDate]) {
- // [_birthDate release];
- // _birthDate = [birthDate retain];
-
- _birthDate = birthDate;
- [self saveInfo];
- // }
- }
- - (void)setCustomInfoArray:(NSMutableArray *)customInfoArray {
- // if (![customInfoArray isEqual:customInfoArray]) {
- // [customInfoArray release];
- // _customInfoArray = [customInfoArray retain];
-
- _customInfoArray = customInfoArray;
- [self saveInfo];
- // }
- }
- #pragma mark - Init Methods
- + (instancetype)sharedInstance {
- static dispatch_once_t onceToken;
- static KMProfileInfo *manager = nil;
- dispatch_once(&onceToken, ^{
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- NSData *encodedObject = [defaults objectForKey:KMProfileUnarchiveKey];
- if (!encodedObject) {
- manager = [[KMProfileInfo alloc] init];
- [manager setBaseInfo];
- [manager saveInfo];
- } else {
- manager = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
- }
-
- });
- return manager;
- }
- #pragma mark - Private Methods
- - (void)saveInfo {
- NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self];
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- [defaults setObject:encodedObject forKey:KMProfileUnarchiveKey];
- [defaults synchronize];
- }
- - (void)setBaseInfo {
- _fullName = @"";
- _firstName = @"";
- _middleName = @"";
- _lastName = @"";
-
- _street1 = @"";
- _street2 = @"";
- _city = @"";
- _state = @"";
- _zip = @"";
- _country = @"";
-
- _email = @"";
- _tel = @"";
-
- _birthDate = @"";
- _customInfoArray = [NSMutableArray array];
- }
- - (void)replaceObjectInCustomInfoArrayAtIndex:(NSInteger)index withObject:(id)object {
- [self.customInfoArray replaceObjectAtIndex:index withObject:object];
- [self saveInfo];
- }
- - (void)removeObjectFromCustomInfoArrayAtIndex:(NSUInteger)index {
- [self.customInfoArray removeObjectAtIndex:index];
- [self saveInfo];
- }
- - (void)insertObject:(id)object inCustomInfoArrayAtIndex:(NSInteger)index {
- [self.customInfoArray insertObject:object atIndex:index];
- [self saveInfo];
- }
- #pragma mark - NSCoding
- - (void)encodeWithCoder:(NSCoder *)aCoder {
- [aCoder encodeObject:self.fullName forKey:KMProfileSaveInfoFullNameKey];
- [aCoder encodeObject:self.firstName forKey:KMProfileSaveInfoFirstNameKey];
- [aCoder encodeObject:self.middleName forKey:KMProfileSaveInfoMiddleNameKey];
- [aCoder encodeObject:self.lastName forKey:KMProfileSaveInfoLastNameKey];
-
- [aCoder encodeObject:self.street1 forKey:KMProfileSaveInfoStreet1Key];
- [aCoder encodeObject:self.street2 forKey:KMProfileSaveInfoStreet2Key];
- [aCoder encodeObject:self.city forKey:KMProfileSaveInfoCityKey];
- [aCoder encodeObject:self.state forKey:KMProfileSaveInfoStateKey];
- [aCoder encodeObject:self.zip forKey:KMProfileSaveInfoZipKey];
- [aCoder encodeObject:self.country forKey:KMProfileSaveInfoCountryKey];
-
- [aCoder encodeObject:self.email forKey:KMProfileSaveInfoEmailKey];
- [aCoder encodeObject:self.tel forKey:KMProfileSaveInfoTelKey];
-
- [aCoder encodeObject:self.birthDate forKey:KMProfileSaveInfoBirthDateKey];
- [aCoder encodeObject:self.customInfoArray forKey:KMProfileSaveInfoCustomInfoKey];
- }
- - (instancetype)initWithCoder:(NSCoder *)aDecoder {
- if (self = [super init]) {
- _fullName = [aDecoder decodeObjectForKey:KMProfileSaveInfoFullNameKey] ?:@"";
- _firstName = [aDecoder decodeObjectForKey:KMProfileSaveInfoFirstNameKey] ?:@"";
- _middleName = [aDecoder decodeObjectForKey:KMProfileSaveInfoMiddleNameKey] ?:@"";
- _lastName = [aDecoder decodeObjectForKey:KMProfileSaveInfoLastNameKey] ?:@"";
-
- _street1 = [aDecoder decodeObjectForKey:KMProfileSaveInfoStreet1Key] ?:@"";
- _street2 = [aDecoder decodeObjectForKey:KMProfileSaveInfoStreet2Key] ?:@"";
- _city = [aDecoder decodeObjectForKey:KMProfileSaveInfoCityKey] ?:@"";
- _state = [aDecoder decodeObjectForKey:KMProfileSaveInfoStateKey] ?:@"";
- _zip = [aDecoder decodeObjectForKey:KMProfileSaveInfoZipKey] ?:@"";
- _country = [aDecoder decodeObjectForKey:KMProfileSaveInfoCountryKey] ?:@"";
-
- _email = [aDecoder decodeObjectForKey:KMProfileSaveInfoEmailKey] ?:@"";
-
- _tel = [aDecoder decodeObjectForKey:KMProfileSaveInfoTelKey] ?:@"";
-
- _birthDate = [aDecoder decodeObjectForKey:KMProfileSaveInfoBirthDateKey] ?:@"";
- _customInfoArray = [aDecoder decodeObjectForKey:KMProfileSaveInfoCustomInfoKey]?: [NSMutableArray array];
- }
- return self;
- }
- - (NSString *)description {
- 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];
- }
- @end
- static NSString *TitleCellIdentifier = @"TitleCell";
- static NSString *NormalCellIdentifier = @"Normal";
- @interface KMProfileInfoWindowController () <NSTableViewDelegate, NSTableViewDataSource, NSTextFieldDelegate>
- @property (assign) IBOutlet NSTextField *titleLabel;
- @property (assign) IBOutlet NSTableView *tableView;
- @property (nonatomic, retain) NSMutableArray *tableContents;
- @property (assign) IBOutlet NSButton *backButton;
- @property (assign) IBOutlet NSButton *addButton;
- @property (assign) IBOutlet NSButton *addNewInfoButton;
- @property (assign) IBOutlet NSButton *removeNewInfoButton;
- @end
- @implementation KMProfileInfoWindowController
- - (void)dealloc {
-
- // [_tableContents release];
- [NSDistributedNotificationCenter.defaultCenter removeObserver:self];
-
- // [super dealloc];
- }
- #pragma mark - Life Cycle
- - (void)windowDidLoad {
- [super windowDidLoad];
-
- [self configuViews];
- [self fetchDisplayKeys];
- [self.tableView reloadData];
- [NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];
- }
- #pragma mark - Private Methods
- - (void)configuViews {
-
- self.titleLabel.stringValue = NSLocalizedString(@"My Profile", nil);
- //Button Part
- self.backButton.title = NSLocalizedString(@"Cancel", nil);
- self.addButton.title = NSLocalizedString(@"Add to Doc", nil);
- self.addButton.enabled = NO;
- self.removeNewInfoButton.enabled = NO;
- //NSMenu Part
- NSMenuItem *editItem = [self.tableView.menu itemAtIndex:0];
- NSMenuItem *addItem = [self.tableView.menu itemAtIndex:1];
- NSMenuItem *deleteItem = [self.tableView.menu itemAtIndex:2];
-
- editItem.title = NSLocalizedString(@"Edit", nil);
- addItem.title = NSLocalizedString(@"Add to Doc", nil);
- deleteItem.title = NSLocalizedString(@"Delete", nil);
- }
- - (void)fetchDisplayKeys {
- _tableContents = [NSMutableArray array];
- [_tableContents addObject:NSLocalizedString(@"Name", nil)];
- [_tableContents addObject:NSLocalizedString(@"Full Name", nil)];
- [_tableContents addObject:NSLocalizedString(@"First Name", nil)];
- [_tableContents addObject:NSLocalizedString(@"Middle Name", nil)];
- [_tableContents addObject:NSLocalizedString(@"Last Name", nil)];
-
- [_tableContents addObject:NSLocalizedString(@"Address", nil)];
- [_tableContents addObject:NSLocalizedString(@"Street 1", nil)];
- [_tableContents addObject:NSLocalizedString(@"Street 2", nil)];
- [_tableContents addObject:NSLocalizedString(@"City", nil)];
- [_tableContents addObject:NSLocalizedString(@"State ", nil)];
- [_tableContents addObject:NSLocalizedString(@"Zip", nil)];
- [_tableContents addObject:NSLocalizedString(@"Country", nil)];
-
- [_tableContents addObject:NSLocalizedString(@"Contact Information", nil)];
- [_tableContents addObject:NSLocalizedString(@"Email", nil)];
- [_tableContents addObject:NSLocalizedString(@"Tel", nil)];
-
- [_tableContents addObject:NSLocalizedString(@"Dates", nil)];
- [_tableContents addObject:NSLocalizedString(@"Date", nil)];
- [_tableContents addObject:NSLocalizedString(@"Birth Date", nil)];
- if ([KMProfileInfo sharedInstance].customInfoArray.count > 0) {
- [_tableContents addObject:NSLocalizedString(@"Custom", nil)];
- for (NSUInteger i = 0 ; i < [KMProfileInfo sharedInstance].customInfoArray.count; i ++) {
- NSDictionary *dict = [[KMProfileInfo sharedInstance].customInfoArray objectAtIndex:i];
- [_tableContents addObject:dict.allKeys.firstObject];
- }
- }
- }
- //- (void)configuTextField:(KMProfileCellView *)cellView {
- // NSTextField * textField = cellView.detailtextField;
- // NSInteger tag = textField.tag;
- // NSString *placeHoldString = @"";
- // NSString *nameString = @"";
- //
- // if (tag == 1) {
- // placeHoldString = NSLocalizedString(@"Add Full Name", nil);
- // nameString = [KMProfileInfo sharedInstance].fullName;
- // } else if (tag == 2) {
- // placeHoldString = NSLocalizedString(@"Add First Name", nil);
- // nameString = [KMProfileInfo sharedInstance].firstName;
- // } else if (tag == 3) {
- // placeHoldString = NSLocalizedString(@"Add Middle Name", nil);
- // nameString = [KMProfileInfo sharedInstance].middleName;
- // } else if (tag == 4) {
- // placeHoldString = NSLocalizedString(@"Add Last Name", nil);
- // nameString = [KMProfileInfo sharedInstance].lastName;
- // } else if (tag == 6) {
- // placeHoldString = NSLocalizedString(@"Add Street 1", nil);
- // nameString = [KMProfileInfo sharedInstance].street1;
- // } else if (tag == 7) {
- // placeHoldString = NSLocalizedString(@"Add Street 2", nil);
- // nameString = [KMProfileInfo sharedInstance].street2;
- // } else if (tag == 8) {
- //
- // placeHoldString = NSLocalizedString(@"Add City", nil);
- // nameString = [KMProfileInfo sharedInstance].city;
- // } else if (tag == 9) {
- // placeHoldString = NSLocalizedString(@"Add State", nil);
- // nameString = [KMProfileInfo sharedInstance].state;
- // } else if (tag == 10) {
- // placeHoldString = NSLocalizedString(@"Add Zip", nil);
- // nameString = [KMProfileInfo sharedInstance].zip;
- // } else if (tag == 11) {
- // placeHoldString = NSLocalizedString(@"Add Country", nil);
- // nameString = [KMProfileInfo sharedInstance].country;
- // } else if (tag == 13) {
- // placeHoldString = NSLocalizedString(@"Add Email", nil);
- // nameString = [KMProfileInfo sharedInstance].email;
- // } else if (tag == 14) {
- // placeHoldString = NSLocalizedString(@"Add Tel", nil);
- // nameString = [KMProfileInfo sharedInstance].tel;
- // } else if (tag == 16) {
- // placeHoldString = NSLocalizedString(@"Add Date", nil);
- // NSDate *date = [NSDate date];
- // NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
- // [formatter setDateFormat:@"YYYY/MM/dd"];
- // nameString = [formatter stringFromDate:date];
- // } else if (tag == 17) {
- // placeHoldString = NSLocalizedString(@"Add Birth Date", nil);
- // nameString = [KMProfileInfo sharedInstance].birthDate;
- // }
- // textField.stringValue = nameString;
- // if (nameString.length > 0) {
- // textField.hidden = NO;
- // cellView.addButton.hidden = YES;
- // } else {
- // cellView.addButton.title = placeHoldString;
- // textField.placeholderString = placeHoldString;
- // textField.hidden = YES;
- // cellView.addButton.tag = tag;
- // cellView.addButton.target = self;
- // [cellView.addButton setAction:@selector(addFileName_Add:)];
- // [cellView.addButton setTitleColor:[NSColor systemBlueColor]];
- // cellView.addButton.hidden = NO;
- //
- // }
- //}
- #pragma mark - NSTableViewDelegate / NSTableViewDataSources
- - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
- return self.tableContents.count;
- }
- - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
- NSString *addingString = [self.tableContents objectAtIndex:row];
- NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
- if (([addingString isEqualToString:NSLocalizedString(@"Name", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Address", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Contact Information", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Dates", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Custom", nil)])
- && row <= customIndex) {
- return 24.0;
- } else {
- return 36.0;
- }
- }
- - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
- NSString *addingString = [self.tableContents objectAtIndex:row];
- NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
- NSTableCellView *tableCellView = nil;
- if (([addingString isEqualToString:NSLocalizedString(@"Name", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Address", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Contact Information", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Dates", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Custom", nil)])
- && row <= customIndex) {
- tableCellView = [tableView makeViewWithIdentifier:TitleCellIdentifier owner:tableColumn];
- // KMProfileTitleCellView *cellView = (KMProfileTitleCellView *)tableCellView;
- // cellView.textField.stringValue = addingString;
- // cellView.textField.editable = NO;
- // cellView.wantsLayer = YES;
- // if (@available(macOS 10.14, *)) {
- // if ([KMAppearance isDarkMode]) {
- // cellView.layer.backgroundColor = [NSColor colorWithRed:73/255.0 green:75/255.0 blue:77/255.0 alpha:1.0].CGColor;
- // } else {
- // cellView.layer.backgroundColor = [NSColor colorWithRed:219/255.0 green:219/255.0 blue:219/255.0 alpha:1.0].CGColor;
- // }
- // } else {
- // cellView.layer.backgroundColor = [NSColor colorWithRed:235.0/255.0 green:235.0/255.0 blue:235.0/255.0 alpha:1.0].CGColor;
- // }
-
- } else {
- tableCellView = [tableView makeViewWithIdentifier:NormalCellIdentifier owner:tableColumn];
- // KMProfileCellView *cellView = (KMProfileCellView *)tableCellView;
- // cellView.textField.stringValue = addingString;
- // if([cellView isKindOfClass:[KMProfileCellView class]]){
- // cellView.detailtextField.hidden = NO;
- // }
- // cellView.textField.textColor = [KMAppearance KMColor_Layout_H0];
- // if (customIndex > row) {
- // cellView.detailtextField.tag = row;
- // [self configuTextField:cellView];
- // cellView.textField.editable = NO;
- // cellView.textField.selectable = NO;
- // if ([addingString isEqualToString:NSLocalizedString(@"Date", nil)]) {
- // cellView.detailtextField.editable = NO;
- // cellView.detailtextField.selectable = NO;
- // }
- //
- // cellView.detailtextField.delegate = self;
- // } else {
- // NSInteger index = row - customIndex - 1;
- // NSDictionary *dict = [[KMProfileInfo sharedInstance].customInfoArray objectAtIndex:index];
- // cellView.addButton.hidden = YES;
- // cellView.detailtextField.stringValue = dict.allValues.firstObject;
- // cellView.detailtextField.placeholderString = NSLocalizedString(@"Type Value Here", nil);
- // cellView.textField.placeholderString = NSLocalizedString(@"Type Label Here", nil);
- // cellView.textField.editable = YES;
- // cellView.textField.selectable = YES;
- // cellView.detailtextField.editable = YES;
- // cellView.detailtextField.selectable = YES;
- // cellView.textField.delegate = self;
- // cellView.detailtextField.delegate = self;
- //
- // }
- }
- return tableCellView;
- }
- - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row {
- NSString *addingString = [self.tableContents objectAtIndex:row];
- NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
- if (([addingString isEqualToString:NSLocalizedString(@"Name", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Address", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Contact Information", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Dates", nil)] ||
- [addingString isEqualToString:NSLocalizedString(@"Custom", nil)])
- && row <= customIndex) {
- return NO;
- }
- return YES;
- }
- #pragma mark - NSTextFieldDelegate
- - (void)controlTextDidChange:(NSNotification *)obj {
-
- NSInteger currentRow = self.tableView.selectedRow;
- if (currentRow == -1) {
- return;
- }
- NSTextField *textField = (NSTextField *)obj.object;
- // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:currentRow makeIfNecessary:NO];
- if (currentRow == 1) {
- [KMProfileInfo sharedInstance].fullName = textField.stringValue;
- } else if (currentRow == 2) {
- [KMProfileInfo sharedInstance].firstName = textField.stringValue;
- } else if (currentRow == 3) {
- [KMProfileInfo sharedInstance].middleName = textField.stringValue;
- } else if (currentRow == 4) {
- [KMProfileInfo sharedInstance].lastName = textField.stringValue;
- } else if (currentRow == 6) {
- [KMProfileInfo sharedInstance].street1 = textField.stringValue;
- } else if (currentRow == 7) {
- [KMProfileInfo sharedInstance].street2 = textField.stringValue;
- } else if (currentRow == 8) {
- [KMProfileInfo sharedInstance].city = textField.stringValue;
- } else if (currentRow == 9) {
- [KMProfileInfo sharedInstance].state = textField.stringValue;
- } else if (currentRow == 10) {
- [KMProfileInfo sharedInstance].zip = textField.stringValue;
- } else if (currentRow == 11) {
- [KMProfileInfo sharedInstance].country = textField.stringValue;
- } else if (currentRow == 13) {
- [KMProfileInfo sharedInstance].email = textField.stringValue;
- } else if (currentRow == 14) {
- [KMProfileInfo sharedInstance].tel = textField.stringValue;
- } else if (currentRow == 16) {
- // [KMProfileInfo sharedInstance].date = textField.stringValue;
- } else if (currentRow == 17) {
- [KMProfileInfo sharedInstance].birthDate = textField.stringValue;
- } else if (currentRow >= 19 ) {
- NSInteger customIndex = currentRow - 19;
- // if (cellView) {
- // NSDictionary *dict = [[KMProfileInfo sharedInstance].customInfoArray objectAtIndex:customIndex];
- // NSString *originalKey = dict.allKeys.firstObject;
- // NSString *originalValue = dict.allValues.firstObject;
- // if ([textField isEqual:cellView.textField]) {
- // NSDictionary *newDict = @{textField.stringValue : originalValue};
- // [[KMProfileInfo sharedInstance] replaceObjectInCustomInfoArrayAtIndex:customIndex withObject:newDict];
- // } else if ([textField isEqual:cellView.detailtextField]){
- // NSDictionary *newDict = @{originalKey:textField.stringValue};
- // [[KMProfileInfo sharedInstance] replaceObjectInCustomInfoArrayAtIndex:customIndex withObject:newDict];
- // }
- // }
- }
-
- // if (cellView.detailtextField.stringValue.length > 0 ) {
- // self.addButton.enabled = YES;
- // } else {
- // self.addButton.enabled = NO;
- // }
- }
- - (void)controlTextDidBeginEditing:(NSNotification *)obj {
- NSTextField *textField = obj.object;
- BOOL isDarkMode = NO;
- if (@available(macOS 10.14, *)) {
- if ([[NSApplication sharedApplication].effectiveAppearance isEqual: [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]]) {
- isDarkMode =YES;
- }
- }
- textField.textColor = [NSColor labelColor];
- }
- - (void)controlTextDidEndEditing:(NSNotification *)obj;
- {
- NSTextField *textField = obj.object;
- NSInteger tag = textField.tag;
- // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:textField.tag makeIfNecessary:NO];
- // if ([cellView isKindOfClass:[KMProfileCellView class]] && tag > 0) {
- // NSString *placeHoldString = @"";
- //
- // if (tag == 1) {
- // placeHoldString = NSLocalizedString(@"Add Full Name", nil);
- // } else if (tag == 2) {
- // placeHoldString = NSLocalizedString(@"Add First Name", nil);
- // } else if (tag == 3) {
- // placeHoldString = NSLocalizedString(@"Add Middle Name", nil);
- // } else if (tag == 4) {
- // placeHoldString = NSLocalizedString(@"Add Last Name", nil);
- // } else if (tag == 6) {
- // placeHoldString = NSLocalizedString(@"Add Street 1", nil);
- // } else if (tag == 7) {
- // placeHoldString = NSLocalizedString(@"Add Street 2", nil);
- // } else if (tag == 8) {
- // placeHoldString = NSLocalizedString(@"Add City", nil);
- // } else if (tag == 9) {
- // placeHoldString = NSLocalizedString(@"Add State", nil);
- // } else if (tag == 10) {
- // placeHoldString = NSLocalizedString(@"Add Zip", nil);
- // } else if (tag == 11) {
- // placeHoldString = NSLocalizedString(@"Add Country", nil);
- // } else if (tag == 13) {
- // placeHoldString = NSLocalizedString(@"Add Email", nil);
- // } else if (tag == 14) {
- // placeHoldString = NSLocalizedString(@"Add Tel", nil);
- // } else if (tag == 16) {
- // placeHoldString = NSLocalizedString(@"Add Date", nil);
- // } else if (tag == 17) {
- // placeHoldString = NSLocalizedString(@"Add Birth Date", nil);
- // }
- //
- // if (textField.stringValue.length > 0) {
- // textField.textColor = [NSColor secondaryLabelColor];
- // textField.hidden = NO;
- // cellView.addButton.hidden = YES;
- // } else {
- //// textField.hidden = YES; //自定义新增,输入Label后,value为空时推出编辑,就无法填写了
- // textField.placeholderString = @"";
- // cellView.addButton.hidden = NO;
- // cellView.addButton.tag = tag;
- // cellView.addButton.target = self;
- // [cellView.addButton setAction:@selector(addFileName_Add:)];
- // cellView.addButton.title = placeHoldString;
- // [cellView.addButton setTitleColor:[NSColor systemBlueColor]];
- // }
- // }
-
- }
- - (void)tableViewSelectionDidChange:(NSNotification *)notification {
- if (self.tableView.selectedRow == NSNotFound || self.tableView.selectedRow == -1) {
- return;
- }
- // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.selectedRow makeIfNecessary:NO];
- // self.addButton.enabled = cellView.detailtextField.stringValue.length > 0;
- //
- // for (NSInteger i = 0; i < self.tableView.numberOfRows; i++) {
- // NSTableCellView *cellView = [self.tableView viewAtColumn:0 row:i makeIfNecessary:NO];
- // if ([cellView isKindOfClass:[KMProfileCellView class]]) {
- // KMProfileCellView *cellView1 = (KMProfileCellView *)cellView;
- // if (i == self.tableView.selectedRow) {
- // cellView1.detailtextField.textColor = [NSColor whiteColor];
- // [cellView1.addButton setTitleColor:[NSColor whiteColor]];
- // } else {
- // cellView1.detailtextField.textColor = [NSColor secondaryLabelColor];
- // [cellView1.addButton setTitleColor:[NSColor systemBlueColor]];
- // }
- // }
- // }
- NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
- if (self.tableView.selectedRow > customIndex) {
- self.removeNewInfoButton.enabled = YES;
- } else {
- self.removeNewInfoButton.enabled = NO;
- }
- }
- #pragma mark - Button Action
- - (IBAction)buttonClicked_back:(id)sender {
- // [self dismissSheet:sender];
- }
- - (IBAction)buttonClicked_add:(id)sender {
- NSInteger selectRow =self.tableView.selectedRow;
- if (selectRow >= 0) {
- if (self.callBack) {
- // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.selectedRow makeIfNecessary:NO];
- // self.callBack(cellView.detailtextField.stringValue);
- // [self dismissSheet:sender];
- }
- }
- }
- - (IBAction)buttonClicked_addNewInfo:(id)sender {
- NSDictionary *dict = @{@"":@""};
- [[KMProfileInfo sharedInstance] insertObject:dict inCustomInfoArrayAtIndex:[KMProfileInfo sharedInstance].customInfoArray.count];
- [self fetchDisplayKeys];
- [self.tableView reloadData];
- if (self.tableView.numberOfRows > 1) {
- [self.tableView scrollRowToVisible:self.tableView.numberOfRows - 1];
- NSIndexSet *set = [NSIndexSet indexSetWithIndex:self.tableView.numberOfRows - 1];
- [self.tableView selectRowIndexes:set byExtendingSelection:NO];
- // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.selectedRow makeIfNecessary:NO];
- // cellView.addButton.hidden = YES;
- // [cellView.textField becomeFirstResponder];
- };
- }
- - (IBAction)buttonClicked_RemoveNewInfo:(id)sender
- {
- NSInteger editIndex = self.tableView.selectedRow;
- NSUInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
- if (editIndex - customIndex - 1 < [KMProfileInfo sharedInstance].customInfoArray.count) {
- [[KMProfileInfo sharedInstance] removeObjectFromCustomInfoArrayAtIndex:editIndex - customIndex - 1];
- [self fetchDisplayKeys];
- [self.tableView reloadData];
- self.removeNewInfoButton.enabled = NO;
- }
- }
- - (IBAction)addFileName_Add:(NSButton *)sender
- {
- [self.window makeFirstResponder:nil];
- NSInteger tag = sender.tag;
- // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:tag makeIfNecessary:NO];
- // if ([cellView isKindOfClass:[KMProfileCellView class]]) {
- // cellView.addButton.hidden = YES;
- // cellView.detailtextField.hidden = NO;
- // [cellView.detailtextField becomeFirstResponder];
- // }
- }
- #pragma mark - NSMenu Action
- - (IBAction)menuItemClicked_Edit:(id)sender {
- // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.clickedRow makeIfNecessary:NO];
- // NSIndexSet *set = [NSIndexSet indexSetWithIndex:self.tableView.clickedRow];
- // [self.tableView selectRowIndexes:set byExtendingSelection:NO];
- // if (cellView.textField.editable) {
- // if (cellView.textField.stringValue.length == 0) {
- // [cellView.textField becomeFirstResponder];
- // }else {
- // [cellView.detailtextField becomeFirstResponder];
- // }
- // } else {
- // [cellView.detailtextField becomeFirstResponder];
- // }
- }
- - (IBAction)menuItemClicked_Add:(id)sender {
- // KMProfileCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.clickedRow makeIfNecessary:NO];
- // if (self.callBack) {
- // self.callBack(cellView.detailtextField.stringValue);
- // [self dismissSheet:sender];
- // }
- }
- - (IBAction)menuItemClicked_Delete:(id)sender {
- NSInteger editIndex = self.tableView.clickedRow;
- NSInteger customIndex = [self.tableContents indexOfObject:NSLocalizedString(@"Custom", nil)];
- if (editIndex < customIndex ) {
- switch (editIndex) {
- case 1:
- [KMProfileInfo sharedInstance].fullName = @"";
- break;
- case 2:
- [KMProfileInfo sharedInstance].firstName = @"";
- break;
- case 3:
- [KMProfileInfo sharedInstance].middleName = @"";
- break;
- case 4:
- [KMProfileInfo sharedInstance].lastName = @"";
- break;
-
- case 6:
- [KMProfileInfo sharedInstance].street1 = @"";
- break;
- case 7:
- [KMProfileInfo sharedInstance].street2 = @"";
- break;
- case 8:
- [KMProfileInfo sharedInstance].city = @"";
- break;
- case 9:
- [KMProfileInfo sharedInstance].state = @"";
- break;
- case 10:
- [KMProfileInfo sharedInstance].zip = @"";
- break;
- case 11:
- [KMProfileInfo sharedInstance].country = @"";
- break;
- case 13:
- [KMProfileInfo sharedInstance].email = @"";
- break;
- case 14:
- [KMProfileInfo sharedInstance].tel = @"";
- break;
- case 16:
- // [KMProfileInfo sharedInstance].date = @"";
- break;
- case 17:
- [KMProfileInfo sharedInstance].birthDate = @"";
- break;
- default:
- break;
- }
- } else {
- [[KMProfileInfo sharedInstance] removeObjectFromCustomInfoArrayAtIndex:editIndex - customIndex - 1];
- }
-
- [self fetchDisplayKeys];
- [self.tableView reloadData];
- }
- - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
-
- // NSTableCellView *cellView = [self.tableView viewAtColumn:0 row:self.tableView.clickedRow makeIfNecessary:NO];
- // if (menuItem.action == @selector(menuItemClicked_Edit:)) {
- // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) {
- // return NO;
- // } else {
- // KMProfileCellView *cellView1 = (KMProfileCellView *)cellView;
- // if ([cellView1.textField.stringValue isEqualToString:NSLocalizedString(@"Date", nil)]) {
- // return NO;
- // }
- // return YES;
- // }
- //
- // } else if (menuItem.action == @selector(menuItemClicked_Add:)) {
- // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) {
- // return NO;
- // } else {
- // KMProfileCellView *cell = (KMProfileCellView *)cellView;
- // return cell.detailtextField.stringValue.length > 0;
- // }
- // } else if (menuItem.action == @selector(menuItemClicked_Delete:)) {
- // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) {
- // return NO;
- // } else {
- // KMProfileCellView *cellView1 = (KMProfileCellView *)cellView;
- // if ([cellView1.textField.stringValue isEqualToString:NSLocalizedString(@"Date", nil)]) {
- // return NO;
- // }
- // return YES;
- // }
- // }
- return NO;
- }
- - (void)themeChanged:(NSNotification *)notification {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self updateViewColor];
- });
- }
- - (void)updateViewColor {
- [self.tableView reloadData];
- }
- @end
|