Permalink
Browse files

* Moved `-[SPHistoryController currentlySelectedView]` to SPDatabaseD…

…ocument (was completely out of place there)

 * Removed some redundant code in the process
* Remove a few cross-thread UI calls Xcode 9.2 complains about (but there are just so many...)
  • Loading branch information...
dmoagx committed Dec 2, 2017
1 parent b04ca67 commit 2f0099e1bf3caf42cfe76aa4074efdfc7f351cef
View
@@ -108,16 +108,18 @@ typedef enum
SPTableTypeEvent = 4
} SPTableType;
// History views
typedef enum
// Content views
typedef NS_ENUM(NSInteger, SPTableViewType)
{
SPTableViewStructure = 0,
SPTableViewContent = 1,
SPTableViewCustomQuery = 2,
SPTableViewStatus = 3,
SPTableViewRelations = 4,
SPTableViewTriggers = 5
} SPTableViewType;
SPTableViewTriggers = 5,
SPTableViewInvalid = NSNotFound
};
// SSH tunnel password modes
typedef enum
@@ -463,6 +463,7 @@
- (NSArray *)allTableNames;
- (SPTablesList *)tablesListInstance;
- (SPCreateDatabaseInfo *)createDatabaseInfo;
- (SPTableViewType) currentlySelectedView;
#ifndef SP_CODA /* method decls */
// Notification center methods
@@ -2691,14 +2691,12 @@ - (BOOL)couldCommitCurrentViewActions
{
[parentWindow endEditingFor:nil];
#ifndef SP_CODA
switch ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]]) {
switch ([self currentlySelectedView]) {
// Table structure view
case 0:
case SPTableViewStructure:
return [tableSourceInstance saveRowOnDeselect];
// Table content view
case 1:
case SPTableViewContent:
return [tableContentInstance saveRowOnDeselect];
default:
@@ -2884,6 +2882,35 @@ - (SPCreateDatabaseInfo *)createDatabaseInfo
return [dbInfo autorelease];
}
/**
* Retrieve the view that is currently selected from the database
*
* MUST BE CALLED ON THE UI THREAD!
*/
- (SPTableViewType)currentlySelectedView
{
SPTableViewType theView = NSNotFound;
// -selectedTabViewItem is a UI method according to Xcode 9.2!
NSString *viewName = [[tableTabView selectedTabViewItem] identifier];
if ([viewName isEqualToString:@"source"]) {
theView = SPTableViewStructure;
} else if ([viewName isEqualToString:@"content"]) {
theView = SPTableViewContent;
} else if ([viewName isEqualToString:@"customQuery"]) {
theView = SPTableViewCustomQuery;
} else if ([viewName isEqualToString:@"status"]) {
theView = SPTableViewStatus;
} else if ([viewName isEqualToString:@"relations"]) {
theView = SPTableViewRelations;
} else if ([viewName isEqualToString:@"triggers"]) {
theView = SPTableViewTriggers;
}
return theView;
}
#pragma mark -
#pragma mark Notification center methods
@@ -3736,7 +3763,7 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
// If Custom Query Tab is active the textView will handle printDocument by itself
// if it is first responder; otherwise allow to print the Query Result table even
// if no db/table is selected
[tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 2);
[self currentlySelectedView] == SPTableViewCustomQuery);
}
#endif
@@ -4687,7 +4714,7 @@ - (NSDictionary *) stateIncludingDetails:(NSDictionary *)detailsToReturn
[sessionState setObject:[self table] forKey:@"table"];
NSString *currentlySelectedViewName;
switch ([spHistoryControllerInstance currentlySelectedView]) {
switch ([self currentlySelectedView]) {
case SPTableViewStructure:
currentlySelectedViewName = @"SP_VIEW_STRUCTURE";
break;
@@ -5921,7 +5948,7 @@ - (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet usingRec
// Locate the sheet "Reset Auto Increment" just centered beneath the chosen index row
// if Structure Pane is active
if([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 0
if([self currentlySelectedView] == SPTableViewStructure
&& [[sheet title] isEqualToString:@"Reset Auto Increment"]) {
id it = [tableSourceInstance valueForKeyPath:@"indexesTableView"];
@@ -50,7 +50,7 @@
@interface SPDatabaseDocument (SPDatabaseViewControllerPrivateAPI)
- (void)_loadTabTask:(NSTabViewItem *)tabViewItem;
- (void)_loadTabTask:(NSNumber *)tabViewItemIndexNumber;
- (void)_loadTableTask;
@end
@@ -120,7 +120,7 @@ - (BOOL)statusLoaded
- (IBAction)viewStructure:(id)sender
{
// Cancel the selection if currently editing a view and unable to save
if (![self couldCommitCurrentViewActions]) {
if (![[self onMainThread] couldCommitCurrentViewActions]) {
[[mainToolbar onMainThread] setSelectedItemIdentifier:*SPViewModeToMainToolbarMap[[prefs integerForKey:SPLastViewMode]]];
return;
}
@@ -226,7 +226,7 @@ - (void)setStructureRequiresReload:(BOOL)reload
BOOL reloadRequired = reload;
#ifndef SP_CODA
if ([tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == SPTableViewStructure) {
if ([self currentlySelectedView] == SPTableViewStructure) {
reloadRequired = NO;
}
#endif
@@ -247,7 +247,7 @@ - (void)setContentRequiresReload:(BOOL)reload
{
if (reload && selectedTableName
#ifndef SP_CODA /* check which tab is selected */
&& [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == SPTableViewContent
&& [self currentlySelectedView] == SPTableViewContent
#endif
) {
[tableContentInstance loadTable:selectedTableName];
@@ -265,7 +265,7 @@ - (void)setStatusRequiresReload:(BOOL)reload
{
if (reload && selectedTableName
#ifndef SP_CODA /* check which tab is selected */
&& [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == SPTableViewStatus
&& [self currentlySelectedView] == SPTableViewStatus
#endif
) {
[[extendedTableInfoInstance onMainThread] loadTable:selectedTableName];
@@ -283,7 +283,7 @@ - (void)setRelationsRequiresReload:(BOOL)reload
{
if (reload && selectedTableName
#ifndef SP_CODA /* check which tab is selected */
&& [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == SPTableViewRelations
&& [self currentlySelectedView] == SPTableViewRelations
#endif
) {
[[tableRelationsInstance onMainThread] refreshRelations:self];
@@ -301,15 +301,19 @@ - (void)setRelationsRequiresReload:(BOOL)reload
- (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
[self startTaskWithDescription:[NSString stringWithFormat:NSLocalizedString(@"Loading %@...", @"Loading table task string"), [self table]]];
// We can't pass aTabView or tabViewItem UI objects to a bg thread, but since the change should already
// be done in *did*SelectTabViewItem we can just ask the tab view for the current selection index and use that
SPTableViewType newView = [self currentlySelectedView];
if ([NSThread isMainThread]) {
[NSThread detachNewThreadWithName:SPCtxt(@"SPDatabaseViewController view load task",self)
target:self
selector:@selector(_loadTabTask:)
object:tabViewItem];
object:@(newView)];
}
else {
[self _loadTabTask:tabViewItem];
[self _loadTabTask:@(newView)];
}
}
#endif
@@ -408,7 +412,7 @@ @implementation SPDatabaseDocument (SPDatabaseViewControllerPrivateAPI)
* In a threaded task, ensure that the supplied tab is loaded -
* usually as a result of switching to it.
*/
- (void)_loadTabTask:(NSTabViewItem *)tabViewItem
- (void)_loadTabTask:(NSNumber *)tabViewItemIndexNumber
{
NSAutoreleasePool *tabLoadPool = [[NSAutoreleasePool alloc] init];
@@ -421,7 +425,7 @@ - (void)_loadTabTask:(NSTabViewItem *)tabViewItem
}
// Get the tab view index and ensure the associated view is loaded
NSInteger selectedTabViewIndex = [[tabViewItem tabView] indexOfTabViewItem:tabViewItem];
SPTableViewType selectedTabViewIndex = [tabViewItemIndexNumber integerValue];
switch (selectedTabViewIndex) {
case SPTableViewStructure:
@@ -531,7 +535,7 @@ - (void)_loadTableTask
if (tableEncoding && (selectedTableType == SPTableTypeView || selectedTableType == SPTableTypeTable))
{
#ifndef SP_CODA /* load everything */
NSInteger selectedTabViewIndex = [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]];
NSInteger selectedTabViewIndex = [[self onMainThread] currentlySelectedView];
switch (selectedTabViewIndex) {
case SPTableViewStructure:
@@ -55,7 +55,6 @@
- (void)goBackInHistory;
- (void)goForwardInHistory;
- (IBAction) historyControlClicked:(NSSegmentedControl *)theControl;
- (NSUInteger) currentlySelectedView;
- (void) setupInterface;
- (void) startDocumentTask:(NSNotification *)aNotification;
- (void) endDocumentTask:(NSNotification *)aNotification;
@@ -169,33 +169,6 @@ - (IBAction) historyControlClicked:(NSSegmentedControl *)theControl
}
}
/**
* Retrieve the view that is currently selected from the database
*/
- (NSUInteger) currentlySelectedView
{
NSUInteger theView = NSNotFound;
NSString *viewName = [[[theDocument valueForKey:@"tableTabView"] selectedTabViewItem] identifier];
if ([viewName isEqualToString:@"source"]) {
theView = SPTableViewStructure;
} else if ([viewName isEqualToString:@"content"]) {
theView = SPTableViewContent;
} else if ([viewName isEqualToString:@"customQuery"]) {
theView = SPTableViewCustomQuery;
} else if ([viewName isEqualToString:@"status"]) {
theView = SPTableViewStatus;
} else if ([viewName isEqualToString:@"relations"]) {
theView = SPTableViewRelations;
}
else if ([viewName isEqualToString:@"triggers"]) {
theView = SPTableViewTriggers;
}
return theView;
}
/**
* Set up the toolbar items as appropriate.
* State tracking is necessary as manipulating items not on the toolbar
@@ -267,15 +240,16 @@ - (void) updateHistoryEntries
// Don't modify anything if we're in the process of restoring an old history state
if (modifyingState) return;
#warning Basically all of those next calls do stuff that must be done on the main thread (AND en block in order to be consistent). This needs to be refactored!
// Work out the current document details
NSString *theDatabase = [theDocument database];
NSString *theTable = [theDocument table];
NSUInteger theView = [self currentlySelectedView];
SPTableViewType theView = [[theDocument onMainThread] currentlySelectedView];
NSString *contentSortCol = [tableContentInstance sortColumnName];
BOOL contentSortColIsAsc = [tableContentInstance sortColumnIsAscending];
NSUInteger contentPageNumber = [tableContentInstance pageNumber];
NSDictionary *contentSelectedRows = [tableContentInstance selectionDetailsAllowingIndexSelection:YES];
NSRect contentViewport = [tableContentInstance viewport];
NSDictionary *contentSelectedRows = [[tableContentInstance onMainThread] selectionDetailsAllowingIndexSelection:YES];
NSRect contentViewport = [[tableContentInstance onMainThread] viewport];
NSDictionary *contentFilter = [[tableContentInstance onMainThread] filterSettings];
NSData *filterTableData = [tableContentInstance filterTableData];
if (!theDatabase) return;
@@ -411,8 +385,8 @@ - (void) loadEntryTaskWithPosition:(NSNumber *)positionNumber
// If the database, table, and view are the same and content - just trigger a table reload (filters)
if ([[theDocument database] isEqualToString:[historyEntry objectForKey:@"database"]]
&& [historyEntry objectForKey:@"table"] && [[theDocument table] isEqualToString:[historyEntry objectForKey:@"table"]]
&& [[historyEntry objectForKey:@"view"] unsignedIntegerValue] == [self currentlySelectedView]
&& [[historyEntry objectForKey:@"view"] unsignedIntegerValue] == SPTableViewContent)
&& [[historyEntry objectForKey:@"view"] integerValue] == [theDocument currentlySelectedView]
&& [[historyEntry objectForKey:@"view"] integerValue] == SPTableViewContent)
{
[tableContentInstance loadTable:[historyEntry objectForKey:@"table"]];
modifyingState = NO;
@@ -441,7 +415,7 @@ - (void) loadEntryTaskWithPosition:(NSNumber *)positionNumber
}
// Check and set the view
if ([self currentlySelectedView] != [[historyEntry objectForKey:@"view"] unsignedIntegerValue]) {
if ([theDocument currentlySelectedView] != [[historyEntry objectForKey:@"view"] integerValue]) {
switch ([[historyEntry objectForKey:@"view"] integerValue]) {
case SPTableViewStructure:
[theDocument viewStructure:self];
@@ -462,7 +436,7 @@ - (void) loadEntryTaskWithPosition:(NSNumber *)positionNumber
[theDocument viewTriggers:self];
break;
}
if ([self currentlySelectedView] != [[historyEntry objectForKey:@"view"] unsignedIntegerValue]) {
if ([theDocument currentlySelectedView] != [[historyEntry objectForKey:@"view"] integerValue]) {
return [self abortEntryLoadWithPool:loadPool];
}
}
Oops, something went wrong.

0 comments on commit 2f0099e

Please sign in to comment.