Skip to content

Commit

Permalink
fix saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Jun 30, 2024
1 parent fe77217 commit 74c2e05
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/happyx_native/abstract/saving.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ proc save*[T: enum](filename: string, data: T) =
filename.save(data.symbolName)


proc loadImpl[T](filename: string): string =
proc loadImpl[T](filename: string, parseFunc: proc(s: string): T): T =
## 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, "")
when T is string:
return $sharedPreferences.getString(filename, "")
else:
return parseFunc($sharedPreferences.getString(filename, ""))
else:
if not dirExists(getHomeDir() / "hpxnative"):
createDir(getHomeDir() / "hpxnative")
Expand All @@ -84,68 +87,71 @@ proc loadImpl[T](filename: string): string =
f = open(userFolder, fmRead)
data = f.readAll()
f.close()
return data
when T is string:
return data
else:
return parseFunc($data)


proc loadString*(filename: string): JsonNode =
## Loads JSON from filename
##
## On Android it uses SharedPreferences to data loading
##
return loadImpl[string](filename)
return loadImpl[string](filename, `$`)


proc loadJson*(filename: string): JsonNode =
## Loads JSON from filename
##
## On Android it uses SharedPreferences to data loading
##
return parseJson(loadImpl[JsonNode](filename))
return loadImpl[JsonNode](filename, parseJson)


proc loadInt*(filename: string): int =
## Loads integer from filename
##
## On Android it uses SharedPreferences to data loading
##
return parseInt(loadImpl[int](filename))
return loadImpl[int](filename, parseInt)


proc loadFloat*(filename: string): float =
## Loads float from filename
##
## On Android it uses SharedPreferences to data loading
##
return parseFloat(loadImpl[float](filename))
return loadImpl[float](filename, parseFloat)


proc loadBool*(filename: string): bool =
## Loads boolean from filename
##
## On Android it uses SharedPreferences to data loading
##
return parseBool(loadImpl[bool](filename))
return loadImpl[bool](filename, parseBool)


proc loadHexInt*(filename: string): int =
## Loads integer from filename
##
## On Android it uses SharedPreferences to data loading
##
return parseHexInt(loadImpl[int](filename))
return loadImpl[int](filename, parseHexInt)


proc loadOctInt*(filename: string): int =
## Loads integer from filename
##
## On Android it uses SharedPreferences to data loading
##
return parseOctInt(loadImpl[int](filename))
return loadImpl[int](filename, parseOctInt)


proc loadEnum*[T: enum](filename: string): T =
## Loads integer from filename
##
## On Android it uses SharedPreferences to data loading
##
return parseEnum[T](loadImpl[T](filename))
return loadImpl[T](filename, parseEnum[T])

0 comments on commit 74c2e05

Please sign in to comment.