Selaa lähdekoodia

Added StateModel. Also added persistant storage.

Thomas Buck 11 vuotta sitten
vanhempi
commit
6dcea84f39

+ 8
- 0
RedditBar.xcodeproj/project.pbxproj Näytä tiedosto

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

+ 3
- 1
RedditBar/AppDelegate.h Näytä tiedosto

@@ -8,6 +8,7 @@
8 8
 
9 9
 #import <Cocoa/Cocoa.h>
10 10
 #import "PrefController.h"
11
+#import "StateModel.h"
11 12
 
12 13
 @interface AppDelegate : NSObject <NSApplicationDelegate>
13 14
 
@@ -17,9 +18,10 @@
17 18
 @property (atomic, retain) NSImage *statusImage;
18 19
 @property (atomic, retain) NSImage *statusHighlightImage;
19 20
 @property (atomic, retain) PrefController *prefWindow;
21
+@property (atomic, retain) StateModel *currentState;
20 22
 
21 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 27
 @end

+ 41
- 6
RedditBar/AppDelegate.m Näytä tiedosto

@@ -10,7 +10,33 @@
10 10
 
11 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 41
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
16 42
     statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
@@ -23,8 +49,11 @@
23 49
     [statusItem setToolTip:@"Reddit Bar"];
24 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 57
     // TODO reload menu list
29 58
 }
30 59
 
@@ -32,12 +61,18 @@
32 61
     [NSApp activateIgnoringOtherApps:YES];
33 62
     prefWindow = [[PrefController alloc] initWithWindowNibName:@"Prefs"];
34 63
     [prefWindow setParent:self];
64
+    [prefWindow setState:currentState];
35 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 76
     // TODO reload menu list
42 77
 }
43 78
 

+ 2
- 2
RedditBar/PrefController.h Näytä tiedosto

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

+ 37
- 19
RedditBar/PrefController.m Näytä tiedosto

@@ -11,47 +11,65 @@
11 11
 
12 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 42
 -(IBAction)buttonSave:(id)sender {
23 43
     Boolean subs;
24
-    NSString *reddits;
25 44
     if (subscriptions.state != 0) {
26 45
         subs = TRUE;
27 46
     } else {
28 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 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 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 64
     [win performClose:self];
37 65
 }
38 66
 
39 67
 -(IBAction)toggleSubs:(id)sender {
40 68
     if (subscriptions.state != 0) {
41
-        // Use subscriptions
42 69
         [subreddits setEditable:FALSE];
43
-        [subreddits setString:@""];
44 70
     } else {
45
-        // Use userlist
46 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 75
 @end

+ 18
- 0
RedditBar/StateModel.h Näytä tiedosto

@@ -0,0 +1,18 @@
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 Näytä tiedosto

@@ -0,0 +1,15 @@
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…
Peruuta
Tallenna