Skip to content

Commit af4be8b

Browse files
committed
Add Core Data classes, global var and functions to save and restore IP addresses history in IP address field
1 parent ad879ce commit af4be8b

File tree

6 files changed

+145
-3
lines changed

6 files changed

+145
-3
lines changed

AddrHistory+CoreDataClass.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// AddrHistory+CoreDataClass.swift
3+
// SubnetCalc
4+
//
5+
// Created by Julien Mulot on 11/03/2022.
6+
//
7+
//
8+
9+
import Foundation
10+
import CoreData
11+
12+
13+
public class AddrHistory: NSManagedObject {
14+
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// AddrHistory+CoreDataProperties.swift
3+
// SubnetCalc
4+
//
5+
// Created by Julien Mulot on 11/03/2022.
6+
//
7+
//
8+
9+
import Foundation
10+
import CoreData
11+
12+
13+
extension AddrHistory {
14+
15+
@nonobjc public class func fetchRequest() -> NSFetchRequest<AddrHistory> {
16+
return NSFetchRequest<AddrHistory>(entityName: "AddrHistory")
17+
}
18+
19+
@NSManaged public var address: String
20+
21+
}

Base.lproj/MainMenu.xib

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@
142142
<action selector="darkMode:" target="b7w-M5-ti7" id="Nhc-rM-HhP"/>
143143
</connections>
144144
</menuItem>
145+
<menuItem title="Clear history" id="s1d-Yz-qSq">
146+
<modifierMask key="keyEquivalentModifierMask"/>
147+
<connections>
148+
<action selector="clearHistory:" target="b7w-M5-ti7" id="H57-v0-WWN"/>
149+
</connections>
150+
</menuItem>
145151
</items>
146152
</menu>
147153
</menuItem>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19574" systemVersion="21D62" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
3+
<entity name="AddrHistory" representedClassName=".AddrHistory" syncable="YES">
4+
<attribute name="address" attributeType="String"/>
5+
</entity>
6+
<elements>
7+
<element name="AddrHistory" positionX="-45" positionY="0" width="128" height="44"/>
8+
</elements>
9+
</model>

SubnetCalc.xcodeproj/project.pbxproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
94AC2613256AF7C900811156 /* SubnetCalcAppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94AC2612256AF7C900811156 /* SubnetCalcAppDelegate.swift */; };
1717
94B1C507256ABC1B00F992A3 /* IPSubnetcalc.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B1C506256ABC1B00F992A3 /* IPSubnetcalc.swift */; };
1818
94B6031212F74DC7005AA5EB /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 94B6031112F74DC7005AA5EB /* CoreFoundation.framework */; };
19+
94DBA15627DBD0BF00F61918 /* SubnetCalc.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 94DBA15427DBD0BF00F61918 /* SubnetCalc.xcdatamodeld */; };
20+
94DBA15E27DBDAC000F61918 /* AddrHistory+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94DBA15C27DBDAC000F61918 /* AddrHistory+CoreDataClass.swift */; };
21+
94DBA15F27DBDAC000F61918 /* AddrHistory+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94DBA15D27DBDAC000F61918 /* AddrHistory+CoreDataProperties.swift */; };
1922
/* End PBXBuildFile section */
2023

2124
/* Begin PBXContainerItemProxy section */
@@ -47,6 +50,9 @@
4750
94B6031112F74DC7005AA5EB /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
4851
94CDB5F6255EE11D0010F1E4 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
4952
94D8C5731520F99500B27A3A /* SubnetCalc.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = SubnetCalc.entitlements; sourceTree = "<group>"; };
53+
94DBA15527DBD0BF00F61918 /* SubnetCalc.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = SubnetCalc.xcdatamodel; sourceTree = "<group>"; };
54+
94DBA15C27DBDAC000F61918 /* AddrHistory+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AddrHistory+CoreDataClass.swift"; sourceTree = "<group>"; };
55+
94DBA15D27DBDAC000F61918 /* AddrHistory+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AddrHistory+CoreDataProperties.swift"; sourceTree = "<group>"; };
5056
/* End PBXFileReference section */
5157

5258
/* Begin PBXFrameworksBuildPhase section */
@@ -72,7 +78,10 @@
7278
080E96DDFE201D6D7F000001 /* Classes */ = {
7379
isa = PBXGroup;
7480
children = (
81+
94DBA15C27DBDAC000F61918 /* AddrHistory+CoreDataClass.swift */,
82+
94DBA15D27DBDAC000F61918 /* AddrHistory+CoreDataProperties.swift */,
7583
94B1C506256ABC1B00F992A3 /* IPSubnetcalc.swift */,
84+
94DBA15427DBD0BF00F61918 /* SubnetCalc.xcdatamodeld */,
7685
94AC2612256AF7C900811156 /* SubnetCalcAppDelegate.swift */,
7786
);
7887
name = Classes;
@@ -267,7 +276,10 @@
267276
buildActionMask = 2147483647;
268277
files = (
269278
94B1C507256ABC1B00F992A3 /* IPSubnetcalc.swift in Sources */,
279+
94DBA15627DBD0BF00F61918 /* SubnetCalc.xcdatamodeld in Sources */,
270280
94AC2613256AF7C900811156 /* SubnetCalcAppDelegate.swift in Sources */,
281+
94DBA15E27DBDAC000F61918 /* AddrHistory+CoreDataClass.swift in Sources */,
282+
94DBA15F27DBDAC000F61918 /* AddrHistory+CoreDataProperties.swift in Sources */,
271283
);
272284
runOnlyForDeploymentPostprocessing = 0;
273285
};
@@ -573,6 +585,19 @@
573585
defaultConfigurationName = Debug;
574586
};
575587
/* End XCConfigurationList section */
588+
589+
/* Begin XCVersionGroup section */
590+
94DBA15427DBD0BF00F61918 /* SubnetCalc.xcdatamodeld */ = {
591+
isa = XCVersionGroup;
592+
children = (
593+
94DBA15527DBD0BF00F61918 /* SubnetCalc.xcdatamodel */,
594+
);
595+
currentVersion = 94DBA15527DBD0BF00F61918 /* SubnetCalc.xcdatamodel */;
596+
path = SubnetCalc.xcdatamodeld;
597+
sourceTree = "<group>";
598+
versionGroupType = wrapper.xcdatamodel;
599+
};
600+
/* End XCVersionGroup section */
576601
};
577602
rootObject = 29B97313FDCFA39411CA2CEA /* Project object */;
578603
}

SubnetCalcAppDelegate.swift

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Foundation
99
import Cocoa
10+
import CoreData
1011

1112
@main
1213
class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate, NSTableViewDataSource {
@@ -95,6 +96,8 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
9596
private var ipsc: IPSubnetCalc?
9697
private var subnetsVLSM = [(Int, String, String)]()
9798
private var globalMaskVLSM: UInt32!
99+
private var container: NSPersistentContainer!
100+
private var history = [AddrHistory]()
98101

99102
//**********************
100103
//Private IPv4 functions
@@ -1427,7 +1430,6 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
14271430
/**
14281431
Triggered when the user hit Enter key in the IP address field
14291432

1430-
14311433
- Parameter sender: sender of the action
14321434

14331435
*/
@@ -1437,15 +1439,23 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
14371439
if ((sender as? NSTextField)?.stringValue) != nil {
14381440
if (sender.stringValue != "") {
14391441
if (addrField.indexOfItem(withObjectValue: sender.stringValue!) == NSNotFound) {
1440-
if (addrField.numberOfItems == Constants.maxAddrHistory) {
1442+
if (addrField.numberOfItems >= Constants.maxAddrHistory) {
14411443
addrField.removeItem(at: 0)
1444+
if (history.count > 0) {
1445+
container.viewContext.delete(history[0])
1446+
history.remove(at: 0)
1447+
saveHistory()
1448+
}
14421449
}
14431450
addrField.addItem(withObjectValue: sender.stringValue!)
1451+
let historyItem = AddrHistory(context: container.viewContext)
1452+
historyItem.address = sender.stringValue!
1453+
history.append(historyItem)
1454+
saveHistory()
14441455
}
14451456
self.calc(sender)
14461457
}
14471458
}
1448-
14491459
}
14501460

14511461
/**
@@ -1646,6 +1656,61 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
16461656
}
16471657
}
16481658

1659+
/**
1660+
Clear address IP field history
1661+
1662+
Triggered when the user select Clear history in the window menu.
1663+
1664+
- Parameter sender: non used
1665+
1666+
*/
1667+
@IBAction func clearHistory(_ sender: AnyObject)
1668+
{
1669+
for _ in (0...addrField.numberOfItems-1) {
1670+
addrField.removeItem(at: 0)
1671+
}
1672+
for _ in (0...history.count-1) {
1673+
container.viewContext.delete(history[0])
1674+
history.remove(at: 0)
1675+
saveHistory()
1676+
}
1677+
}
1678+
1679+
/**
1680+
Save the address IP field history for future App sessions
1681+
*/
1682+
private func saveHistory() {
1683+
if container.viewContext.hasChanges {
1684+
do {
1685+
try container.viewContext.save()
1686+
} catch {
1687+
print("An error occurred while saving: \(error)")
1688+
}
1689+
}
1690+
}
1691+
1692+
/**
1693+
Load in the address IP field the history of previous App sessions
1694+
*/
1695+
private func loadHistory() {
1696+
container = NSPersistentContainer(name: "SubnetCalc")
1697+
container.loadPersistentStores { storeDescription, error in
1698+
if let error = error {
1699+
print("Unresolved error \(error)")
1700+
}
1701+
}
1702+
do {
1703+
history = try container.viewContext.fetch(AddrHistory.fetchRequest())
1704+
//print("Got \(history.count) items")
1705+
for item in history {
1706+
//print(item.address)
1707+
addrField.addItem(withObjectValue: item.address)
1708+
}
1709+
} catch {
1710+
print("Fetch history failed")
1711+
}
1712+
}
1713+
16491714
/**
16501715
Auto invoked when the Main Windows has been resized
16511716
*/
@@ -1667,6 +1732,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
16671732
*/
16681733
func applicationDidFinishLaunching(_ aNotification: Notification) {
16691734
// Insert code here to initialize your application
1735+
loadHistory()
16701736
initSubnetsTab()
16711737
initCIDRTab()
16721738
initIPv6Tab()

0 commit comments

Comments
 (0)