Commits

Ivan Andrus  committed 2322a2d

Switched to tri-valued button -- evaluation not implemented

  • Participants
  • Parent commits 7fd2eae

Comments (0)

Files changed (3)

File Classes/EditViewController.h

     id delegate;
     NSManagedObject *selectedObject;
     UITextView *activeField;
+    UIBarButtonItem *saveButton;
+    UIBarButtonItem *evalButton;
+    UIBarButtonItem *newButton;
 }
 
 @property (nonatomic, retain) NSManagedObject *selectedObject;
 - (IBAction)insertDelimiters:(id)sender;
 - (IBAction)insertTab:(id)sender;
 - (IBAction)insertCharacter:(id)sender;
-- (IBAction)editingDone:(id)sender;
 - (IBAction)hideKeyboard:(id)sender;
 
+- (void)save:(id)sender;
+- (void)eval:(id)sender;
+- (IBAction)newCell:(id)sender;
+
+- (void)readyToEval;
+
 
 @end

File Classes/EditViewController.m

     [self registerForKeyboardNotifications];
 
     self.navigationItem.title = @"Editing";
-    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
-                                   initWithBarButtonSystemItem:UIBarButtonSystemItemDone
-                                   target:self
-                                   action:@selector(editingDone:)];
-    self.navigationItem.rightBarButtonItem = doneButton;
 
-    [doneButton release];
-}
-
-- (void)editingDone:(id)sender {
-    if ( ![[selectedObject valueForKey:@"input"] isEqual:[inputView text]] ) {
-        [selectedObject setValue:[inputView text] forKey:@"input"];
-        [selectedObject setValue:[NSNumber numberWithInt:AlephCellStatusHasInput] forKey:@"status"];
-    }
-
-    // NSLog(@"editing done status: %@",[selectedObject valueForKey:@"status"]);
-    [delegate editViewControllerDidFinish:self];
-    [self.navigationController popViewControllerAnimated:YES];
-}
-
-- (void)hideKeyboard:(id)sender {
-    [inputView resignFirstResponder];
+    saveButton = [[UIBarButtonItem alloc]
+                  initWithTitle:@"Save"
+                  style:UIBarButtonItemStylePlain
+                  target:self
+                  action:@selector(save:)];
+    evalButton = [[UIBarButtonItem alloc]
+                  initWithTitle:@"Evaluate"
+                  style:UIBarButtonItemStyleDone
+                  target:self
+                  action:@selector(eval:)];
+    newButton = [[UIBarButtonItem alloc]
+                 initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
+                 target:self
+                 action:@selector(newCell:)];
 }
 
 - (void)viewDidAppear:(BOOL)animated {
         outputView.text = @"";
     }
 
+    switch ( [[selectedObject valueForKey:@"status"] intValue] ) {
+        case AlephCellStatusHasOutput:
+        case AlephCellStatusSyntaxError:
+            self.navigationItem.rightBarButtonItem = newButton;
+            break;
+
+        case AlephCellStatusHasInput:
+        case AlephCellStatusHasAlephID:
+        case AlephCellStatusUninitialized:
+        case AlephCellStatusJSONParseError:
+        case AlephCellStatusNetworkError:
+        default:
+            [self readyToEval];
+            break;
+    }
+
     if ( [selectedObject valueForKey:@"image"] ) {
         imageView.image = [UIImage imageWithData:[selectedObject valueForKey:@"image"]];
         imageView.hidden = NO;
     [self updateImageSize];
 }
 
+#pragma mark -
+#pragma mark Button methods
+
+- (void)newCell:(id)sender {
+    if ( ![[selectedObject valueForKey:@"input"] isEqual:[inputView text]] ) {
+        [selectedObject setValue:[inputView text] forKey:@"input"];
+        [selectedObject setValue:[NSNumber numberWithInt:AlephCellStatusHasInput] forKey:@"status"];
+    }
+
+    [self.navigationController popViewControllerAnimated:NO];
+    [delegate editViewControllerDidFinish:self];
+}
+
+- (void)save:(id)sender {
+    [selectedObject setValue:[inputView text] forKey:@"input"];
+    [selectedObject setValue:[NSNumber numberWithInt:AlephCellStatusHasInput] forKey:@"status"];
+    [inputView resignFirstResponder]; // Hide keyboard
+    [self readyToEval];
+}
+
+- (void)readyToEval {
+    // Mark as stale
+    outputView.textColor = [UIColor colorWithRed:0.75 green:0.25 blue:0.25 alpha:1.0];
+    self.navigationItem.rightBarButtonItem = evalButton;
+}
+
+- (void)eval:(id)sender {
+    // Mark as stale
+    outputView.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
+    self.navigationItem.rightBarButtonItem = newButton;
+    [inputView resignFirstResponder];
+}
+
+
+- (void)hideKeyboard:(id)sender {
+    [inputView resignFirstResponder];
+}
+
 
 #pragma mark -
 #pragma mark Text view delegate methods
     activeField = nil;
 }
 
+- (void)textViewDidChange:(UITextView *)textView {
+    self.navigationItem.rightBarButtonItem = saveButton;
+}
+
 #pragma mark -
 #pragma mark Accessory View
 
 
 - (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
+    self.navigationItem.rightBarButtonItem = nil;
+    [saveButton dealloc];
+    [newButton dealloc];
+    [evalButton dealloc];
     [super dealloc];
 }
 
+#pragma mark -
+#pragma mark View rotation
 
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     return YES;

File Classes/RootViewController.m

 
 
 - (void)editViewControllerDidFinish:(EditViewController *)controller {
-    //NSLog(@"editViewControllerDidFinish");
-    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
-    NSError *error = nil;
-    if (![context save:&error]) {
-        /*
-         Replace this implementation with code to handle the error appropriately.
-
-         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
-         */
-        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
-        abort();
-    }
-
-    // TODO: this is a hack.  What I really want to do is reload the correct one, but since it's done automatically except for when creating a new one, this works okay.
-    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]
-                          withRowAnimation:UITableViewRowAnimationRight];
-
-    // TODO: we may have to delete the first item if it's uninitialized.
-    // Alternately we could not add it to the list until we get back from editing successfully, but that seems harder (though less hacky).
-
+    // The only time this is called is when we want to add a new one
+    [self insertNewObject];
 }
 
 #pragma mark -