Skip to content

Commit

Permalink
build: code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed May 8, 2024
1 parent c204ce1 commit 141b4f2
Show file tree
Hide file tree
Showing 51 changed files with 445 additions and 650 deletions.
52 changes: 5 additions & 47 deletions androidApp/src/main/java/com/m3u/androidApp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,22 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.m3u.androidApp.ui.App
import com.m3u.androidApp.ui.AppViewModel
import com.m3u.core.architecture.dispatcher.Dispatcher
import com.m3u.core.architecture.dispatcher.M3uDispatchers.Main
import com.m3u.core.architecture.preferences.Preferences
import com.m3u.data.service.Messager
import com.m3u.data.service.PlayerManager
import com.m3u.data.service.RemoteDirectionService
import com.m3u.ui.EventBus.registerActionEventCollector
import com.m3u.ui.Events.connectDPadIntent
import com.m3u.ui.Toolkit
import com.m3u.ui.helper.AbstractHelper
import com.m3u.ui.helper.Helper
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private val viewModel: AppViewModel by viewModels()
private val helper by lazy {
AbstractHelper(
activity = this,
mainDispatcher = mainDispatcher,
playerManager = playerManager,
messager = messager,
title = viewModel.title,
message = viewModel.message,
actions = viewModel.actions,
fob = viewModel.fob
)
}

@Inject
lateinit var preferences: Preferences

@Inject
lateinit var playerManager: PlayerManager

@Inject
@Dispatcher(Main)
lateinit var mainDispatcher: CoroutineDispatcher

@Inject
lateinit var remoteDirectionService: RemoteDirectionService

@Inject
lateinit var messager: Messager
private val helper: Helper = Helper(this)

override fun onResume() {
super.onResume()
helper.applyConfiguration()
}

override fun onUserLeaveHint() {
super.onUserLeaveHint()
helper.onUserLeaveHint?.invoke()
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
helper.applyConfiguration()
Expand All @@ -72,17 +33,14 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
enableEdgeToEdge()
connectDPadIntent()
super.onCreate(savedInstanceState)
setContent {
Toolkit(
helper = helper,
preferences = preferences,
) {
Toolkit(helper) {
App(
viewModel = viewModel
)
}
}
registerActionEventCollector(remoteDirectionService.actions)
}
}
20 changes: 2 additions & 18 deletions androidApp/src/main/java/com/m3u/androidApp/ui/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.m3u.androidApp.ui.sheet.RemoteControlSheet
import com.m3u.androidApp.ui.sheet.RemoteControlSheetValue
import com.m3u.core.architecture.preferences.LocalPreferences
import com.m3u.core.architecture.preferences.hiltPreferences
import com.m3u.data.television.model.RemoteDirection
import com.m3u.material.components.Icon
import com.m3u.material.ktx.isTelevision
Expand All @@ -40,9 +40,6 @@ import com.m3u.ui.Destination
import com.m3u.ui.FontFamilies
import com.m3u.ui.LocalNavController
import com.m3u.ui.SnackHost
import com.m3u.ui.helper.Action
import com.m3u.ui.helper.Fob


@Composable
fun App(
Expand All @@ -53,10 +50,6 @@ fun App(
LocalOnBackPressedDispatcherOwner.current
).onBackPressedDispatcher

val title: String by viewModel.title
val actions by viewModel.actions
val fob by viewModel.fob

val navController = rememberNavController()
val entry by navController.currentBackStackEntryAsState()

Expand Down Expand Up @@ -89,9 +82,6 @@ fun App(
LocalNavController provides navController
) {
AppImpl(
title = title,
actions = actions,
fob = fob,
rootDestination = viewModel.rootDestination,
onBackPressed = onBackPressed.takeUnless { shouldDispatchBackStack },
navigateToRoot = navigateToRootDestination,
Expand All @@ -114,9 +104,6 @@ fun App(

@Composable
private fun AppImpl(
title: String,
actions: List<Action>,
fob: Fob?,
rootDestination: Destination.Root,
isRemoteControlSheetVisible: Boolean,
remoteControlSheetValue: RemoteControlSheetValue,
Expand All @@ -132,14 +119,11 @@ private fun AppImpl(
modifier: Modifier = Modifier
) {
val spacing = LocalSpacing.current
val preferences = LocalPreferences.current
val preferences = hiltPreferences()

val tv = isTelevision()

AppScaffold(
title = title,
actions = actions,
fob = fob,
rootDestination = rootDestination,
onBackPressed = onBackPressed,
navigateToRoot = navigateToRoot,
Expand Down
10 changes: 5 additions & 5 deletions androidApp/src/main/java/com/m3u/androidApp/ui/AppNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.compose.NavHost
import com.m3u.core.architecture.preferences.LocalPreferences
import com.m3u.core.architecture.preferences.hiltPreferences
import com.m3u.core.wrapper.eventOf
import com.m3u.features.playlist.configuration.playlistConfigurationScreen
import com.m3u.features.playlist.configuration.navigateToPlaylistConfiguration
Expand All @@ -21,9 +21,9 @@ import com.m3u.features.playlist.navigation.playlistTvScreen
import com.m3u.features.stream.PlayerActivity
import com.m3u.material.ktx.isTelevision
import com.m3u.ui.Destination
import com.m3u.ui.EventBus
import com.m3u.ui.Events
import com.m3u.ui.LocalNavController
import com.m3u.ui.SettingFragment
import com.m3u.ui.SettingDestination

@Composable
fun AppNavHost(
Expand All @@ -34,7 +34,7 @@ fun AppNavHost(
startDestination: String = ROOT_ROUTE
) {
val context = LocalContext.current
val preferences = LocalPreferences.current
val preferences = hiltPreferences()
val navController = LocalNavController.current

val tv = isTelevision()
Expand Down Expand Up @@ -83,7 +83,7 @@ fun AppNavHost(
},
navigateToSettingPlaylistManagement = {
navigateToRoot(Destination.Root.Setting)
EventBus.settingFragment = eventOf(SettingFragment.Playlists)
Events.settingDestination = eventOf(SettingDestination.Playlists)
},
navigateToPlaylistConfiguration = {
navController.navigateToPlaylistConfiguration(it.url)
Expand Down
8 changes: 5 additions & 3 deletions androidApp/src/main/java/com/m3u/androidApp/ui/AppScaffold.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import com.m3u.ui.FontFamilies
import com.m3u.ui.helper.Action
import com.m3u.ui.helper.Fob
import com.m3u.ui.helper.LocalHelper
import com.m3u.ui.helper.Metadata
import com.m3u.ui.helper.useRailNav
import dev.chrisbanes.haze.HazeState
import dev.chrisbanes.haze.HazeStyle
Expand All @@ -59,10 +60,7 @@ import dev.chrisbanes.haze.hazeChild
@Composable
@OptIn(InternalComposeApi::class)
internal fun AppScaffold(
title: String,
actions: List<Action>,
rootDestination: Destination.Root?,
fob: Fob?,
navigateToRoot: (Destination.Root) -> Unit,
modifier: Modifier = Modifier,
onBackPressed: (() -> Unit)? = null,
Expand All @@ -72,6 +70,10 @@ internal fun AppScaffold(
val useRailNav = LocalHelper.current.useRailNav
val tv = isTelevision()

val title = Metadata.title
val fob = Metadata.fob
val actions = Metadata.actions

val hazeState = remember { HazeState() }

CompositionLocalProvider(LocalHazeState provides hazeState) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.m3u.androidApp.ui

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand All @@ -18,8 +17,6 @@ import com.m3u.data.repository.television.TelevisionRepository
import com.m3u.data.service.Messager
import com.m3u.data.television.model.RemoteDirection
import com.m3u.ui.Destination
import com.m3u.ui.helper.Action
import com.m3u.ui.helper.Fob
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -157,9 +154,6 @@ class AppViewModel @Inject constructor(
}
)

val title = mutableStateOf("")
val actions: MutableState<List<Action>> = mutableStateOf(emptyList())
val fob = mutableStateOf<Fob?>(null)
var code by mutableStateOf("")
var isConnectSheetVisible by mutableStateOf(false)
val message = messager.message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.m3u.data.service.collectMessageAsState
import com.m3u.data.television.model.RemoteDirection
import com.m3u.data.television.model.Television
import com.m3u.material.model.LocalSpacing
import com.m3u.ui.FontFamilies
import com.m3u.ui.helper.LocalHelper

@Composable
@InternalComposeApi
Expand All @@ -31,8 +30,7 @@ internal fun DPadContent(
modifier: Modifier = Modifier
) {
val spacing = LocalSpacing.current
val helper = LocalHelper.current
val message by helper.message.collectAsStateWithLifecycle()
val message by collectMessageAsState()
Column(
modifier = modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.m3u.androidApp.ui.CodeRow
import com.m3u.core.util.basic.title
import com.m3u.core.wrapper.Message
import com.m3u.data.service.collectMessageAsState
import com.m3u.i18n.R
import com.m3u.ui.helper.LocalHelper

@Composable
@InternalComposeApi
Expand All @@ -42,8 +41,7 @@ internal fun PrepareContent(
onCode: (String) -> Unit,
modifier: Modifier = Modifier
) {
val helper = LocalHelper.current
val message by helper.message.collectAsStateWithLifecycle()
val message by collectMessageAsState()

val title = stringResource(R.string.feat_foryou_connect_title).title()
val subtitle = if (message.level == Message.LEVEL_EMPTY) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package com.m3u.core.architecture.preferences

import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent

val LocalPreferences = compositionLocalOf<Preferences> { error("Please provide pref.") }
@Composable
fun hiltPreferences(): Preferences {
val context = LocalContext.current
return remember {
val applicationContext = context.applicationContext ?: throw IllegalStateException()
EntryPointAccessors
.fromApplication<PreferencesEntryPoint>(applicationContext)
.preferences
}
}

@EntryPoint
@InstallIn(SingletonComponent::class)
private interface PreferencesEntryPoint {
val preferences: Preferences
}
28 changes: 28 additions & 0 deletions data/src/main/java/com/m3u/data/service/Messager.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
package com.m3u.data.service

import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.m3u.core.wrapper.Message
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.flow.StateFlow

interface Messager {
fun emit(message: Message)
fun emit(message: String)
val message: StateFlow<Message>
}

@EntryPoint
@InstallIn(SingletonComponent::class)
private interface MessagerEntryPoint {
val messager: Messager
}

@Composable
fun collectMessageAsState(): State<Message> {
val context = LocalContext.current
return remember {
val applicationContext = context.applicationContext ?: throw IllegalStateException()
EntryPointAccessors
.fromApplication<MessagerEntryPoint>(applicationContext)
.messager
.message
}
.collectAsState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import kotlinx.coroutines.flow.SharedFlow

@Immutable
interface RemoteDirectionService {
val actions: SharedFlow<Action>
val incoming: SharedFlow<RemoteDirection>
fun emit(remoteDirection: RemoteDirection)
sealed class Action {
data object Back : Action()
data class Common(val keyCode: Int) : Action()
}
}
Loading

0 comments on commit 141b4f2

Please sign in to comment.