feat: 集成服务端驱动 UI 演示
新增 Remote Compose 播放端与服务端生成逻辑,支持按页面拉取并渲染二进制 UI 文档。
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.ttstd.composeremote.server.controller;
|
||||
|
||||
import com.ttstd.composeremote.server.RemoteDocumentBuilderKt;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 服务端驱动 UI 接口。
|
||||
*
|
||||
* <p>一个页面一个接口:每个页面通过独立的路径暴露,例如
|
||||
* <ul>
|
||||
* <li>GET /api/remote-ui/home → 首页</li>
|
||||
* <li>GET /api/remote-ui/detail → 详情页</li>
|
||||
* </ul>
|
||||
* 返回 {@code application/octet-stream} 的 Remote Compose 二进制文档,
|
||||
* 由 Android 端 {@code RemoteComposePlayer} 原生渲染。
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/remote-ui")
|
||||
public class RemoteUIController {
|
||||
|
||||
@GetMapping(
|
||||
value = {"/{page}", ""},
|
||||
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
|
||||
)
|
||||
public byte[] getPage(@PathVariable(name = "page", required = false) String page) {
|
||||
String target = (page == null || page.isBlank()) ? "home" : page;
|
||||
return RemoteDocumentBuilderKt.buildRemoteDocument(target);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.ttstd.composeremote.server
|
||||
|
||||
import androidx.compose.remote.core.CoreDocument
|
||||
import androidx.compose.remote.creation.JvmRcPlatformServices
|
||||
import androidx.compose.remote.creation.RemoteComposeWriter
|
||||
import androidx.compose.remote.creation.dsl.RcColumnScope
|
||||
import androidx.compose.remote.creation.dsl.RcProfile
|
||||
import androidx.compose.remote.creation.dsl.RcSp
|
||||
import androidx.compose.remote.creation.dsl.createRcBuffer
|
||||
import androidx.compose.remote.creation.dsl.fillMaxSize
|
||||
import androidx.compose.remote.creation.dsl.fillMaxWidth
|
||||
import androidx.compose.remote.creation.dsl.padding
|
||||
import androidx.compose.remote.creation.dsl.background
|
||||
import androidx.compose.remote.creation.dsl.Modifier
|
||||
import androidx.compose.remote.creation.profile.Profile
|
||||
import androidx.compose.remote.creation.profile.RemoteComposeWriterFactory
|
||||
|
||||
fun buildRemoteDocument(page: String): ByteArray {
|
||||
val writerFactory = RemoteComposeWriterFactory { _, _, _ ->
|
||||
RemoteComposeWriter(1080, 1920, "remote-ui", JvmRcPlatformServices())
|
||||
}
|
||||
val profile = Profile(
|
||||
CoreDocument.DOCUMENT_API_LEVEL,
|
||||
CoreDocument.PROFILE,
|
||||
JvmRcPlatformServices(),
|
||||
writerFactory
|
||||
)
|
||||
val rcProfile = RcProfile(profile, false)
|
||||
return createRcBuffer(rcProfile) {
|
||||
Column(Modifier.fillMaxSize().background(0xFFFFFFFF.toInt()).padding(16f)) {
|
||||
Text(
|
||||
text = if (page == "home") "Home Page" else "Detail Page",
|
||||
fontSize = RcSp(36f),
|
||||
color = 0xFF0000FF.toInt() // Blue
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user