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 @@ + + + + test + + +
+ + + diff --git a/tests/test/assets/main.nim b/tests/test/assets/main.nim new file mode 100644 index 0000000..6fb2665 --- /dev/null +++ b/tests/test/assets/main.nim @@ -0,0 +1,62 @@ +import + happyx, + jsffi + + +# Object for working with HappyX Native +var hpxNative {.importc, nodecl.}: JsObject + +var x = remember 0 + + +appRoutes "app": + "/": + tDiv(class = "container"): + tH1: + "test" + tDiv(class = "content"): + "x is {x}" + tButton: + "increase" + @click: + # Call HappyX Native callback named helloWorld without arguments + hpxNative.callNim("helloWorld") + x->inc() + tStyle: """ + body { + padding: 0; + margin: 0; + } + .container { + color: #efefef; + width: 100vw; + height: 100vh; + display: flex; + flex-direction: column; + gap: 1rem; + background-color: #0A0A0A; + justify-content: center; + align-items: center; + } + .content { + gap: .2rem; + display: flex; + flex-direction: column; + align-items: center; + } + button { + padding: .4rem 1.2rem; + font-weight: bold; + transition: all; + border: none; + border-radius: 8px; + transition-duration: .3s; + background-color: #ecf; + } + button:hover { + background-color: #cbe; + } + button:active { + background-color: #bad; + } + """ diff --git a/tests/test/happyx.native.cfg b/tests/test/happyx.native.cfg new file mode 100644 index 0000000..28cfd6a --- /dev/null +++ b/tests/test/happyx.native.cfg @@ -0,0 +1,9 @@ +# HappyX Native config file +[Main] +name = r"test" +kind = r"SPA" +androidSdk = r"C:\Users\79833\AppData\Local\Android\Sdk" +androidPackage = r"com.hapticx.tmpl" +appDirectory = r"/assets" +version = 1.0.0 +port = 5123 # uses in android build