mirror of
https://github.com/clangen/musikcube.git
synced 2025-01-02 11:58:27 +00:00
Added a super simple, bare-bones component system. We'll use this to
swap functionality at runtime in the near future. Or not, and we'll end up removing it.
This commit is contained in:
parent
4edd5476a3
commit
b950ae0259
@ -0,0 +1,39 @@
|
||||
package io.casey.musikcube.remote.framework.components
|
||||
|
||||
import android.os.Bundle
|
||||
|
||||
abstract class ComponentBase: IComponent {
|
||||
enum class State {
|
||||
Unknown, Created, Started, Resumed, Paused, Stopped, Destroyed
|
||||
}
|
||||
|
||||
protected var state = State.Unknown
|
||||
private set
|
||||
|
||||
override fun onCreate(bundle: Bundle) {
|
||||
state = State.Created
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
state = State.Started
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
state = State.Resumed
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
state = State.Paused
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
state = State.Stopped
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(bundle: Bundle) {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
state = State.Destroyed
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package io.casey.musikcube.remote.framework.components
|
||||
|
||||
import android.os.Bundle
|
||||
|
||||
class ComponentSet : ComponentBase() {
|
||||
private val components: MutableMap<Class<out IComponent>, IComponent> = mutableMapOf()
|
||||
private var bundle = Bundle()
|
||||
|
||||
fun add(component: IComponent) {
|
||||
components.put(component.javaClass, component)
|
||||
|
||||
when (state) {
|
||||
State.Created ->
|
||||
component.onCreate(bundle)
|
||||
State.Started -> {
|
||||
component.onCreate(bundle)
|
||||
component.onStart()
|
||||
}
|
||||
State.Resumed -> {
|
||||
component.onCreate(bundle)
|
||||
component.onStart()
|
||||
component.onResume()
|
||||
}
|
||||
State.Paused -> {
|
||||
component.onCreate(bundle)
|
||||
component.onStart()
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> get(cls: Class<out IComponent>): T? = components.get(cls) as T
|
||||
|
||||
override fun onCreate(bundle: Bundle) {
|
||||
super.onCreate(bundle)
|
||||
this.bundle = bundle
|
||||
components.values.forEach { it.onCreate(bundle) }
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
components.values.forEach { it.onStart() }
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
components.values.forEach { it.onResume() }
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
components.values.forEach { it.onPause() }
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
components.values.forEach { it.onStop() }
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(bundle: Bundle) {
|
||||
super.onSaveInstanceState(bundle)
|
||||
components.values.forEach { it.onSaveInstanceState(bundle) }
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
components.values.forEach { it.onDestroy() }
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package io.casey.musikcube.remote.framework.components
|
||||
|
||||
import android.os.Bundle
|
||||
|
||||
interface IComponent {
|
||||
fun onCreate(bundle: Bundle)
|
||||
fun onStart()
|
||||
fun onResume()
|
||||
fun onPause()
|
||||
fun onStop()
|
||||
fun onSaveInstanceState(bundle: Bundle)
|
||||
fun onDestroy()
|
||||
}
|
@ -11,6 +11,8 @@ import com.uacf.taskrunner.LifecycleDelegate
|
||||
import com.uacf.taskrunner.Runner
|
||||
import com.uacf.taskrunner.Task
|
||||
import io.casey.musikcube.remote.Application
|
||||
import io.casey.musikcube.remote.framework.components.ComponentSet
|
||||
import io.casey.musikcube.remote.framework.components.IComponent
|
||||
import io.casey.musikcube.remote.service.websocket.model.IDataProvider
|
||||
import io.casey.musikcube.remote.injection.*
|
||||
import io.casey.musikcube.remote.service.playback.IPlaybackService
|
||||
@ -26,6 +28,7 @@ abstract class BaseActivity : AppCompatActivity(), Runner.TaskCallbacks {
|
||||
private lateinit var runnerDelegate: LifecycleDelegate
|
||||
private lateinit var prefs: SharedPreferences
|
||||
private var paused = false
|
||||
private val components = ComponentSet()
|
||||
@Inject lateinit var wss: WebSocketService
|
||||
@Inject lateinit var dataProvider: IDataProvider
|
||||
|
||||
@ -40,6 +43,7 @@ abstract class BaseActivity : AppCompatActivity(), Runner.TaskCallbacks {
|
||||
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
components.onCreate(savedInstanceState ?: Bundle())
|
||||
volumeControlStream = AudioManager.STREAM_MUSIC
|
||||
runnerDelegate = LifecycleDelegate(this, this, javaClass, null)
|
||||
runnerDelegate.onCreate(savedInstanceState)
|
||||
@ -47,11 +51,34 @@ abstract class BaseActivity : AppCompatActivity(), Runner.TaskCallbacks {
|
||||
prefs = getSharedPreferences(Prefs.NAME, Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
components.onStart()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
components.onResume()
|
||||
dataProvider.attach()
|
||||
runnerDelegate.onResume()
|
||||
|
||||
playbackService = PlaybackServiceFactory.instance(this)
|
||||
|
||||
val playbackListener = playbackServiceEventListener
|
||||
if (playbackListener != null) {
|
||||
this.playbackService?.connect(playbackServiceEventListener!!)
|
||||
}
|
||||
|
||||
paused = false
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
hideKeyboard()
|
||||
|
||||
super.onPause()
|
||||
|
||||
components.onPause()
|
||||
dataProvider.detach()
|
||||
runnerDelegate.onPause()
|
||||
|
||||
@ -66,37 +93,24 @@ abstract class BaseActivity : AppCompatActivity(), Runner.TaskCallbacks {
|
||||
paused = true
|
||||
}
|
||||
|
||||
protected fun isPaused(): Boolean {
|
||||
return paused
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
dataProvider.attach()
|
||||
runnerDelegate.onResume()
|
||||
|
||||
playbackService = PlaybackServiceFactory.instance(this)
|
||||
|
||||
val playbackListener = playbackServiceEventListener
|
||||
if (playbackListener != null) {
|
||||
this.playbackService?.connect(playbackServiceEventListener!!)
|
||||
}
|
||||
|
||||
paused = false
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
runnerDelegate.onDestroy()
|
||||
dataProvider.destroy()
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
components.onStop()
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
components.onSaveInstanceState(outState)
|
||||
runnerDelegate.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
components.onDestroy()
|
||||
runnerDelegate.onDestroy()
|
||||
dataProvider.destroy()
|
||||
}
|
||||
|
||||
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
|
||||
val streaming = prefs.getBoolean(
|
||||
Prefs.Key.STREAMING_PLAYBACK, Prefs.Default.STREAMING_PLAYBACK)
|
||||
@ -133,6 +147,10 @@ abstract class BaseActivity : AppCompatActivity(), Runner.TaskCallbacks {
|
||||
|
||||
}
|
||||
|
||||
protected fun isPaused(): Boolean = paused
|
||||
protected fun component(component: IComponent) = components.add(component)
|
||||
protected fun <T> component(cls: Class<out IComponent>): T? = components.get(cls)
|
||||
|
||||
protected val socketService: WebSocketService get() = wss
|
||||
|
||||
protected var playbackService: IPlaybackService? = null
|
||||
|
Loading…
Reference in New Issue
Block a user