Skip to content

Commit

Permalink
feat(common): add json user pref service
Browse files Browse the repository at this point in the history
  • Loading branch information
thetric committed Mar 16, 2017
1 parent 79e02a8 commit 0004194
Showing 1 changed file with 34 additions and 0 deletions.
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")
})
}
}

0 comments on commit 0004194

Please sign in to comment.