Component: Refactor profile
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package io.neoterm.component.colorscheme
|
||||
|
||||
import android.content.Context
|
||||
import io.neolang.visitor.ConfigVisitor
|
||||
import io.neoterm.App
|
||||
import io.neoterm.R
|
||||
import io.neoterm.component.codegen.CodeGenComponent
|
||||
@ -49,7 +50,7 @@ class ColorSchemeComponent : ConfigFileBasedComponent<NeoColorScheme>() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateComponentObject(): NeoColorScheme {
|
||||
override fun onCreateComponentObject(configVisitor: ConfigVisitor): NeoColorScheme {
|
||||
return NeoColorScheme()
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package io.neoterm.component.extrakey
|
||||
|
||||
import android.content.Context
|
||||
import io.neolang.visitor.ConfigVisitor
|
||||
import io.neoterm.App
|
||||
import io.neoterm.frontend.component.helper.ConfigFileBasedComponent
|
||||
import io.neoterm.frontend.config.NeoTermPath
|
||||
@ -34,7 +35,7 @@ class ExtraKeyComponent : ConfigFileBasedComponent<NeoExtraKey>() {
|
||||
reloadExtraKeyConfig()
|
||||
}
|
||||
|
||||
override fun onCreateComponentObject(): NeoExtraKey {
|
||||
override fun onCreateComponentObject(configVisitor: ConfigVisitor): NeoExtraKey {
|
||||
return NeoExtraKey()
|
||||
}
|
||||
|
||||
|
@ -1,19 +1,17 @@
|
||||
package io.neoterm.component.profile
|
||||
|
||||
import io.neolang.visitor.ConfigVisitor
|
||||
import io.neoterm.component.codegen.CodeGenParameter
|
||||
import io.neoterm.component.codegen.generator.ICodeGenerator
|
||||
import io.neoterm.component.codegen.impl.NeoProfileGenerator
|
||||
import io.neoterm.component.codegen.model.CodeGenObject
|
||||
import io.neoterm.frontend.component.helper.ConfigFileBasedObject
|
||||
|
||||
/**
|
||||
* @author kiva
|
||||
*/
|
||||
abstract class NeoProfile : CodeGenObject {
|
||||
abstract class NeoProfile : CodeGenObject, ConfigFileBasedObject {
|
||||
abstract val profileMetaName: String
|
||||
|
||||
abstract fun onProfileLoaded(visitor: ConfigVisitor): Boolean
|
||||
|
||||
override fun getCodeGenerator(parameter: CodeGenParameter): ICodeGenerator {
|
||||
return NeoProfileGenerator(parameter)
|
||||
}
|
||||
|
@ -1,17 +1,37 @@
|
||||
package io.neoterm.component.profile
|
||||
|
||||
import io.neolang.visitor.ConfigVisitor
|
||||
import io.neoterm.component.config.ConfigureComponent
|
||||
import io.neoterm.frontend.component.ComponentManager
|
||||
import io.neoterm.frontend.component.NeoComponent
|
||||
import io.neoterm.frontend.component.helper.ConfigFileBasedComponent
|
||||
import io.neoterm.frontend.config.NeoConfigureFile
|
||||
import io.neoterm.frontend.config.NeoTermPath
|
||||
import io.neoterm.frontend.logging.NLog
|
||||
import org.jetbrains.annotations.TestOnly
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* @author kiva
|
||||
*/
|
||||
class ProfileComponent : NeoComponent {
|
||||
class ProfileComponent : NeoComponent, ConfigFileBasedComponent<NeoProfile>() {
|
||||
override fun onCheckComponentFiles() {
|
||||
}
|
||||
|
||||
override fun onCreateComponentObject(configVisitor: ConfigVisitor): NeoProfile {
|
||||
val rootContext = configVisitor.getRootContext()
|
||||
|
||||
val profileClass = rootContext.children
|
||||
.mapNotNull { profileRegistry[it.contextName] }
|
||||
.singleOrNull()
|
||||
|
||||
if (profileClass != null) {
|
||||
return profileClass.newInstance()
|
||||
}
|
||||
|
||||
throw IllegalArgumentException("No proper profile registry for found")
|
||||
}
|
||||
|
||||
private val profileRegistry = mutableMapOf<String, Class<out NeoProfile>>()
|
||||
private val profileList = mutableListOf<NeoProfile>()
|
||||
|
||||
@ -23,41 +43,6 @@ class ProfileComponent : NeoComponent {
|
||||
profileRegistry.remove(metaName)
|
||||
}
|
||||
|
||||
inline fun <reified T : NeoProfile> loadConfigure(file: File): T {
|
||||
return loadConfigure(file, T::class.java) as T
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun loadConfigure(file: File, clazz: Class<out NeoProfile>): NeoProfile {
|
||||
val loaderService = ComponentManager.getComponent<ConfigureComponent>()
|
||||
|
||||
val configure: NeoConfigureFile?
|
||||
try {
|
||||
configure = loaderService.newLoader(file).loadConfigure()
|
||||
if (configure == null) {
|
||||
throw RuntimeException("Parse configuration failed.")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
NLog.e("Profile", "Failed to load profile: ${file.absolutePath}: ${e.localizedMessage}")
|
||||
throw e
|
||||
}
|
||||
|
||||
val visitor = configure.getVisitor()
|
||||
val rootContext = visitor.getRootContext()
|
||||
|
||||
val profileClass = rootContext.children
|
||||
.mapNotNull { profileRegistry[it.contextName] }
|
||||
.singleOrNull()
|
||||
|
||||
if (profileClass != null) {
|
||||
val profile = profileClass.newInstance()
|
||||
profile.onProfileLoaded(visitor)
|
||||
return profile
|
||||
}
|
||||
|
||||
throw IllegalArgumentException("No profile registry for ${clazz.simpleName} found")
|
||||
}
|
||||
|
||||
override fun onServiceInit() {
|
||||
checkForFiles()
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package io.neoterm.frontend.component.helper
|
||||
|
||||
import io.neolang.visitor.ConfigVisitor
|
||||
import io.neoterm.component.config.ConfigureComponent
|
||||
import io.neoterm.frontend.component.ComponentManager
|
||||
import io.neoterm.frontend.component.NeoComponent
|
||||
@ -35,8 +36,9 @@ abstract class ConfigFileBasedComponent<out T : ConfigFileBasedObject> : NeoComp
|
||||
val configure = loaderService.newLoader(file).loadConfigure()
|
||||
?: throw RuntimeException("Parse configuration failed.")
|
||||
|
||||
val componentObject = onCreateComponentObject()
|
||||
componentObject.onConfigLoaded(configure.getVisitor())
|
||||
val configVisitor = configure.getVisitor()
|
||||
val componentObject = onCreateComponentObject(configVisitor)
|
||||
componentObject.onConfigLoaded(configVisitor)
|
||||
componentObject
|
||||
} catch (e: RuntimeException) {
|
||||
NLog.e(TAG, "Failed to load config: ${file.absolutePath}: ${e.localizedMessage}")
|
||||
@ -46,5 +48,5 @@ abstract class ConfigFileBasedComponent<out T : ConfigFileBasedObject> : NeoComp
|
||||
|
||||
abstract fun onCheckComponentFiles()
|
||||
|
||||
abstract fun onCreateComponentObject(): T
|
||||
abstract fun onCreateComponentObject(configVisitor: ConfigVisitor): T
|
||||
}
|
@ -67,20 +67,19 @@ class ShellProfile : NeoProfile() {
|
||||
enableWordBasedIme = NeoPreference.isWordBasedImeEnabled()
|
||||
}
|
||||
|
||||
override fun onProfileLoaded(visitor: ConfigVisitor): Boolean {
|
||||
loginShell = visitor.getProfileString(LOGIN_SHELL, loginShell)
|
||||
initialCommand = visitor.getProfileString(INITIAL_COMMAND, initialCommand)
|
||||
enableBell = visitor.getProfileBoolean(BELL, enableBell)
|
||||
enableVibrate = visitor.getProfileBoolean(VIBRATE, enableVibrate)
|
||||
enableExecveWrapper = visitor.getProfileBoolean(EXECVE_WRAPPER, enableExecveWrapper)
|
||||
enableSpecialVolumeKeys = visitor.getProfileBoolean(SPECIAL_VOLUME_KEYS, enableSpecialVolumeKeys)
|
||||
enableAutoCompletion = visitor.getProfileBoolean(AUTO_COMPLETION, enableAutoCompletion)
|
||||
enableBackKeyToEscape = visitor.getProfileBoolean(BACK_KEY_TO_ESC, enableBackKeyToEscape)
|
||||
enableExtraKeys = visitor.getProfileBoolean(EXTRA_KEYS, enableExtraKeys)
|
||||
enableWordBasedIme = visitor.getProfileBoolean(WORD_BASED_IME, enableWordBasedIme)
|
||||
profileFont = visitor.getProfileString(FONT, profileFont)
|
||||
profileColorScheme = visitor.getProfileString(COLOR_SCHEME, profileColorScheme)
|
||||
return true
|
||||
override fun onConfigLoaded(configVisitor: ConfigVisitor) {
|
||||
loginShell = configVisitor.getProfileString(LOGIN_SHELL, loginShell)
|
||||
initialCommand = configVisitor.getProfileString(INITIAL_COMMAND, initialCommand)
|
||||
enableBell = configVisitor.getProfileBoolean(BELL, enableBell)
|
||||
enableVibrate = configVisitor.getProfileBoolean(VIBRATE, enableVibrate)
|
||||
enableExecveWrapper = configVisitor.getProfileBoolean(EXECVE_WRAPPER, enableExecveWrapper)
|
||||
enableSpecialVolumeKeys = configVisitor.getProfileBoolean(SPECIAL_VOLUME_KEYS, enableSpecialVolumeKeys)
|
||||
enableAutoCompletion = configVisitor.getProfileBoolean(AUTO_COMPLETION, enableAutoCompletion)
|
||||
enableBackKeyToEscape = configVisitor.getProfileBoolean(BACK_KEY_TO_ESC, enableBackKeyToEscape)
|
||||
enableExtraKeys = configVisitor.getProfileBoolean(EXTRA_KEYS, enableExtraKeys)
|
||||
enableWordBasedIme = configVisitor.getProfileBoolean(WORD_BASED_IME, enableWordBasedIme)
|
||||
profileFont = configVisitor.getProfileString(FONT, profileFont)
|
||||
profileColorScheme = configVisitor.getProfileString(COLOR_SCHEME, profileColorScheme)
|
||||
}
|
||||
|
||||
private fun ConfigVisitor.getProfileString(key: String, fallback: String): String {
|
||||
|
Reference in New Issue
Block a user