davidliu

consolidate into main view model

... ... @@ -42,6 +42,7 @@ dependencies {
api "androidx.lifecycle:lifecycle-common-java8:${versions.androidx_lifecycle}"
api "com.google.protobuf:protobuf-java:${versions.protobuf}"
api project(":livekit-android-sdk")
implementation 'androidx.preference:preference-ktx:1.1.1'
// debugImplementation because LeakCanary should only run in debug builds.
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'
testImplementation 'junit:junit:4.+'
... ...
package io.livekit.android.sample
import android.app.Application
import androidx.core.content.edit
import androidx.lifecycle.AndroidViewModel
import androidx.preference.PreferenceManager
class MainViewModel(application: Application) : AndroidViewModel(application) {
private val preferences = PreferenceManager.getDefaultSharedPreferences(application)
fun getSavedUrl() = preferences.getString(PREFERENCES_KEY_URL, URL) as String
fun getSavedToken() = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN) as String
fun setSavedUrl(url: String) {
preferences.edit {
putString(PREFERENCES_KEY_URL, url)
}
}
fun setSavedToken(token: String) {
preferences.edit {
putString(PREFERENCES_KEY_TOKEN, token)
}
}
fun reset() {
preferences.edit { clear() }
}
companion object {
private const val PREFERENCES_KEY_URL = "url"
private const val PREFERENCES_KEY_TOKEN = "token"
const val URL = "ws://192.168.11.5:7880"
const val TOKEN =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDUyNDU1ODEsImlzcyI6IkFQSVNRdmdrYWJZdXFUQSIsImp0aSI6InBob25lIiwibmJmIjoxNjQyNjUzNTgxLCJzdWIiOiJwaG9uZSIsInZpZGVvIjp7InJvb20iOiJteXJvb20iLCJyb29tSm9pbiI6dHJ1ZX19.JLJgQoCdPCTELJYsEhWGOiBuLl3eoZksWyAl08tJqLg" //nocommit
}
}
\ No newline at end of file
... ...
... ... @@ -63,7 +63,6 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidx_lifecycle}"
implementation "androidx.lifecycle:lifecycle-common-java8:${versions.androidx_lifecycle}"
implementation 'androidx.activity:activity-compose:1.3.1'
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'com.google.accompanist:accompanist-pager:0.19.0'
implementation 'com.google.accompanist:accompanist-pager-indicators:0.19.0'
implementation deps.timber
... ...
... ... @@ -8,6 +8,7 @@ import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
... ... @@ -18,24 +19,22 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import com.google.accompanist.pager.ExperimentalPagerApi
import io.livekit.android.composesample.ui.theme.AppTheme
import io.livekit.android.sample.MainViewModel
@ExperimentalPagerApi
class MainActivity : ComponentActivity() {
val viewModel by viewModels<MainViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestPermissions()
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val defaultUrl = preferences.getString(PREFERENCES_KEY_URL, URL) as String
val defaultToken = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN) as String
setContent {
MainContent(
defaultUrl = defaultUrl,
defaultToken = defaultToken,
defaultUrl = viewModel.getSavedUrl(),
defaultToken = viewModel.getSavedToken(),
onConnect = { url, token ->
val intent = Intent(this@MainActivity, CallActivity::class.java).apply {
putExtra(
... ... @@ -49,10 +48,8 @@ class MainActivity : ComponentActivity() {
startActivity(intent)
},
onSave = { url, token ->
preferences.edit {
putString(PREFERENCES_KEY_URL, url)
putString(PREFERENCES_KEY_TOKEN, token)
}
viewModel.setSavedUrl(url)
viewModel.setSavedToken(token)
Toast.makeText(
this@MainActivity,
... ... @@ -61,9 +58,7 @@ class MainActivity : ComponentActivity() {
).show()
},
onReset = {
preferences.edit {
clear()
}
viewModel.reset()
Toast.makeText(
this@MainActivity,
"Values reset.",
... ... @@ -80,8 +75,8 @@ class MainActivity : ComponentActivity() {
)
@Composable
fun MainContent(
defaultUrl: String = URL,
defaultToken: String = TOKEN,
defaultUrl: String = MainViewModel.URL,
defaultToken: String = MainViewModel.TOKEN,
onConnect: (url: String, token: String) -> Unit = { _, _ -> },
onSave: (url: String, token: String) -> Unit = { _, _ -> },
onReset: () -> Unit = {},
... ... @@ -130,9 +125,9 @@ class MainActivity : ComponentActivity() {
Spacer(modifier = Modifier.height(20.dp))
Button(onClick = {
url = URL
token = TOKEN
onReset()
url = MainViewModel.URL
token = MainViewModel.TOKEN
}) {
Text("Reset Values")
}
... ... @@ -170,11 +165,4 @@ class MainActivity : ComponentActivity() {
}
}
companion object {
const val PREFERENCES_KEY_URL = "url"
const val PREFERENCES_KEY_TOKEN = "token"
const val URL = "wss://www.example.com"
const val TOKEN = ""
}
}
... ...
... ... @@ -41,7 +41,6 @@ dependencies {
implementation "androidx.core:core-ktx:${versions.androidx_core}"
implementation "androidx.activity:activity-ktx:1.4.0"
implementation 'androidx.fragment:fragment-ktx:1.3.6'
implementation 'androidx.preference:preference:1.1.1'
implementation "androidx.viewpager2:viewpager2:1.0.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:${versions.androidx_lifecycle}"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidx_lifecycle}"
... ...
... ... @@ -7,23 +7,22 @@ import android.os.Bundle
import android.text.SpannableStringBuilder
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import io.livekit.android.sample.databinding.MainActivityBinding
class MainActivity : AppCompatActivity() {
val viewModel by viewModels<MainViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = MainActivityBinding.inflate(layoutInflater)
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val urlString = preferences.getString(PREFERENCES_KEY_URL, URL)
val tokenString = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN)
val urlString = viewModel.getSavedUrl()
val tokenString = viewModel.getSavedToken()
binding.run {
url.editText?.text = SpannableStringBuilder(urlString)
token.editText?.text = SpannableStringBuilder(tokenString)
... ... @@ -42,10 +41,9 @@ class MainActivity : AppCompatActivity() {
}
saveButton.setOnClickListener {
preferences.edit {
putString(PREFERENCES_KEY_URL, url.editText?.text.toString())
putString(PREFERENCES_KEY_TOKEN, token.editText?.text.toString())
}
viewModel.setSavedUrl(url.editText?.text?.toString() ?: "")
viewModel.setSavedToken(token.editText?.text?.toString() ?: "")
Toast.makeText(
this@MainActivity,
... ... @@ -55,11 +53,9 @@ class MainActivity : AppCompatActivity() {
}
resetButton.setOnClickListener {
preferences.edit {
clear()
}
url.editText?.text = SpannableStringBuilder(URL)
token.editText?.text = SpannableStringBuilder(TOKEN)
viewModel.reset()
url.editText?.text = SpannableStringBuilder(MainViewModel.URL)
token.editText?.text = SpannableStringBuilder(MainViewModel.TOKEN)
Toast.makeText(
this@MainActivity,
... ... @@ -103,13 +99,4 @@ class MainActivity : AppCompatActivity() {
requestPermissionLauncher.launch(neededPermissions)
}
}
companion object {
const val PREFERENCES_KEY_URL = "url"
const val PREFERENCES_KEY_TOKEN = "token"
const val URL = "wss://livekit.watercooler.fm"
const val TOKEN =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5ODQyMzE0OTgsImlzcyI6IkFQSU1teGlMOHJxdUt6dFpFb1pKVjlGYiIsImp0aSI6ImZvcnRoIiwibmJmIjoxNjI0MjMxNDk4LCJ2aWRlbyI6eyJyb29tIjoibXlyb29tIiwicm9vbUpvaW4iOnRydWV9fQ.PVx_lXAIGxcD2VRslosrbkigc777GXbu-DQME8hjJKI"
}
}
\ No newline at end of file
... ...