Skip to content

Commit

Permalink
build: local code.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jan 22, 2024
1 parent 269db40 commit 7bbb6e4
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 119 deletions.
11 changes: 9 additions & 2 deletions data/src/main/java/com/m3u/data/manager/ServicesModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ package com.m3u.data.manager

import android.app.NotificationManager
import android.content.Context
import android.net.wifi.WifiManager
import androidx.core.app.NotificationManagerCompat
import androidx.work.WorkManager
import com.m3u.core.architecture.TraceFileProvider
import com.m3u.core.architecture.logger.Logger
import com.m3u.data.manager.impl.MessageManagerImpl
import com.m3u.data.manager.impl.PlayerManagerImpl
import com.m3u.data.manager.impl.TraceFileProviderImpl
import com.m3u.data.repository.logger.CommonLogger
import com.m3u.data.repository.logger.MessageLogger
import com.m3u.data.manager.impl.PlayerManagerImpl
import com.m3u.data.manager.impl.MessageManagerImpl
import dagger.Binds
import dagger.Module
import dagger.Provides
Expand Down Expand Up @@ -66,4 +67,10 @@ object ProvidedServicesModule {
fun provideNotificationManager(@ApplicationContext context: Context): NotificationManager {
return context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}

@Provides
@Singleton
fun provideWifiManager(@ApplicationContext context: Context): WifiManager {
return context.getSystemService(Context.WIFI_SERVICE) as WifiManager
}
}
83 changes: 83 additions & 0 deletions data/src/main/java/com/m3u/data/net/udp/Broadcast.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.m3u.data.net.udp

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.flowOn
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress
import java.nio.charset.Charset

interface Broadcast {
fun send(bytes: ByteArray)
fun receive(): Flow<ByteArray>
}

data class LocalCode(
val host: String,
val port: Int,
val code: Int,
val expiration: Long
) {
companion object {
fun encode(code: LocalCode): ByteArray =
"#M3U_LOCAL_CODE,${code.host},${code.port},${code.code},${code.expiration}#"
.toByteArray(Charsets.UTF_8)

fun decode(bytes: ByteArray): LocalCode {
val line = bytes
.toString(Charset.defaultCharset())
.run {
take(indexOf('#', 1))
}
check(line.startsWith("#M3U_LOCAL_CODE", true)) {
"Invalidate bytes: $line"
}
val elements = line.split(",")
return LocalCode(
host = elements[1],
port = elements[2].toInt(),
code = elements[3].toInt(),
expiration = elements[4].toLong()
)
}
}
}

object LocalCodeBroadcast : Broadcast {
override fun send(bytes: ByteArray) {
val socket = DatagramSocket()
socket.broadcast = true
val packet = DatagramPacket(bytes, bytes.size, InetAddress.getByName("0.0.0.0"), PORT)
try {
socket.send(packet)
} catch (ignore: Exception) {
}
}

override fun receive(): Flow<ByteArray> = channelFlow {
val socket = DatagramSocket(PORT, InetAddress.getByName("0.0.0.0"))
socket.broadcast = true
awaitClose {
try {
socket.close()
} catch (ignore: Exception) {
}
}
while (true) {
val bytes = ByteArray(65535)
val packet = DatagramPacket(bytes, bytes.size)
try {
socket.receive(packet)
send(packet.data)
} catch (ignore: Exception) {
}
}
}
.flowOn(Dispatchers.IO)


private const val PORT = 56981
}
48 changes: 0 additions & 48 deletions data/src/main/java/com/m3u/data/net/udp/UdpPacket.kt

This file was deleted.

34 changes: 0 additions & 34 deletions data/src/main/java/com/m3u/data/net/udp/UdpServer.kt

This file was deleted.

26 changes: 0 additions & 26 deletions data/src/main/java/com/m3u/data/net/udp/action/Action.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.m3u.features.foryou

import android.content.res.Configuration.ORIENTATION_PORTRAIT
import android.util.Log
import android.view.KeyEvent
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Arrangement
Expand All @@ -18,10 +19,12 @@ import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.Link
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
Expand Down Expand Up @@ -54,6 +57,8 @@ import com.m3u.ui.helper.Action
import com.m3u.ui.helper.LocalHelper
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

@Composable
fun ForyouRoute(
Expand All @@ -71,13 +76,42 @@ fun ForyouRoute(
val helper = LocalHelper.current
val pref = LocalPref.current

// temp
var loading by remember { mutableStateOf(false) }
var code by remember { mutableStateOf("") }

val tv = isTelevision()

val sheetState = rememberModalBottomSheetState { !loading }

val details by viewModel.details.collectAsStateWithLifecycle()
val recommend by viewModel.recommend.collectAsStateWithLifecycle()

val currentLocalCode by viewModel.currentLocalCode.collectAsStateWithLifecycle()
val localCodes by viewModel.localCodes.collectAsStateWithLifecycle()

var isConnectSheetVisible by remember { mutableStateOf(false) }

LaunchedEffect(Unit) {
snapshotFlow { sheetState.isVisible }
.onEach { visible ->
if (visible) {
viewModel.searchLocalCodes()
} else {
viewModel.stopSearchLocalCodes()
}
}
.launchIn(this)
}

LaunchedEffect(Unit) {
snapshotFlow { localCodes }
.onEach {
Log.e("TAG", "$it")
}
.launchIn(this)
}

EventHandler(resume, title) {
helper.deep = 0
helper.title = title.title()
Expand Down Expand Up @@ -120,14 +154,12 @@ fun ForyouRoute(
}
.then(modifier)
)
var loading by remember { mutableStateOf(false) }
var code by remember { mutableStateOf("") }
ConnectBottomSheet(
visible = isConnectSheetVisible,
code = code,
loading = loading,
onCode = { code = it },
sheetState = rememberModalBottomSheetState { !loading },
sheetState = sheetState,
onDismissRequest = { isConnectSheetVisible = false },
onConnect = { loading = true }
)
Expand Down
Loading

0 comments on commit 7bbb6e4

Please sign in to comment.