Browse Source

Added StateModel. Also added persistant storage.

Thomas Buck 10 years ago
parent
commit
6dcea84f39

+ 8
- 0
RedditBar.xcodeproj/project.pbxproj View File

7
 	objects = {
7
 	objects = {
8
 
8
 
9
 /* Begin PBXBuildFile section */
9
 /* Begin PBXBuildFile section */
10
+		E9341914184A1A1A00D03488 /* StateModel.m in Sources */ = {isa = PBXBuildFile; fileRef = E9341913184A1A1A00D03488 /* StateModel.m */; };
10
 		E9CF1F21184961010004AE02 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E9CF1F20184961010004AE02 /* Cocoa.framework */; };
11
 		E9CF1F21184961010004AE02 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E9CF1F20184961010004AE02 /* Cocoa.framework */; };
11
 		E9CF1F2B184961010004AE02 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9CF1F29184961010004AE02 /* InfoPlist.strings */; };
12
 		E9CF1F2B184961010004AE02 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = E9CF1F29184961010004AE02 /* InfoPlist.strings */; };
12
 		E9CF1F2D184961010004AE02 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = E9CF1F2C184961010004AE02 /* main.m */; };
13
 		E9CF1F2D184961010004AE02 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = E9CF1F2C184961010004AE02 /* main.m */; };
35
 /* End PBXContainerItemProxy section */
36
 /* End PBXContainerItemProxy section */
36
 
37
 
37
 /* Begin PBXFileReference section */
38
 /* Begin PBXFileReference section */
39
+		E9341912184A1A1A00D03488 /* StateModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StateModel.h; sourceTree = "<group>"; };
40
+		E9341913184A1A1A00D03488 /* StateModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StateModel.m; sourceTree = "<group>"; };
38
 		E9CF1F1D184961010004AE02 /* RedditBar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RedditBar.app; sourceTree = BUILT_PRODUCTS_DIR; };
41
 		E9CF1F1D184961010004AE02 /* RedditBar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RedditBar.app; sourceTree = BUILT_PRODUCTS_DIR; };
39
 		E9CF1F20184961010004AE02 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
42
 		E9CF1F20184961010004AE02 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
40
 		E9CF1F23184961010004AE02 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
43
 		E9CF1F23184961010004AE02 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
128
 				E9CF1F551849654F0004AE02 /* icon.png */,
131
 				E9CF1F551849654F0004AE02 /* icon.png */,
129
 				E9CF1F32184961010004AE02 /* AppDelegate.h */,
132
 				E9CF1F32184961010004AE02 /* AppDelegate.h */,
130
 				E9CF1F33184961010004AE02 /* AppDelegate.m */,
133
 				E9CF1F33184961010004AE02 /* AppDelegate.m */,
134
+				E9341912184A1A1A00D03488 /* StateModel.h */,
135
+				E9341913184A1A1A00D03488 /* StateModel.m */,
131
 				E9CF1F35184961020004AE02 /* MainMenu.xib */,
136
 				E9CF1F35184961020004AE02 /* MainMenu.xib */,
132
 				E9CF1F5A1849685C0004AE02 /* PrefController.h */,
137
 				E9CF1F5A1849685C0004AE02 /* PrefController.h */,
133
 				E9CF1F5B1849685C0004AE02 /* PrefController.m */,
138
 				E9CF1F5B1849685C0004AE02 /* PrefController.m */,
270
 			buildActionMask = 2147483647;
275
 			buildActionMask = 2147483647;
271
 			files = (
276
 			files = (
272
 				E9CF1F34184961010004AE02 /* AppDelegate.m in Sources */,
277
 				E9CF1F34184961010004AE02 /* AppDelegate.m in Sources */,
278
+				E9341914184A1A1A00D03488 /* StateModel.m in Sources */,
273
 				E9CF1F2D184961010004AE02 /* main.m in Sources */,
279
 				E9CF1F2D184961010004AE02 /* main.m in Sources */,
274
 				E9CF1F5C1849685C0004AE02 /* PrefController.m in Sources */,
280
 				E9CF1F5C1849685C0004AE02 /* PrefController.m in Sources */,
275
 			);
281
 			);
482
 				E9CF1F50184961020004AE02 /* Release */,
488
 				E9CF1F50184961020004AE02 /* Release */,
483
 			);
489
 			);
484
 			defaultConfigurationIsVisible = 0;
490
 			defaultConfigurationIsVisible = 0;
491
+			defaultConfigurationName = Release;
485
 		};
492
 		};
486
 		E9CF1F51184961020004AE02 /* Build configuration list for PBXNativeTarget "RedditBarTests" */ = {
493
 		E9CF1F51184961020004AE02 /* Build configuration list for PBXNativeTarget "RedditBarTests" */ = {
487
 			isa = XCConfigurationList;
494
 			isa = XCConfigurationList;
490
 				E9CF1F53184961020004AE02 /* Release */,
497
 				E9CF1F53184961020004AE02 /* Release */,
491
 			);
498
 			);
492
 			defaultConfigurationIsVisible = 0;
499
 			defaultConfigurationIsVisible = 0;
500
+			defaultConfigurationName = Release;
493
 		};
501
 		};
494
 /* End XCConfigurationList section */
502
 /* End XCConfigurationList section */
495
 	};
503
 	};

+ 3
- 1
RedditBar/AppDelegate.h View File

8
 
8
 
9
 #import <Cocoa/Cocoa.h>
9
 #import <Cocoa/Cocoa.h>
10
 #import "PrefController.h"
10
 #import "PrefController.h"
11
+#import "StateModel.h"
11
 
12
 
12
 @interface AppDelegate : NSObject <NSApplicationDelegate>
13
 @interface AppDelegate : NSObject <NSApplicationDelegate>
13
 
14
 
17
 @property (atomic, retain) NSImage *statusImage;
18
 @property (atomic, retain) NSImage *statusImage;
18
 @property (atomic, retain) NSImage *statusHighlightImage;
19
 @property (atomic, retain) NSImage *statusHighlightImage;
19
 @property (atomic, retain) PrefController *prefWindow;
20
 @property (atomic, retain) PrefController *prefWindow;
21
+@property (atomic, retain) StateModel *currentState;
20
 
22
 
21
 -(IBAction)showPreferences:(id)sender;
23
 -(IBAction)showPreferences:(id)sender;
22
 
24
 
23
--(void)prefReturnName:(NSString *)name Pass:(NSString *)pass subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits;
25
+-(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits;
24
 
26
 
25
 @end
27
 @end

+ 41
- 6
RedditBar/AppDelegate.m View File

10
 
10
 
11
 @implementation AppDelegate
11
 @implementation AppDelegate
12
 
12
 
13
-@synthesize statusMenu, statusItem, statusImage, statusHighlightImage, prefWindow;
13
+@synthesize statusMenu, statusItem, statusImage, statusHighlightImage, prefWindow, currentState;
14
+
15
+-(void)defaultPreferences {
16
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
17
+    NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"" forKey:@"username"];
18
+    [appDefaults setValue:@"" forKey:@"modhash"];
19
+    [appDefaults setValue:[NSNumber numberWithBool:YES] forKey:@"subscriptions"];
20
+    [store registerDefaults:appDefaults];
21
+}
22
+
23
+-(void)savePreferences {
24
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
25
+    [store setObject:currentState.username forKey:@"username"];
26
+    [store setObject:currentState.modhash forKey:@"modhash"];
27
+    [store setBool:currentState.useSubsciptions forKey:@"subscriptions"];
28
+    [store setObject:currentState.subreddits forKey:@"subreddits"];
29
+    [store synchronize];
30
+}
31
+
32
+-(void)loadPreferences {
33
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
34
+    [store synchronize];
35
+    [currentState setUsername:[store stringForKey:@"username"]];
36
+    [currentState setModhash:[store stringForKey:@"modhash"]];
37
+    [currentState setUseSubsciptions:[store boolForKey:@"subscriptions"]];
38
+    [currentState setSubreddits:[store arrayForKey:@"subreddits"]];
39
+}
14
 
40
 
15
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
41
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
16
     statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
42
     statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
23
     [statusItem setToolTip:@"Reddit Bar"];
49
     [statusItem setToolTip:@"Reddit Bar"];
24
     [statusItem setHighlightMode:YES];
50
     [statusItem setHighlightMode:YES];
25
     
51
     
26
-    // TODO load preferences
27
-    // TODO apply config
52
+    [self defaultPreferences];
53
+    currentState = [[StateModel alloc] init];
54
+    [self loadPreferences]; // Fill currentState
55
+    
56
+    // TODO apply currentState
28
     // TODO reload menu list
57
     // TODO reload menu list
29
 }
58
 }
30
 
59
 
32
     [NSApp activateIgnoringOtherApps:YES];
61
     [NSApp activateIgnoringOtherApps:YES];
33
     prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
62
     prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
34
     [prefWindow setParent:self];
63
     [prefWindow setParent:self];
64
+    [prefWindow setState:currentState];
35
     [prefWindow showWindow:self];
65
     [prefWindow showWindow:self];
36
 }
66
 }
37
 
67
 
38
--(void)prefReturnName:(NSString *)name Pass:(NSString *)pass subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits {
39
-    // TODO store preferences
40
-    // TODO apply config
68
+-(void)prefReturnName:(NSString *)name Modhash:(NSString *)modhash subscriptions:(Boolean)subscriptions subreddits:(NSString *)subreddits {
69
+    currentState.username = name;
70
+    currentState.modhash = modhash;
71
+    currentState.useSubsciptions = subscriptions;
72
+    currentState.subreddits = [subreddits componentsSeparatedByString: @"\n"];
73
+    [self savePreferences]; // write currentState
74
+    
75
+    // TODO apply currentState
41
     // TODO reload menu list
76
     // TODO reload menu list
42
 }
77
 }
43
 
78
 

+ 2
- 2
RedditBar/PrefController.h View File

7
 //
7
 //
8
 
8
 
9
 #import <Foundation/Foundation.h>
9
 #import <Foundation/Foundation.h>
10
+#import "StateModel.h"
10
 
11
 
11
 @interface PrefController : NSWindowController
12
 @interface PrefController : NSWindowController
12
 
13
 
16
 @property (atomic, retain) IBOutlet NSTextView *subreddits;
17
 @property (atomic, retain) IBOutlet NSTextView *subreddits;
17
 @property (atomic, retain) IBOutlet NSWindow *win;
18
 @property (atomic, retain) IBOutlet NSWindow *win;
18
 @property (atomic, retain) NSObject *parent;
19
 @property (atomic, retain) NSObject *parent;
20
+@property (atomic, retain) StateModel *state;
19
 
21
 
20
 -(IBAction)buttonSave:(id)sender;
22
 -(IBAction)buttonSave:(id)sender;
21
 -(IBAction)toggleSubs:(id)sender;
23
 -(IBAction)toggleSubs:(id)sender;
22
 
24
 
23
--(void)setParent:(NSObject *)par;
24
-
25
 @end
25
 @end

+ 37
- 19
RedditBar/PrefController.m View File

11
 
11
 
12
 @implementation PrefController
12
 @implementation PrefController
13
 
13
 
14
-@synthesize username, password, subscriptions, subreddits, win, parent;
14
+@synthesize username, password, subscriptions, subreddits, win, parent, state;
15
 
15
 
16
--(Boolean)isValidList:(NSString *)input validated:(NSString **)output {
17
-    // TODO: Check if subreddit input is valid
18
-    *output = input;
19
-    return TRUE;
16
+-(Boolean)isValidList:(NSString *)input {
17
+    NSCharacterSet *invalidChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_\n"] invertedSet];
18
+    if ([input rangeOfCharacterFromSet:invalidChars].location != NSNotFound) {
19
+        return FALSE;
20
+    } else {
21
+        return TRUE;
22
+    }
23
+}
24
+
25
+-(void)showWindow:(id)sender {
26
+    [super showWindow:sender];
27
+    
28
+    [username setStringValue:state.username];
29
+    
30
+    // TODO what to do with modhash and password field??
31
+    
32
+    [subscriptions setState:[NSNumber numberWithBool:state.useSubsciptions].integerValue];
33
+    [self toggleSubs:nil]; // Maybe the subreddits field needs to be editable
34
+    
35
+    NSMutableString *reddits = [[NSMutableString alloc] init];
36
+    for(int i = 0; i < [state.subreddits count]; i++) {
37
+        [reddits appendFormat:@"%@\n", [state.subreddits objectAtIndex:i]];
38
+    }
39
+    [subreddits setString:reddits];
20
 }
40
 }
21
 
41
 
22
 -(IBAction)buttonSave:(id)sender {
42
 -(IBAction)buttonSave:(id)sender {
23
     Boolean subs;
43
     Boolean subs;
24
-    NSString *reddits;
25
     if (subscriptions.state != 0) {
44
     if (subscriptions.state != 0) {
26
         subs = TRUE;
45
         subs = TRUE;
27
     } else {
46
     } else {
28
         subs = FALSE;
47
         subs = FALSE;
29
-        if (![self isValidList:subreddits.textStorage.string validated:&reddits]) {
30
-            // TODO show error message
48
+        if (![self isValidList:subreddits.textStorage.string]) {
49
+            NSAlert *alert = [[NSAlert alloc] init];
50
+            [alert addButtonWithTitle:@"OK"];
51
+            [alert setMessageText:@"Preferences Error"];
52
+            [alert setInformativeText:@"Subreddit List Invalid!"];
53
+            [alert setAlertStyle:NSCriticalAlertStyle];
54
+            [alert beginSheetModalForWindow:win modalDelegate:nil didEndSelector:nil contextInfo:nil];
31
             return;
55
             return;
32
         }
56
         }
33
     }
57
     }
58
+    
59
+    // TODO if username / password changed, get modhash! Else, use the one we got from init
60
+    NSString *modhash;
61
+    
34
     AppDelegate *app = (AppDelegate *)parent;
62
     AppDelegate *app = (AppDelegate *)parent;
35
-    [app prefReturnName:username.stringValue Pass:password.stringValue subscriptions:subs subreddits:reddits];
63
+    [app prefReturnName:username.stringValue Modhash:modhash subscriptions:subs subreddits:subreddits.textStorage.string];
36
     [win performClose:self];
64
     [win performClose:self];
37
 }
65
 }
38
 
66
 
39
 -(IBAction)toggleSubs:(id)sender {
67
 -(IBAction)toggleSubs:(id)sender {
40
     if (subscriptions.state != 0) {
68
     if (subscriptions.state != 0) {
41
-        // Use subscriptions
42
         [subreddits setEditable:FALSE];
69
         [subreddits setEditable:FALSE];
43
-        [subreddits setString:@""];
44
     } else {
70
     } else {
45
-        // Use userlist
46
         [subreddits setEditable:TRUE];
71
         [subreddits setEditable:TRUE];
47
-        [subreddits setString:@"One Subreddit per line!"];
48
     }
72
     }
49
 }
73
 }
50
 
74
 
51
--(void)showWindow:(id)sender {
52
-    [super showWindow:sender];
53
-    
54
-    
55
-}
56
-
57
 @end
75
 @end

+ 18
- 0
RedditBar/StateModel.h View File

1
+//
2
+//  StateModel.h
3
+//  RedditBar
4
+//
5
+//  Created by Thomas Buck on 30.11.13.
6
+//  Copyright (c) 2013 xythobuz. All rights reserved.
7
+//
8
+
9
+#import <Foundation/Foundation.h>
10
+
11
+@interface StateModel : NSObject
12
+
13
+@property (atomic, retain) NSString *username;
14
+@property (atomic, retain) NSString *modhash;
15
+@property (atomic) Boolean useSubsciptions;
16
+@property (atomic, retain) NSArray *subreddits;
17
+
18
+@end

+ 15
- 0
RedditBar/StateModel.m View File

1
+//
2
+//  StateModel.m
3
+//  RedditBar
4
+//
5
+//  Created by Thomas Buck on 30.11.13.
6
+//  Copyright (c) 2013 xythobuz. All rights reserved.
7
+//
8
+
9
+#import "StateModel.h"
10
+
11
+@implementation StateModel
12
+
13
+@synthesize username, modhash, useSubsciptions, subreddits;
14
+
15
+@end

Loading…
Cancel
Save