-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): add json user pref service
- Loading branch information
thetric
committed
Mar 16, 2017
1 parent
79e02a8
commit 0004194
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...roovy/com/github/thetric/iliasdownloader/ui/common/prefs/JsonUserPreferenceService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.github.thetric.iliasdownloader.ui.common.prefs | ||
|
||
import groovy.json.JsonOutput | ||
import groovy.json.JsonSlurper | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Path | ||
|
||
final class JsonUserPreferenceService implements UserPreferenceService { | ||
private final JsonSlurper jsonSlurper | ||
final Path settingsFile | ||
|
||
JsonUserPreferenceService(Path settingsPath) { | ||
this.settingsFile = settingsPath | ||
jsonSlurper = new JsonSlurper() | ||
} | ||
|
||
@Override | ||
UserPreferences loadUserPreferences() throws IOException { | ||
return settingsFile.withInputStream { | ||
def a = new UserPreferences(jsonSlurper.parse(it) as Map) | ||
return a | ||
} as UserPreferences | ||
} | ||
|
||
@Override | ||
void saveUserPreferences(final UserPreferences userPreferences) throws IOException { | ||
settingsFile.withWriter(StandardCharsets.UTF_8.name(), { | ||
def compressedJson = JsonOutput.toJson(userPreferences) | ||
def prettyPrintedJson = JsonOutput.prettyPrint(compressedJson) | ||
it.write("$prettyPrintedJson\n") | ||
}) | ||
} | ||
} |