forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectacleHistory.m
89 lines (64 loc) · 2.25 KB
/
SpectacleHistory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#import "SpectacleHistory.h"
#import "SpectacleHistoryItem.h"
#import "SpectacleConstants.h"
@interface SpectacleHistory ()
@property (nonatomic) SpectacleHistoryItem *firstHistoryItem;
@property (nonatomic) SpectacleHistoryItem *lastHistoryItem;
@property (nonatomic) SpectacleHistoryItem *currentHistoryItem;
@property (nonatomic) NSInteger size;
@end
#pragma mark -
@implementation SpectacleHistory
- (id)init {
if ((self = [super init])) {
_firstHistoryItem = nil;
_lastHistoryItem = _firstHistoryItem;
_currentHistoryItem = _firstHistoryItem;
_size = 0;
}
return self;
}
#pragma mark -
- (void)addHistoryItem: (SpectacleHistoryItem *)historyItem {
if ([self isEmpty]) {
_currentHistoryItem = historyItem;
_firstHistoryItem = _currentHistoryItem;
_lastHistoryItem = _firstHistoryItem;
} else {
historyItem.nextHistoryItem = _currentHistoryItem.nextHistoryItem;
historyItem.previousHistoryItem = _currentHistoryItem;
_currentHistoryItem.nextHistoryItem.previousHistoryItem = historyItem;
_currentHistoryItem.nextHistoryItem = historyItem;
if (![historyItem nextHistoryItem]) {
_firstHistoryItem = historyItem;
}
if (![historyItem previousHistoryItem]) {
_lastHistoryItem = historyItem;
}
_currentHistoryItem = historyItem;
}
if (++_size >= SpectacleWindowActionHistorySize) {
_lastHistoryItem.nextHistoryItem.previousHistoryItem = _lastHistoryItem.previousHistoryItem;
_lastHistoryItem = _lastHistoryItem.nextHistoryItem;
_size--;
}
}
#pragma mark -
- (SpectacleHistoryItem *)nextHistoryItem {
return [self moveCurrentHistoryItemToHistoryItem: _currentHistoryItem.nextHistoryItem];
}
- (SpectacleHistoryItem *)previousHistoryItem {
return [self moveCurrentHistoryItemToHistoryItem: _currentHistoryItem.previousHistoryItem];
}
#pragma mark -
- (BOOL)isEmpty {
return _size == 0;
}
#pragma mark -
- (SpectacleHistoryItem *)moveCurrentHistoryItemToHistoryItem: (SpectacleHistoryItem *)historyItem {
if (historyItem) {
_currentHistoryItem = historyItem;
}
return historyItem;
}
@end