feat: toast
This commit is contained in:
parent
1b0e5ec8ab
commit
ca99eaa3e4
|
|
@ -6,21 +6,6 @@ import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.ViewCompat
|
import androidx.core.view.ViewCompat
|
||||||
import androidx.core.view.WindowInsetsCompat
|
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() {
|
class MainActivity : AppCompatActivity() {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
|
@ -32,26 +17,8 @@ class MainActivity : AppCompatActivity() {
|
||||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||||
insets
|
insets
|
||||||
}
|
}
|
||||||
val json = "{\n" +
|
|
||||||
" \"code\": 0,\n" +
|
|
||||||
" \"msg\": \"success\",\n" +
|
|
||||||
" \"data\": {\n" +
|
|
||||||
" \"userId\": \"123\",\n" +
|
|
||||||
" \"name\": \"Alice\"\n" +
|
|
||||||
" }\n" +
|
|
||||||
"}"
|
|
||||||
|
|
||||||
val x = test<Test>(json)
|
|
||||||
|
|
||||||
// onBackPressedDispatcher.addCallback {
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
private inline fun <reified T> test(responseBody: String): T {
|
|
||||||
val typeToken = object : TypeToken<ApiResponse<T>>() {}
|
|
||||||
val typeOfT: Type = typeToken.type
|
|
||||||
val apiResponse: ApiResponse<T> = Gson().fromJson(responseBody, typeOfT)
|
|
||||||
return apiResponse.data
|
|
||||||
|
|
||||||
|
onBackPressedDispatcher.addCallback {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import com.yuanxuan.rokid.network.http.ApiRepository
|
||||||
import com.yuanxuan.rokid.network.http.ApiService
|
import com.yuanxuan.rokid.network.http.ApiService
|
||||||
import com.yuanxuan.rokid.network.http.RetrofitClient
|
import com.yuanxuan.rokid.network.http.RetrofitClient
|
||||||
import com.yuanxuan.rokid.network.websocket.WebSocketManager
|
import com.yuanxuan.rokid.network.websocket.WebSocketManager
|
||||||
|
import com.yuanxuan.rokid.toast.ToastManager
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 项目的服务管理器,所有依赖集中管理
|
* 项目的服务管理器,所有依赖集中管理
|
||||||
|
|
@ -44,12 +45,16 @@ object AppDependencies {
|
||||||
apiService = retrofitClient.apiService
|
apiService = retrofitClient.apiService
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
val toastManager by lazy {
|
||||||
|
provider.provideToastManager()
|
||||||
|
}
|
||||||
|
|
||||||
interface Provider {
|
interface Provider {
|
||||||
fun provideDeviceServiceManager(): DeviceServiceManager
|
fun provideDeviceServiceManager(): DeviceServiceManager
|
||||||
fun provideWebSocketManager(): WebSocketManager
|
fun provideWebSocketManager(): WebSocketManager
|
||||||
fun provideRetrofitClient(): RetrofitClient
|
fun provideRetrofitClient(): RetrofitClient
|
||||||
fun provideApiRepository(apiService: ApiService): ApiRepository
|
fun provideApiRepository(apiService: ApiService): ApiRepository
|
||||||
|
fun provideToastManager(): ToastManager
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import com.yuanxuan.rokid.network.http.ApiRepository
|
||||||
import com.yuanxuan.rokid.network.http.ApiService
|
import com.yuanxuan.rokid.network.http.ApiService
|
||||||
import com.yuanxuan.rokid.network.http.RetrofitClient
|
import com.yuanxuan.rokid.network.http.RetrofitClient
|
||||||
import com.yuanxuan.rokid.network.websocket.WebSocketManager
|
import com.yuanxuan.rokid.network.websocket.WebSocketManager
|
||||||
|
import com.yuanxuan.rokid.toast.ToastManager
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
|
||||||
class ApplicationDependencyProvider(val context: Application, val scope: 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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue