Skip to content

Commit

Permalink
refactor: use new ui-common api
Browse files Browse the repository at this point in the history
  • Loading branch information
thetric committed Jun 25, 2017
1 parent 9902036 commit a4bc762
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.github.thetric.iliasdownloader.cli

import com.github.thetric.iliasdownloader.cli.console.ConsoleService
import com.github.thetric.iliasdownloader.cli.console.SystemEnvironmentAwareConsoleService
import com.github.thetric.iliasdownloader.ui.common.prefs.JsonUserPreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferenceService
import groovy.transform.CompileStatic
import com.github.thetric.iliasdownloader.ui.common.prefs.JsonPreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.PreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferences
import groovy.transform.TupleConstructor
import groovy.util.logging.Log4j2

Expand All @@ -17,7 +17,6 @@ import static org.apache.logging.log4j.Level.TRACE
*/
@Log4j2
@TupleConstructor
@CompileStatic
final class Cli {
// NOTE DO NOT launch in IntelliJ with 'Delegate IDE build/run actions to gradle' as no console is then available!
// instead you should define system environment variables (see SyncController for the var names)
Expand All @@ -31,7 +30,9 @@ final class Cli {
final cliOptions = cliService.parseOpts(args)
final Path settingsPath = cliOptions.syncDir.resolve(SETTINGS_FILE_NAME)
final ConsoleService consoleService = new SystemEnvironmentAwareConsoleService()
final UserPreferenceService preferenceService = new JsonUserPreferenceService(settingsPath)
final PreferenceService<UserPreferences> preferenceService = new JsonPreferenceService(
settingsPath,
{ new UserPreferences(it) })
new CliController(resourceBundle, cliOptions, settingsPath, consoleService, preferenceService).startCliController()
} catch (final InvalidUsageException ue) {
log.catching(TRACE, ue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import com.github.thetric.iliasdownloader.service.exception.IliasAuthenticationE
import com.github.thetric.iliasdownloader.service.webparser.CookieService
import com.github.thetric.iliasdownloader.service.webparser.JsoupCookieService
import com.github.thetric.iliasdownloader.service.webparser.WebParserIliasServiceProvider
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.PreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferences
import groovy.transform.CompileStatic
import groovy.util.logging.Log4j2

Expand All @@ -25,12 +26,12 @@ final class CliController {
private final CliOptions cliOptions

private final Path settingsPath
private final UserPreferenceService preferenceService
private final PreferenceService<UserPreferences> preferenceService
private final ConsoleService consoleService

CliController(
final ResourceBundle resourceBundle, final CliOptions cliOptions, final Path settingsPath,
final ConsoleService consoleService, final UserPreferenceService preferenceService) {
final ConsoleService consoleService, final PreferenceService<UserPreferences> preferenceService) {
this.resourceBundle = resourceBundle
this.cliOptions = cliOptions
this.settingsPath = settingsPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.github.thetric.iliasdownloader.cli
import com.github.thetric.iliasdownloader.cli.console.ConsoleService
import com.github.thetric.iliasdownloader.service.IliasService
import com.github.thetric.iliasdownloader.service.model.LoginCredentials
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.PreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferences
import groovy.transform.CompileStatic
import groovy.util.logging.Log4j2
Expand All @@ -19,13 +19,13 @@ import java.util.function.Function
final class LoginServiceImpl implements LoginService {
private final Function<String, IliasService> iliasProvider
private final ResourceBundle resourceBundle
private final UserPreferenceService preferenceService
private final PreferenceService<UserPreferences> preferenceService
private final ConsoleService consoleService

LoginServiceImpl(
final Function<String, IliasService> iliasProvider,
final ResourceBundle resourceBundle,
final UserPreferenceService preferenceService, final ConsoleService consoleService) {
final PreferenceService<UserPreferences> preferenceService, final ConsoleService consoleService) {
this.iliasProvider = iliasProvider
this.resourceBundle = resourceBundle
this.preferenceService = preferenceService
Expand All @@ -38,7 +38,7 @@ final class LoginServiceImpl implements LoginService {
}

private IliasService createServiceFromConfig() {
final UserPreferences prefs = preferenceService.loadUserPreferences()
final UserPreferences prefs = preferenceService.loadPreferences()
final IliasService iliasService = iliasProvider.apply(prefs.iliasServerURL)
final String password = promptForPassword(prefs.userName)
iliasService.login(new LoginCredentials(userName: prefs.userName, password: password))
Expand All @@ -59,7 +59,7 @@ final class LoginServiceImpl implements LoginService {
final LoginCredentials credentials = promptForCredentials()
iliasService.login(credentials)
final UserPreferences prefs = new UserPreferences(iliasLoginUrl, credentials.userName, 0)
preferenceService.saveUserPreferences(prefs)
preferenceService.savePreferences(prefs)
return iliasService
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.thetric.iliasdownloader.cli.console.ConsoleService
import com.github.thetric.iliasdownloader.service.IliasItemVisitor
import com.github.thetric.iliasdownloader.service.IliasService
import com.github.thetric.iliasdownloader.service.model.Course
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.PreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferences
import groovy.transform.CompileStatic
import groovy.util.logging.Log4j2
Expand All @@ -13,7 +13,7 @@ import groovy.util.logging.Log4j2
* Updates the {@link UserPreferences} and executes the sync.
*
* @see UserPreferences
* @see UserPreferenceService
* @see PreferenceService
*/
@Log4j2
@CompileStatic
Expand All @@ -23,14 +23,14 @@ final class SyncController {
private final IliasItemVisitor iliasItemVisitor

private final ResourceBundle resourceBundle
private final UserPreferenceService preferenceService
private final PreferenceService<UserPreferences> preferenceService
private final ConsoleService consoleService

SyncController(
final IliasService iliasService,
final IliasItemVisitor iliasItemVisitor,
final ResourceBundle resourceBundle,
final UserPreferenceService preferenceService,
final PreferenceService<UserPreferences> preferenceService,
final ConsoleService consoleService) {
this.iliasService = iliasService
this.iliasItemVisitor = iliasItemVisitor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.github.thetric.iliasdownloader.cli
import com.github.thetric.iliasdownloader.cli.console.ConsoleService
import com.github.thetric.iliasdownloader.service.IliasService
import com.github.thetric.iliasdownloader.service.model.Course
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.PreferenceService
import com.github.thetric.iliasdownloader.ui.common.prefs.UserPreferences
import groovy.transform.CompileStatic
import groovy.util.logging.Log4j2
Expand All @@ -19,12 +19,13 @@ final class UserPreferencesUpdateServiceImpl implements UserPreferencesUpdateSer
private final IliasService iliasService

private final ResourceBundle resourceBundle
private final UserPreferenceService preferenceService
private final PreferenceService<UserPreferences> preferenceService
private final ConsoleService consoleService

UserPreferencesUpdateServiceImpl(
final IliasService iliasService,
final ResourceBundle resourceBundle, final UserPreferenceService preferenceService, final ConsoleService consoleService) {
final ResourceBundle resourceBundle, final PreferenceService<UserPreferences> preferenceService,
final ConsoleService consoleService) {
this.iliasService = iliasService
this.resourceBundle = resourceBundle
this.preferenceService = preferenceService
Expand All @@ -33,7 +34,7 @@ final class UserPreferencesUpdateServiceImpl implements UserPreferencesUpdateSer

@Override
SyncSettings updatePreferences(final CliOptions cliOptions) {
final UserPreferences prefs = preferenceService.loadUserPreferences()
final UserPreferences prefs = preferenceService.loadPreferences()

updateFileSizeLimitFromCliOpts(prefs, cliOptions)

Expand All @@ -44,7 +45,7 @@ final class UserPreferencesUpdateServiceImpl implements UserPreferencesUpdateSer
.map { it.id as Long }
.distinct()
.collect(Collectors.<Long> toList())
preferenceService.saveUserPreferences(prefs)
preferenceService.savePreferences(prefs)
return new SyncSettings(coursesToSync, prefs.maxFileSizeInMiB)
}

Expand All @@ -68,7 +69,7 @@ final class UserPreferencesUpdateServiceImpl implements UserPreferencesUpdateSer
if (cliOptions.fileSizeLimitInMiB >= 0) {
log.debug('New max file size limit (MiB): {}, old was {}', cliOptions.fileSizeLimitInMiB, prefs.maxFileSizeInMiB)
prefs.maxFileSizeInMiB = cliOptions.fileSizeLimitInMiB
preferenceService.saveUserPreferences(prefs)
preferenceService.savePreferences(prefs)
} else {
final GString errMsg = "${resourceBundle.getString('args.sync.max-size.negative')} $cliOptions.fileSizeLimitInMiB"
throw new IllegalArgumentException(errMsg)
Expand Down

0 comments on commit a4bc762

Please sign in to comment.