diff --git a/src/happyx_native/abstract/saving.nim b/src/happyx_native/abstract/saving.nim index 5a4d709..c7742e4 100644 --- a/src/happyx_native/abstract/saving.nim +++ b/src/happyx_native/abstract/saving.nim @@ -1,8 +1,9 @@ import - os, - json, - strformat, - strutils, + std/os, + std/json, + std/strformat, + std/strutils, + std/enumutils, ../app/app export @@ -15,44 +16,128 @@ when defined(export2android): ../android/autils -template saveAndLoad[T](saveFunc, loadFunc, parseFunc: untyped, tdesc: typedesc[T], - genLoad: bool = true, genSave: bool = true) = - when genSave: - proc `saveFunc`*(filename: string, data: `tdesc`) = - when defined(export2android): - var sharedPreferences = appContext.getSharedPreferences(cfgAndroidPackageId(), MODE_PRIVATE) - sharedPreferences.edit().putString(filename, $data).apply() - else: - if not dirExists(getHomeDir() / "hpxnative"): - createDir(getHomeDir() / "hpxnative") - var - userFolder = getHomeDir() / "hpxnative" / filename - f = open(userFolder, fmWrite) - f.write($data) - f.close() - when genLoad: - proc `loadFunc`*(filename: string): `tdesc` = - when defined(export2android): - var sharedPreferences = appContext.getSharedPreferences(cfgAndroidPackageId(), MODE_PRIVATE) - return `parseFunc`(sharedPreferences.getString(filename, "")) - else: - if not dirExists(getHomeDir() / "hpxnative"): - createDir(getHomeDir() / "hpxnative") - if not fileExists(getHomeDir() / "hpxnative" / filename): - return `tdesc`.default - var - userFolder = getHomeDir() / "hpxnative" / filename - f = open(userFolder, fmRead) - data = f.readAll() - f.close() - return `parseFunc`(data) - - -saveAndLoad(save, loadJson, parseJson, JsonNode) -saveAndLoad(save, loadString, `$`, string) -saveAndLoad(save, loadInt, parseInt, int) -saveAndLoad(save, loadFloat, parseFloat, float) -saveAndLoad(save, loadBool, parseBool, bool) -saveAndLoad(save, loadHexInt, parseHexInt, int, genSave = false) -saveAndLoad(save, loadHexInt, parseHexInt, int, genSave = false) -saveAndLoad(save, loadOctInt, parseOctInt, int, genSave = false) +proc save*(filename: string, data: string) = + ## Saves string in filename. + ## + ## On Android it uses SharedPreferences to data saving + ## + when defined(export2android): + var sharedPreferences = appContext.getSharedPreferences(cfgAndroidPackageId(), MODE_PRIVATE) + sharedPreferences.edit().putString(filename, data).apply() + else: + if not dirExists(getHomeDir() / "hpxnative"): + createDir(getHomeDir() / "hpxnative") + var + userFolder = getHomeDir() / "hpxnative" / filename + f = open(userFolder, fmWrite) + f.write(data) + f.close() + + +proc save*(filename: string, data: SomeNumber) = + ## Saves number in filename. + ## + ## On Android it uses SharedPreferences to data saving + ## + filename.save($data) + + +proc save*(filename: string, data: JsonNode) = + ## Saves JSON in filename. + ## + ## On Android it uses SharedPreferences to data saving + ## + filename.save($data) + + +proc save*(filename: string, data: bool) = + ## Saves boolean in filename. + ## + ## On Android it uses SharedPreferences to data saving + ## + filename.save($data) + + +proc save*[T: enum](filename: string, data: T) = + ## Saves Enum in filename. + ## + ## On Android it uses SharedPreferences to data saving + ## + filename.save(data.symbolName) + + +proc loadString*(filename: string): string = + ## Loads string from filename + ## + ## On Android it uses SharedPreferences to data loading + ## + when defined(export2android): + var sharedPreferences = appContext.getSharedPreferences(cfgAndroidPackageId(), MODE_PRIVATE) + return $sharedPreferences.getString(filename, "") + else: + if not dirExists(getHomeDir() / "hpxnative"): + createDir(getHomeDir() / "hpxnative") + if not fileExists(getHomeDir() / "hpxnative" / filename): + return `tdesc`.default + var + userFolder = getHomeDir() / "hpxnative" / filename + f = open(userFolder, fmRead) + data = f.readAll() + f.close() + return data + + +proc loadJson*(filename: string): JsonNode = + ## Loads JSON from filename + ## + ## On Android it uses SharedPreferences to data loading + ## + return parseJson(filename.loadString()) + + +proc loadInt*(filename: string): int = + ## Loads integer from filename + ## + ## On Android it uses SharedPreferences to data loading + ## + return parseInt(filename.loadString()) + + +proc loadFloat*(filename: string): float = + ## Loads float from filename + ## + ## On Android it uses SharedPreferences to data loading + ## + return parseFloat(filename.loadString()) + + +proc loadBool*(filename: string): bool = + ## Loads boolean from filename + ## + ## On Android it uses SharedPreferences to data loading + ## + return parseBool(filename.loadString()) + + +proc loadHexInt*(filename: string): int = + ## Loads integer from filename + ## + ## On Android it uses SharedPreferences to data loading + ## + return parseHexInt(filename.loadString()) + + +proc loadOctInt*(filename: string): int = + ## Loads integer from filename + ## + ## On Android it uses SharedPreferences to data loading + ## + return parseOctInt(filename.loadString()) + + +proc loadEnum*[T: enum](filename: string): T = + ## Loads integer from filename + ## + ## On Android it uses SharedPreferences to data loading + ## + return parseEnum[T](filename.loadString()) diff --git a/tests/test/.gitignore b/tests/test/.gitignore new file mode 100644 index 0000000..78a7926 --- /dev/null +++ b/tests/test/.gitignore @@ -0,0 +1,15 @@ +# Build +build/ +*.apk +*.exe + +# Android +.gradle/ +.idea/ + +# Logs +*.log +*.lg + +# Db +*.db diff --git a/tests/test/README.md b/tests/test/README.md new file mode 100644 index 0000000..239b59b --- /dev/null +++ b/tests/test/README.md @@ -0,0 +1,10 @@ +# test +### made with HappyX Native with ❤ + +## Get Started 👨🔬 + +Main frontend file is `/assets/main.nim`. It compiles into JS. +This files includes into `/assets/index.html`. + +Main file is `app.nim`. It compiles into C/C++. + diff --git a/tests/test/app.nim b/tests/test/app.nim new file mode 100644 index 0000000..8c37b10 --- /dev/null +++ b/tests/test/app.nim @@ -0,0 +1,11 @@ +# Main native file +import happyx_native + + +callback: + # HappyX Native helloWorld callback + proc helloWorld() = + echo "Hello from Nim" + + +nativeApp("/assets", resizeable = false, title = "test") diff --git a/tests/test/assets/favicon.ico b/tests/test/assets/favicon.ico new file mode 100644 index 0000000..46f49da Binary files /dev/null and b/tests/test/assets/favicon.ico differ diff --git a/tests/test/assets/favicon.png b/tests/test/assets/favicon.png new file mode 100644 index 0000000..aec729c Binary files /dev/null and b/tests/test/assets/favicon.png differ diff --git a/tests/test/assets/index.html b/tests/test/assets/index.html new file mode 100644 index 0000000..5c677a3 --- /dev/null +++ b/tests/test/assets/index.html @@ -0,0 +1,10 @@ + + +
+