Implement database access (with migrations)

This commit is contained in:
2019-01-26 22:58:53 -06:00
parent e19339d808
commit 7cab0a39e5
67 changed files with 1826 additions and 136 deletions

View File

@@ -1,49 +0,0 @@
import Foundation
@objc(CoreModule)
class CoreModule: RCTEventEmitter {
@objc
open override func supportedEvents() -> [String] {
return ["onHabitList"]
}
@objc
func requestHabitList() {
DispatchQueue.main.async {
let habits = AppDelegate.backend.getHabitList()
let result = habits.map {
["key": String($0.key.intValue),
"name": $0.value["name"],
"color": $0.value["color"]]
}
self.sendEvent(withName: "onHabitList", body: result)
}
}
@objc
func createHabit(_ name: String) {
DispatchQueue.main.async {
AppDelegate.backend.createHabit(name: name)
}
}
@objc
func deleteHabit(_ id: Int32) {
DispatchQueue.main.async {
AppDelegate.backend.deleteHabit(id: id)
}
}
@objc
func updateHabit(_ id: Int32, _ name: String) {
DispatchQueue.main.async {
AppDelegate.backend.updateHabit(id: id, name: name)
}
}
@objc
override static func requiresMainQueueSetup() -> Bool {
return true
}
}

View File

@@ -15,6 +15,13 @@
000BCDF521F69E1400F4DA11 /* FontAwesome Regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 000BCDF421F69E1400F4DA11 /* FontAwesome Regular.ttf */; };
000BCE0521F6CB1100F4DA11 /* libRCTWebSocket.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 000BCDFE21F6CAFF00F4DA11 /* libRCTWebSocket.a */; };
000C283821F51C9B00C5EC6D /* libRNSVG.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 000C283521F51C4E00C5EC6D /* libRNSVG.a */; };
0021019C21F8AA3E00F9283D /* IosDatabase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0021019B21F8AA3E00F9283D /* IosDatabase.swift */; };
002101A421F936A300F9283D /* IosSqlDatabaseTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 002101A321F936A300F9283D /* IosSqlDatabaseTest.swift */; };
002101AC21F9428C00F9283D /* core.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0008A5C021F16D25000DB3E7 /* core.framework */; };
0091878521FD70B5001BDE6B /* IosLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0091878421FD70B5001BDE6B /* IosLog.swift */; };
00B2AC3D21FCA9D900CBEC8E /* IosFiles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00B2AC3C21FCA9D900CBEC8E /* IosFiles.swift */; };
00B2AC6521FD1A4500CBEC8E /* migrations in Resources */ = {isa = PBXBuildFile; fileRef = 00B2AC6421FD1A4500CBEC8E /* migrations */; };
00B2AC6821FD1DA700CBEC8E /* IosFilesTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00B2AC6621FD1CEF00CBEC8E /* IosFilesTest.swift */; };
00C302E51ABCBA2D00DB3ED1 /* libRCTActionSheet.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302AC1ABCB8CE00DB3ED1 /* libRCTActionSheet.a */; };
00C302E71ABCBA2D00DB3ED1 /* libRCTGeolocation.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302BA1ABCB90400DB3ED1 /* libRCTGeolocation.a */; };
00C302E81ABCBA2D00DB3ED1 /* libRCTImage.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302C01ABCB91800DB3ED1 /* libRCTImage.a */; };
@@ -72,6 +79,13 @@
remoteGlobalIDString = 94DDAC5C1F3D024300EED511;
remoteInfo = "RNSVG-tvOS";
};
002101A621F936A300F9283D /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 83CBB9F71A601CBA00E9B192 /* Project object */;
proxyType = 1;
remoteGlobalIDString = 13B07F861A680F5B00A75B9A;
remoteInfo = uhabits;
};
00C302AB1ABCB8CE00DB3ED1 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 00C302A71ABCB8CE00DB3ED1 /* RCTActionSheet.xcodeproj */;
@@ -316,11 +330,19 @@
0008A5C021F16D25000DB3E7 /* core.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = core.framework; path = ../core/build/bin/iOS/main/debug/framework/core.framework; sourceTree = "<group>"; };
0008A5F521F17513000DB3E7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = uhabits/AppDelegate.swift; sourceTree = "<group>"; };
0008A5F721F17531000DB3E7 /* BridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = BridgingHeader.h; path = uhabits/BridgingHeader.h; sourceTree = "<group>"; };
0008A62921F2B728000DB3E7 /* CoreModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreModule.swift; sourceTree = "<group>"; };
0008A62921F2B728000DB3E7 /* CoreModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = CoreModule.swift; path = uhabits/CoreModule.swift; sourceTree = "<group>"; };
0008A62B21F2B755000DB3E7 /* CoreModuleBridge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = CoreModuleBridge.m; path = uhabits/CoreModuleBridge.m; sourceTree = "<group>"; };
000BCDF421F69E1400F4DA11 /* FontAwesome Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = "FontAwesome Regular.ttf"; path = "../react-native/res/fonts/FontAwesome Regular.ttf"; sourceTree = "<group>"; };
000BCDF621F6CAFF00F4DA11 /* RCTWebSocket.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTWebSocket.xcodeproj; path = "../react-native/node_modules/react-native/Libraries/WebSocket/RCTWebSocket.xcodeproj"; sourceTree = "<group>"; };
000C280A21F51C4E00C5EC6D /* RNSVG.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RNSVG.xcodeproj; path = "../react-native/node_modules/react-native-svg/ios/RNSVG.xcodeproj"; sourceTree = "<group>"; };
0021019B21F8AA3E00F9283D /* IosDatabase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = IosDatabase.swift; path = uhabits/IosDatabase.swift; sourceTree = "<group>"; };
002101A121F936A300F9283D /* uhabitsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = uhabitsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
002101A321F936A300F9283D /* IosSqlDatabaseTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IosSqlDatabaseTest.swift; sourceTree = "<group>"; };
002101A521F936A300F9283D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0091878421FD70B5001BDE6B /* IosLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = IosLog.swift; path = uhabits/IosLog.swift; sourceTree = "<group>"; };
00B2AC3C21FCA9D900CBEC8E /* IosFiles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = IosFiles.swift; path = uhabits/IosFiles.swift; sourceTree = "<group>"; };
00B2AC6421FD1A4500CBEC8E /* migrations */ = {isa = PBXFileReference; lastKnownFileType = folder; name = migrations; path = ../core/assets/main/migrations; sourceTree = "<group>"; };
00B2AC6621FD1CEF00CBEC8E /* IosFilesTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IosFilesTest.swift; sourceTree = "<group>"; };
00C302A71ABCB8CE00DB3ED1 /* RCTActionSheet.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTActionSheet.xcodeproj; path = "../node_modules/react-native/Libraries/ActionSheetIOS/RCTActionSheet.xcodeproj"; sourceTree = "<group>"; };
00C302B51ABCB90400DB3ED1 /* RCTGeolocation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTGeolocation.xcodeproj; path = "../node_modules/react-native/Libraries/Geolocation/RCTGeolocation.xcodeproj"; sourceTree = "<group>"; };
00C302BB1ABCB91800DB3ED1 /* RCTImage.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTImage.xcodeproj; path = "../node_modules/react-native/Libraries/Image/RCTImage.xcodeproj"; sourceTree = "<group>"; };
@@ -339,6 +361,14 @@
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
0021019E21F936A300F9283D /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
002101AC21F9428C00F9283D /* core.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
13B07F8C1A680F5B00A75B9A /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
@@ -365,6 +395,7 @@
000BCDCC21F69E0000F4DA11 /* Assets */ = {
isa = PBXGroup;
children = (
00B2AC6421FD1A4500CBEC8E /* migrations */,
000BCDF421F69E1400F4DA11 /* FontAwesome Regular.ttf */,
);
name = Assets;
@@ -390,6 +421,17 @@
name = Products;
sourceTree = "<group>";
};
002101A221F936A300F9283D /* Unit Tests */ = {
isa = PBXGroup;
children = (
002101A521F936A300F9283D /* Info.plist */,
002101A321F936A300F9283D /* IosSqlDatabaseTest.swift */,
00B2AC6621FD1CEF00CBEC8E /* IosFilesTest.swift */,
);
name = "Unit Tests";
path = uhabitsTest;
sourceTree = "<group>";
};
00C302A81ABCB8CE00DB3ED1 /* Products */ = {
isa = PBXGroup;
children = (
@@ -437,12 +479,15 @@
isa = PBXGroup;
children = (
13B07FB61A68108700A75B9A /* Info.plist */,
0008A5F521F17513000DB3E7 /* AppDelegate.swift */,
0008A5F721F17531000DB3E7 /* BridgingHeader.h */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
13B07FB11A68108700A75B9A /* LaunchScreen.xib */,
0008A62921F2B728000DB3E7 /* CoreModule.swift */,
0008A5F721F17531000DB3E7 /* BridgingHeader.h */,
0008A62B21F2B755000DB3E7 /* CoreModuleBridge.m */,
0008A5F521F17513000DB3E7 /* AppDelegate.swift */,
0008A62921F2B728000DB3E7 /* CoreModule.swift */,
0021019B21F8AA3E00F9283D /* IosDatabase.swift */,
00B2AC3C21FCA9D900CBEC8E /* IosFiles.swift */,
0091878421FD70B5001BDE6B /* IosLog.swift */,
);
name = Application;
sourceTree = "<group>";
@@ -530,6 +575,7 @@
children = (
000BCDCC21F69E0000F4DA11 /* Assets */,
13B07FAE1A68108700A75B9A /* Application */,
002101A221F936A300F9283D /* Unit Tests */,
832341AE1AAA6A7D00B99B32 /* Libraries */,
83CBBA001A601CBA00E9B192 /* Products */,
2D16E6871FA4F8E400B85C8A /* Frameworks */,
@@ -543,6 +589,7 @@
isa = PBXGroup;
children = (
13B07F961A680F5B00A75B9A /* uhabits.app */,
002101A121F936A300F9283D /* uhabitsTests.xctest */,
);
name = Products;
sourceTree = "<group>";
@@ -559,6 +606,24 @@
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
002101A021F936A300F9283D /* uhabitsTests */ = {
isa = PBXNativeTarget;
buildConfigurationList = 002101A821F936A300F9283D /* Build configuration list for PBXNativeTarget "uhabitsTests" */;
buildPhases = (
0021019D21F936A300F9283D /* Sources */,
0021019E21F936A300F9283D /* Frameworks */,
0021019F21F936A300F9283D /* Resources */,
);
buildRules = (
);
dependencies = (
002101A721F936A300F9283D /* PBXTargetDependency */,
);
name = uhabitsTests;
productName = uhabitsTests;
productReference = 002101A121F936A300F9283D /* uhabitsTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
13B07F861A680F5B00A75B9A /* uhabits */ = {
isa = PBXNativeTarget;
buildConfigurationList = 13B07F931A680F5B00A75B9A /* Build configuration list for PBXNativeTarget "uhabits" */;
@@ -584,9 +649,16 @@
83CBB9F71A601CBA00E9B192 /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1010;
LastUpgradeCheck = 0940;
ORGANIZATIONNAME = Facebook;
ORGANIZATIONNAME = "Loop Habit Tracker";
TargetAttributes = {
002101A021F936A300F9283D = {
CreatedOnToolsVersion = 10.1;
DevelopmentTeam = R5YTHGE3PS;
ProvisioningStyle = Automatic;
TestTargetID = 13B07F861A680F5B00A75B9A;
};
13B07F861A680F5B00A75B9A = {
DevelopmentTeam = R5YTHGE3PS;
LastSwiftMigration = 1010;
@@ -657,6 +729,7 @@
projectRoot = "";
targets = (
13B07F861A680F5B00A75B9A /* uhabits */,
002101A021F936A300F9283D /* uhabitsTests */,
);
};
/* End PBXProject section */
@@ -931,6 +1004,13 @@
/* End PBXReferenceProxy section */
/* Begin PBXResourcesBuildPhase section */
0021019F21F936A300F9283D /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
13B07F8E1A680F5B00A75B9A /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
@@ -938,6 +1018,7 @@
000BCDF521F69E1400F4DA11 /* FontAwesome Regular.ttf in Resources */,
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
13B07FBD1A68108700A75B9A /* LaunchScreen.xib in Resources */,
00B2AC6521FD1A4500CBEC8E /* migrations in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -956,16 +1037,28 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh\n";
};
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
0021019D21F936A300F9283D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
002101A421F936A300F9283D /* IosSqlDatabaseTest.swift in Sources */,
00B2AC6821FD1DA700CBEC8E /* IosFilesTest.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
13B07F871A680F5B00A75B9A /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
00B2AC3D21FCA9D900CBEC8E /* IosFiles.swift in Sources */,
0008A62C21F2B755000DB3E7 /* CoreModuleBridge.m in Sources */,
0091878521FD70B5001BDE6B /* IosLog.swift in Sources */,
0021019C21F8AA3E00F9283D /* IosDatabase.swift in Sources */,
0008A62A21F2B728000DB3E7 /* CoreModule.swift in Sources */,
0008A5F621F17513000DB3E7 /* AppDelegate.swift in Sources */,
);
@@ -973,6 +1066,14 @@
};
/* End PBXSourcesBuildPhase section */
/* Begin PBXTargetDependency section */
002101A721F936A300F9283D /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 13B07F861A680F5B00A75B9A /* uhabits */;
targetProxy = 002101A621F936A300F9283D /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */
/* Begin PBXVariantGroup section */
13B07FB11A68108700A75B9A /* LaunchScreen.xib */ = {
isa = PBXVariantGroup;
@@ -986,6 +1087,71 @@
/* End PBXVariantGroup section */
/* Begin XCBuildConfiguration section */
002101A921F936A300F9283D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = R5YTHGE3PS;
FRAMEWORK_SEARCH_PATHS = "${PROJECT_DIR}/../core/build/bin/iOS/main/debug/framework/**";
GCC_C_LANGUAGE_STANDARD = gnu11;
HEADER_SEARCH_PATHS = "${PROJECT_DIR}/../core/build/bin/iOS/main/debug/framework/core.framework/**";
INFOPLIST_FILE = "$(PROJECT_DIR)/uhabitsTest/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = org.isoron.uhabitsTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OBJC_BRIDGING_HEADER = "${PROJECT_DIR}/uhabits/BridgingHeader.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/uhabits.app/uhabits";
};
name = Debug;
};
002101AA21F936A300F9283D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = R5YTHGE3PS;
FRAMEWORK_SEARCH_PATHS = "${PROJECT_DIR}/../core/build/bin/iOS/main/debug/framework/**";
GCC_C_LANGUAGE_STANDARD = gnu11;
HEADER_SEARCH_PATHS = "${PROJECT_DIR}/../core/build/bin/iOS/main/debug/framework/core.framework/**";
INFOPLIST_FILE = "$(PROJECT_DIR)/uhabitsTest/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = org.isoron.uhabitsTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "${PROJECT_DIR}/uhabits/BridgingHeader.h";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/uhabits.app/uhabits";
};
name = Release;
};
13B07F941A680F5B00A75B9A /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
@@ -1140,6 +1306,15 @@
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
002101A821F936A300F9283D /* Build configuration list for PBXNativeTarget "uhabitsTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
002101A921F936A300F9283D /* Debug */,
002101AA21F936A300F9283D /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
13B07F931A680F5B00A75B9A /* Build configuration list for PBXNativeTarget "uhabits" */ = {
isa = XCConfigurationList;
buildConfigurations = (

View File

@@ -40,8 +40,19 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "002101A021F936A300F9283D"
BuildableName = "uhabitsTests.xctest"
BlueprintName = "uhabitsTests"
ReferencedContainer = "container:uhabits.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
@@ -75,6 +86,13 @@
ReferencedContainer = "container:uhabits.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "OS_ACTIVITY_MODE"
value = "disable"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>

View File

@@ -1,22 +1,35 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Foundation
import SQLite3
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var bridge: RCTBridge!
static var backend = Backend()
static var backend = Backend(databaseOpener: IosDatabaseOpener(),
fileOpener: IosFileOpener(),
log: IosLog())
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AppDelegate.backend.createHabit(name: "Wake up early")
AppDelegate.backend.createHabit(name: "Wash clothes")
AppDelegate.backend.createHabit(name: "Exercise")
AppDelegate.backend.createHabit(name: "Meditate")
AppDelegate.backend.createHabit(name: "Take vitamins")
AppDelegate.backend.createHabit(name: "Write 'the quick brown fox jumps over the lazy dog' daily")
AppDelegate.backend.createHabit(name: "Write journal")
AppDelegate.backend.createHabit(name: "Study French")
let jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index.ios", fallbackResource: nil)
let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "LoopHabitTracker", initialProperties: nil, launchOptions: launchOptions)
rootView?.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

View File

@@ -1,3 +1,22 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTBridgeModule.h>

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Foundation
extension String: Error {}
@objc(CoreModule)
class CoreModule: RCTEventEmitter {
func convert(_ obj: Any?) -> Any? {
if obj is KotlinInt {
return (obj as! KotlinInt).intValue
}
if obj is NSString {
return obj
}
if obj is Dictionary<String, Any> {
return (obj as! Dictionary<String, Any>).mapValues{ convert($0) }
}
if obj is Array<Any> {
return (obj as! Array<Any>).map { convert($0) }
}
return nil
}
@objc
open override func supportedEvents() -> [String] {
return ["onHabitList"]
}
@objc
func requestHabitList() {
DispatchQueue.main.async {
let result = AppDelegate.backend.getHabitList()
self.sendEvent(withName: "onHabitList", body: self.convert(result))
}
}
@objc
func createHabit(_ name: String) {
DispatchQueue.main.async {
AppDelegate.backend.createHabit(name: name)
}
}
@objc
func deleteHabit(_ id: Int32) {
DispatchQueue.main.async {
AppDelegate.backend.deleteHabit(id: id)
}
}
@objc
func updateHabit(_ id: Int32, _ name: String) {
DispatchQueue.main.async {
AppDelegate.backend.updateHabit(id: id, name: name)
}
}
@objc
override static func requiresMainQueueSetup() -> Bool {
return true
}
}

View File

@@ -1,3 +1,22 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

View File

@@ -0,0 +1,115 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Foundation
import SQLite3
internal let SQLITE_STATIC = unsafeBitCast(0, to: sqlite3_destructor_type.self)
internal let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
class IosPreparedStatement : NSObject, PreparedStatement {
var db: OpaquePointer
var statement: OpaquePointer
init(withStatement statement: OpaquePointer, withDb db: OpaquePointer) {
self.statement = statement
self.db = db
}
func step() -> StepResult {
let result = sqlite3_step(statement)
if result == SQLITE_ROW {
return StepResult.row
} else if result == SQLITE_DONE {
return StepResult.done
} else {
let errMsg = String(cString: sqlite3_errmsg(db)!)
fatalError("Database error: \(errMsg) (\(result))")
}
}
func getInt(index: Int32) -> Int32 {
return sqlite3_column_int(statement, index)
}
func getText(index: Int32) -> String {
return String(cString: sqlite3_column_text(statement, index))
}
func bindInt(index: Int32, value: Int32) {
sqlite3_bind_int(statement, index, value)
}
func bindText(index: Int32, value: String) {
sqlite3_bind_text(statement, index, value, -1, SQLITE_TRANSIENT)
}
func reset() {
sqlite3_reset(statement)
}
override func finalize() {
sqlite3_finalize(statement)
}
}
class IosDatabase : NSObject, Database {
var db: OpaquePointer
init(withDb db: OpaquePointer) {
self.db = db
}
func prepareStatement(sql: String) -> PreparedStatement {
if sql.isEmpty {
fatalError("Provided SQL query is empty")
}
print("Running SQL: \(sql)")
var statement : OpaquePointer?
let result = sqlite3_prepare_v2(db, sql, -1, &statement, nil)
if result == SQLITE_OK {
return IosPreparedStatement(withStatement: statement!, withDb: db)
} else {
let errMsg = String(cString: sqlite3_errmsg(db)!)
fatalError("Database error: \(errMsg)")
}
}
func close() {
sqlite3_close(db)
}
}
class IosDatabaseOpener : NSObject, DatabaseOpener {
func open(file: UserFile) -> Database {
let dbPath = (file as! IosUserFile).path
let version = String(cString: sqlite3_libversion())
print("SQLite \(version)")
print("Opening database: \(dbPath)")
var db: OpaquePointer?
let result = sqlite3_open(dbPath, &db)
if result == SQLITE_OK {
return IosDatabase(withDb: db!)
} else {
fatalError("Error opening database (code \(result))")
}
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Foundation
class IosResourceFile : NSObject, ResourceFile {
var path: String
var fileManager = FileManager.default
init(forPath path: String) {
self.path = path
}
func readLines() -> [String] {
do {
let contents = try String(contentsOfFile: self.path, encoding: .utf8)
return contents.components(separatedBy: CharacterSet.newlines)
} catch {
return [""]
}
}
}
class IosUserFile : NSObject, UserFile {
var path: String
init(forPath path: String) {
self.path = path
}
func delete() {
do {
try FileManager.default.removeItem(atPath: path)
} catch {
}
}
func exists() -> Bool {
return FileManager.default.fileExists(atPath: path)
}
}
class IosFileOpener : NSObject, FileOpener {
func openResourceFile(filename: String) -> ResourceFile {
let path = "\(Bundle.main.resourcePath!)/\(filename)"
return IosResourceFile(forPath: path)
}
func openUserFile(filename: String) -> UserFile {
do {
let root = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path
return IosUserFile(forPath: "\(root)/\(filename)")
} catch {
return IosUserFile(forPath: "invalid")
}
}
}

30
ios/uhabits/IosLog.swift Normal file
View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Foundation
class IosLog : NSObject, Log {
func info(msg: String) {
print("[I] \(msg)")
}
func debug(msg: String) {
print("[D] \(msg)")
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

View File

@@ -0,0 +1,44 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import XCTest
@testable import uhabits
class IosFilesTest: XCTestCase {
func testResourceFiles() {
let fileOpener = IosFileOpener()
let file = fileOpener.openResourceFile(filename: "migrations/010.sql")
let lines = file.readLines()
XCTAssertEqual(lines[0], "delete from Score")
}
func testUserFiles() throws {
let fm = FileManager.default
let root = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path
let path = "\(root)/test.txt"
fm.createFile(atPath: path, contents: "Hello world\nThis is line 2".data(using: .utf8), attributes: nil)
let fileOpener = IosFileOpener()
let file = fileOpener.openUserFile(filename: "test.txt")
XCTAssertTrue(file.exists())
file.delete()
XCTAssertFalse(file.exists())
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import XCTest
@testable import uhabits
class IosDatabaseTest: XCTestCase {
func testUsage() {
let databaseOpener = IosDatabaseOpener()
let fileOpener = IosFileOpener()
let dbFile = fileOpener.openUserFile(filename: "test.sqlite3")
if dbFile.exists() {
dbFile.delete()
}
let db = databaseOpener.open(file: dbFile)
var stmt = db.prepareStatement(sql: "drop table if exists demo")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "begin")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "create table if not exists demo(key int, value text)")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "insert into demo(key, value) values (?1, ?2)")
stmt.bindInt(index: 1, value: 42)
stmt.bindText(index: 2, value: "Hello World")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "select * from demo where key > ?1")
stmt.bindInt(index: 1, value: 10)
var result = stmt.step()
XCTAssertEqual(result, StepResult.row)
XCTAssertEqual(stmt.getInt(index: 0), 42)
XCTAssertEqual(stmt.getText(index: 1), "Hello World")
result = stmt.step()
XCTAssertEqual(result, StepResult.done)
stmt.finalize()
stmt = db.prepareStatement(sql: "drop table demo")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "commit")
stmt.step()
stmt.finalize()
db.close()
dbFile.delete()
}
}