From ca99eaa3e4721d809371cde7b7149ecd3b8a8b09 Mon Sep 17 00:00:00 2001 From: yangxisong Date: Thu, 13 Nov 2025 16:04:13 +0800 Subject: [PATCH] feat: toast --- .../java/com/yuanxuan/rokid/MainActivity.kt | 37 +------------------ .../rokid/dependencies/AppDependencies.kt | 5 +++ .../ApplicationDependencyProvider.kt | 8 ++++ .../com/yuanxuan/rokid/toast/ToastManager.kt | 37 +++++++++++++++++++ .../com/yuanxuan/rokid/toast/ToastUtils.kt | 19 ++++++++++ 5 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/com/yuanxuan/rokid/toast/ToastManager.kt create mode 100644 app/src/main/java/com/yuanxuan/rokid/toast/ToastUtils.kt diff --git a/app/src/main/java/com/yuanxuan/rokid/MainActivity.kt b/app/src/main/java/com/yuanxuan/rokid/MainActivity.kt index 8954d56..53c96bf 100644 --- a/app/src/main/java/com/yuanxuan/rokid/MainActivity.kt +++ b/app/src/main/java/com/yuanxuan/rokid/MainActivity.kt @@ -6,21 +6,6 @@ import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat -import androidx.lifecycle.lifecycleScope -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken -import com.yuanxuan.rokid.network.http.ApiResponse -import com.yuanxuan.rokid.network.http.Test -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import kotlinx.coroutines.suspendCancellableCoroutine -import kotlinx.coroutines.withContext -import okhttp3.OkHttpClient -import okhttp3.Request -import timber.log.Timber -import java.lang.reflect.Type class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -32,26 +17,8 @@ class MainActivity : AppCompatActivity() { v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } - val json = "{\n" + - " \"code\": 0,\n" + - " \"msg\": \"success\",\n" + - " \"data\": {\n" + - " \"userId\": \"123\",\n" + - " \"name\": \"Alice\"\n" + - " }\n" + - "}" - - val x = test(json) - -// onBackPressedDispatcher.addCallback { -// } - } - - private inline fun test(responseBody: String): T { - val typeToken = object : TypeToken>() {} - val typeOfT: Type = typeToken.type - val apiResponse: ApiResponse = Gson().fromJson(responseBody, typeOfT) - return apiResponse.data + onBackPressedDispatcher.addCallback { + } } } \ No newline at end of file diff --git a/app/src/main/java/com/yuanxuan/rokid/dependencies/AppDependencies.kt b/app/src/main/java/com/yuanxuan/rokid/dependencies/AppDependencies.kt index 7995e55..adbf251 100644 --- a/app/src/main/java/com/yuanxuan/rokid/dependencies/AppDependencies.kt +++ b/app/src/main/java/com/yuanxuan/rokid/dependencies/AppDependencies.kt @@ -6,6 +6,7 @@ import com.yuanxuan.rokid.network.http.ApiRepository import com.yuanxuan.rokid.network.http.ApiService import com.yuanxuan.rokid.network.http.RetrofitClient import com.yuanxuan.rokid.network.websocket.WebSocketManager +import com.yuanxuan.rokid.toast.ToastManager /** * 项目的服务管理器,所有依赖集中管理 @@ -44,12 +45,16 @@ object AppDependencies { apiService = retrofitClient.apiService ) } + val toastManager by lazy { + provider.provideToastManager() + } interface Provider { fun provideDeviceServiceManager(): DeviceServiceManager fun provideWebSocketManager(): WebSocketManager fun provideRetrofitClient(): RetrofitClient fun provideApiRepository(apiService: ApiService): ApiRepository + fun provideToastManager(): ToastManager } } \ No newline at end of file diff --git a/app/src/main/java/com/yuanxuan/rokid/dependencies/ApplicationDependencyProvider.kt b/app/src/main/java/com/yuanxuan/rokid/dependencies/ApplicationDependencyProvider.kt index 71c0a85..3e4c5ef 100644 --- a/app/src/main/java/com/yuanxuan/rokid/dependencies/ApplicationDependencyProvider.kt +++ b/app/src/main/java/com/yuanxuan/rokid/dependencies/ApplicationDependencyProvider.kt @@ -6,6 +6,7 @@ import com.yuanxuan.rokid.network.http.ApiRepository import com.yuanxuan.rokid.network.http.ApiService import com.yuanxuan.rokid.network.http.RetrofitClient import com.yuanxuan.rokid.network.websocket.WebSocketManager +import com.yuanxuan.rokid.toast.ToastManager import kotlinx.coroutines.CoroutineScope class ApplicationDependencyProvider(val context: Application, val scope: CoroutineScope) : @@ -31,4 +32,11 @@ class ApplicationDependencyProvider(val context: Application, val scope: Corouti ) } + override fun provideToastManager(): ToastManager { + return ToastManager( + context = context, + scope = scope + ) + } + } \ No newline at end of file diff --git a/app/src/main/java/com/yuanxuan/rokid/toast/ToastManager.kt b/app/src/main/java/com/yuanxuan/rokid/toast/ToastManager.kt new file mode 100644 index 0000000..8504ec2 --- /dev/null +++ b/app/src/main/java/com/yuanxuan/rokid/toast/ToastManager.kt @@ -0,0 +1,37 @@ +package com.yuanxuan.rokid.toast + +import android.content.Context +import android.view.Gravity +import android.widget.Toast +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + +class ToastManager( + val context: Context, + val scope: CoroutineScope +) { + + fun showShort(message: String?) { + message?.let { + scope.launch(Dispatchers.Main) { + Toast.makeText(context, message, Toast.LENGTH_SHORT) + .apply { + setGravity(Gravity.CENTER, 0, 0) + }.show() + } + } + } + + fun showLong(message: String?) { + message?.let { + scope.launch(Dispatchers.Main) { + Toast.makeText(context, message, Toast.LENGTH_LONG) + .apply { + setGravity(Gravity.CENTER, 0, 0) + }.show() + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/yuanxuan/rokid/toast/ToastUtils.kt b/app/src/main/java/com/yuanxuan/rokid/toast/ToastUtils.kt new file mode 100644 index 0000000..8c9efae --- /dev/null +++ b/app/src/main/java/com/yuanxuan/rokid/toast/ToastUtils.kt @@ -0,0 +1,19 @@ +package com.yuanxuan.rokid.toast + +import com.yuanxuan.rokid.dependencies.AppDependencies + +object ToastUtils { + + private val toastManager by lazy { + AppDependencies.toastManager + } + + fun showShort(messages: String?) { + toastManager.showShort(messages) + } + + fun showLong(messages: String?) { + toastManager.showLong(messages) + } + +} \ No newline at end of file