Skip to content

Commit

Permalink
upgrade saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Jun 30, 2024
1 parent b26f601 commit 6833a77
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 45 deletions.
175 changes: 130 additions & 45 deletions src/happyx_native/abstract/saving.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import
os,
json,
strformat,
strutils,
std/os,
std/json,
std/strformat,
std/strutils,
std/enumutils,
../app/app

export
Expand All @@ -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())
15 changes: 15 additions & 0 deletions tests/test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Build
build/
*.apk
*.exe

# Android
.gradle/
.idea/

# Logs
*.log
*.lg

# Db
*.db
10 changes: 10 additions & 0 deletions tests/test/README.md
Original file line number Diff line number Diff line change
@@ -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++.

11 changes: 11 additions & 0 deletions tests/test/app.nim
Original file line number Diff line number Diff line change
@@ -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")
Binary file added tests/test/assets/favicon.ico
Binary file not shown.
Binary file added tests/test/assets/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/test/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<div id="app"></div>
<script src="main.js"></script>
</body>
</html>
62 changes: 62 additions & 0 deletions tests/test/assets/main.nim
Original file line number Diff line number Diff line change
@@ -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;
}
"""
9 changes: 9 additions & 0 deletions tests/test/happyx.native.cfg
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6833a77

Please sign in to comment.