davidliu

consolidate into main view model

@@ -42,6 +42,7 @@ dependencies { @@ -42,6 +42,7 @@ dependencies {
42 api "androidx.lifecycle:lifecycle-common-java8:${versions.androidx_lifecycle}" 42 api "androidx.lifecycle:lifecycle-common-java8:${versions.androidx_lifecycle}"
43 api "com.google.protobuf:protobuf-java:${versions.protobuf}" 43 api "com.google.protobuf:protobuf-java:${versions.protobuf}"
44 api project(":livekit-android-sdk") 44 api project(":livekit-android-sdk")
  45 + implementation 'androidx.preference:preference-ktx:1.1.1'
45 // debugImplementation because LeakCanary should only run in debug builds. 46 // debugImplementation because LeakCanary should only run in debug builds.
46 debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1' 47 debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'
47 testImplementation 'junit:junit:4.+' 48 testImplementation 'junit:junit:4.+'
  1 +package io.livekit.android.sample
  2 +
  3 +import android.app.Application
  4 +import androidx.core.content.edit
  5 +import androidx.lifecycle.AndroidViewModel
  6 +import androidx.preference.PreferenceManager
  7 +
  8 +class MainViewModel(application: Application) : AndroidViewModel(application) {
  9 +
  10 + private val preferences = PreferenceManager.getDefaultSharedPreferences(application)
  11 +
  12 + fun getSavedUrl() = preferences.getString(PREFERENCES_KEY_URL, URL) as String
  13 + fun getSavedToken() = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN) as String
  14 +
  15 + fun setSavedUrl(url: String) {
  16 + preferences.edit {
  17 + putString(PREFERENCES_KEY_URL, url)
  18 + }
  19 + }
  20 +
  21 + fun setSavedToken(token: String) {
  22 + preferences.edit {
  23 + putString(PREFERENCES_KEY_TOKEN, token)
  24 + }
  25 + }
  26 +
  27 + fun reset() {
  28 + preferences.edit { clear() }
  29 + }
  30 +
  31 + companion object {
  32 + private const val PREFERENCES_KEY_URL = "url"
  33 + private const val PREFERENCES_KEY_TOKEN = "token"
  34 +
  35 + const val URL = "ws://192.168.11.5:7880"
  36 + const val TOKEN =
  37 + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDUyNDU1ODEsImlzcyI6IkFQSVNRdmdrYWJZdXFUQSIsImp0aSI6InBob25lIiwibmJmIjoxNjQyNjUzNTgxLCJzdWIiOiJwaG9uZSIsInZpZGVvIjp7InJvb20iOiJteXJvb20iLCJyb29tSm9pbiI6dHJ1ZX19.JLJgQoCdPCTELJYsEhWGOiBuLl3eoZksWyAl08tJqLg" //nocommit
  38 + }
  39 +}
@@ -63,7 +63,6 @@ dependencies { @@ -63,7 +63,6 @@ dependencies {
63 implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidx_lifecycle}" 63 implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidx_lifecycle}"
64 implementation "androidx.lifecycle:lifecycle-common-java8:${versions.androidx_lifecycle}" 64 implementation "androidx.lifecycle:lifecycle-common-java8:${versions.androidx_lifecycle}"
65 implementation 'androidx.activity:activity-compose:1.3.1' 65 implementation 'androidx.activity:activity-compose:1.3.1'
66 - implementation 'androidx.preference:preference-ktx:1.1.1'  
67 implementation 'com.google.accompanist:accompanist-pager:0.19.0' 66 implementation 'com.google.accompanist:accompanist-pager:0.19.0'
68 implementation 'com.google.accompanist:accompanist-pager-indicators:0.19.0' 67 implementation 'com.google.accompanist:accompanist-pager-indicators:0.19.0'
69 implementation deps.timber 68 implementation deps.timber
@@ -8,6 +8,7 @@ import android.widget.Toast @@ -8,6 +8,7 @@ import android.widget.Toast
8 import androidx.activity.ComponentActivity 8 import androidx.activity.ComponentActivity
9 import androidx.activity.compose.setContent 9 import androidx.activity.compose.setContent
10 import androidx.activity.result.contract.ActivityResultContracts 10 import androidx.activity.result.contract.ActivityResultContracts
  11 +import androidx.activity.viewModels
11 import androidx.compose.foundation.Image 12 import androidx.compose.foundation.Image
12 import androidx.compose.foundation.layout.* 13 import androidx.compose.foundation.layout.*
13 import androidx.compose.material.* 14 import androidx.compose.material.*
@@ -18,24 +19,22 @@ import androidx.compose.ui.res.painterResource @@ -18,24 +19,22 @@ import androidx.compose.ui.res.painterResource
18 import androidx.compose.ui.tooling.preview.Preview 19 import androidx.compose.ui.tooling.preview.Preview
19 import androidx.compose.ui.unit.dp 20 import androidx.compose.ui.unit.dp
20 import androidx.core.content.ContextCompat 21 import androidx.core.content.ContextCompat
21 -import androidx.core.content.edit  
22 -import androidx.preference.PreferenceManager  
23 import com.google.accompanist.pager.ExperimentalPagerApi 22 import com.google.accompanist.pager.ExperimentalPagerApi
24 import io.livekit.android.composesample.ui.theme.AppTheme 23 import io.livekit.android.composesample.ui.theme.AppTheme
  24 +import io.livekit.android.sample.MainViewModel
25 25
26 @ExperimentalPagerApi 26 @ExperimentalPagerApi
27 class MainActivity : ComponentActivity() { 27 class MainActivity : ComponentActivity() {
  28 +
  29 + val viewModel by viewModels<MainViewModel>()
28 override fun onCreate(savedInstanceState: Bundle?) { 30 override fun onCreate(savedInstanceState: Bundle?) {
29 super.onCreate(savedInstanceState) 31 super.onCreate(savedInstanceState)
30 32
31 requestPermissions() 33 requestPermissions()
32 - val preferences = PreferenceManager.getDefaultSharedPreferences(this)  
33 - val defaultUrl = preferences.getString(PREFERENCES_KEY_URL, URL) as String  
34 - val defaultToken = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN) as String  
35 setContent { 34 setContent {
36 MainContent( 35 MainContent(
37 - defaultUrl = defaultUrl,  
38 - defaultToken = defaultToken, 36 + defaultUrl = viewModel.getSavedUrl(),
  37 + defaultToken = viewModel.getSavedToken(),
39 onConnect = { url, token -> 38 onConnect = { url, token ->
40 val intent = Intent(this@MainActivity, CallActivity::class.java).apply { 39 val intent = Intent(this@MainActivity, CallActivity::class.java).apply {
41 putExtra( 40 putExtra(
@@ -49,10 +48,8 @@ class MainActivity : ComponentActivity() { @@ -49,10 +48,8 @@ class MainActivity : ComponentActivity() {
49 startActivity(intent) 48 startActivity(intent)
50 }, 49 },
51 onSave = { url, token -> 50 onSave = { url, token ->
52 - preferences.edit {  
53 - putString(PREFERENCES_KEY_URL, url)  
54 - putString(PREFERENCES_KEY_TOKEN, token)  
55 - } 51 + viewModel.setSavedUrl(url)
  52 + viewModel.setSavedToken(token)
56 53
57 Toast.makeText( 54 Toast.makeText(
58 this@MainActivity, 55 this@MainActivity,
@@ -61,9 +58,7 @@ class MainActivity : ComponentActivity() { @@ -61,9 +58,7 @@ class MainActivity : ComponentActivity() {
61 ).show() 58 ).show()
62 }, 59 },
63 onReset = { 60 onReset = {
64 - preferences.edit {  
65 - clear()  
66 - } 61 + viewModel.reset()
67 Toast.makeText( 62 Toast.makeText(
68 this@MainActivity, 63 this@MainActivity,
69 "Values reset.", 64 "Values reset.",
@@ -80,8 +75,8 @@ class MainActivity : ComponentActivity() { @@ -80,8 +75,8 @@ class MainActivity : ComponentActivity() {
80 ) 75 )
81 @Composable 76 @Composable
82 fun MainContent( 77 fun MainContent(
83 - defaultUrl: String = URL,  
84 - defaultToken: String = TOKEN, 78 + defaultUrl: String = MainViewModel.URL,
  79 + defaultToken: String = MainViewModel.TOKEN,
85 onConnect: (url: String, token: String) -> Unit = { _, _ -> }, 80 onConnect: (url: String, token: String) -> Unit = { _, _ -> },
86 onSave: (url: String, token: String) -> Unit = { _, _ -> }, 81 onSave: (url: String, token: String) -> Unit = { _, _ -> },
87 onReset: () -> Unit = {}, 82 onReset: () -> Unit = {},
@@ -130,9 +125,9 @@ class MainActivity : ComponentActivity() { @@ -130,9 +125,9 @@ class MainActivity : ComponentActivity() {
130 125
131 Spacer(modifier = Modifier.height(20.dp)) 126 Spacer(modifier = Modifier.height(20.dp))
132 Button(onClick = { 127 Button(onClick = {
133 - url = URL  
134 - token = TOKEN  
135 onReset() 128 onReset()
  129 + url = MainViewModel.URL
  130 + token = MainViewModel.TOKEN
136 }) { 131 }) {
137 Text("Reset Values") 132 Text("Reset Values")
138 } 133 }
@@ -170,11 +165,4 @@ class MainActivity : ComponentActivity() { @@ -170,11 +165,4 @@ class MainActivity : ComponentActivity() {
170 } 165 }
171 } 166 }
172 167
173 - companion object {  
174 - const val PREFERENCES_KEY_URL = "url"  
175 - const val PREFERENCES_KEY_TOKEN = "token"  
176 -  
177 - const val URL = "wss://www.example.com"  
178 - const val TOKEN = ""  
179 - }  
180 } 168 }
@@ -41,7 +41,6 @@ dependencies { @@ -41,7 +41,6 @@ dependencies {
41 implementation "androidx.core:core-ktx:${versions.androidx_core}" 41 implementation "androidx.core:core-ktx:${versions.androidx_core}"
42 implementation "androidx.activity:activity-ktx:1.4.0" 42 implementation "androidx.activity:activity-ktx:1.4.0"
43 implementation 'androidx.fragment:fragment-ktx:1.3.6' 43 implementation 'androidx.fragment:fragment-ktx:1.3.6'
44 - implementation 'androidx.preference:preference:1.1.1'  
45 implementation "androidx.viewpager2:viewpager2:1.0.0" 44 implementation "androidx.viewpager2:viewpager2:1.0.0"
46 implementation "androidx.lifecycle:lifecycle-runtime-ktx:${versions.androidx_lifecycle}" 45 implementation "androidx.lifecycle:lifecycle-runtime-ktx:${versions.androidx_lifecycle}"
47 implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidx_lifecycle}" 46 implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidx_lifecycle}"
@@ -7,23 +7,22 @@ import android.os.Bundle @@ -7,23 +7,22 @@ import android.os.Bundle
7 import android.text.SpannableStringBuilder 7 import android.text.SpannableStringBuilder
8 import android.widget.Toast 8 import android.widget.Toast
9 import androidx.activity.result.contract.ActivityResultContracts 9 import androidx.activity.result.contract.ActivityResultContracts
  10 +import androidx.activity.viewModels
10 import androidx.appcompat.app.AppCompatActivity 11 import androidx.appcompat.app.AppCompatActivity
11 import androidx.core.content.ContextCompat 12 import androidx.core.content.ContextCompat
12 -import androidx.core.content.edit  
13 -import androidx.preference.PreferenceManager  
14 import io.livekit.android.sample.databinding.MainActivityBinding 13 import io.livekit.android.sample.databinding.MainActivityBinding
15 14
16 15
17 class MainActivity : AppCompatActivity() { 16 class MainActivity : AppCompatActivity() {
18 17
  18 + val viewModel by viewModels<MainViewModel>()
19 override fun onCreate(savedInstanceState: Bundle?) { 19 override fun onCreate(savedInstanceState: Bundle?) {
20 super.onCreate(savedInstanceState) 20 super.onCreate(savedInstanceState)
21 21
22 val binding = MainActivityBinding.inflate(layoutInflater) 22 val binding = MainActivityBinding.inflate(layoutInflater)
23 23
24 - val preferences = PreferenceManager.getDefaultSharedPreferences(this)  
25 - val urlString = preferences.getString(PREFERENCES_KEY_URL, URL)  
26 - val tokenString = preferences.getString(PREFERENCES_KEY_TOKEN, TOKEN) 24 + val urlString = viewModel.getSavedUrl()
  25 + val tokenString = viewModel.getSavedToken()
27 binding.run { 26 binding.run {
28 url.editText?.text = SpannableStringBuilder(urlString) 27 url.editText?.text = SpannableStringBuilder(urlString)
29 token.editText?.text = SpannableStringBuilder(tokenString) 28 token.editText?.text = SpannableStringBuilder(tokenString)
@@ -42,10 +41,9 @@ class MainActivity : AppCompatActivity() { @@ -42,10 +41,9 @@ class MainActivity : AppCompatActivity() {
42 } 41 }
43 42
44 saveButton.setOnClickListener { 43 saveButton.setOnClickListener {
45 - preferences.edit {  
46 - putString(PREFERENCES_KEY_URL, url.editText?.text.toString())  
47 - putString(PREFERENCES_KEY_TOKEN, token.editText?.text.toString())  
48 - } 44 +
  45 + viewModel.setSavedUrl(url.editText?.text?.toString() ?: "")
  46 + viewModel.setSavedToken(token.editText?.text?.toString() ?: "")
49 47
50 Toast.makeText( 48 Toast.makeText(
51 this@MainActivity, 49 this@MainActivity,
@@ -55,11 +53,9 @@ class MainActivity : AppCompatActivity() { @@ -55,11 +53,9 @@ class MainActivity : AppCompatActivity() {
55 } 53 }
56 54
57 resetButton.setOnClickListener { 55 resetButton.setOnClickListener {
58 - preferences.edit {  
59 - clear()  
60 - }  
61 - url.editText?.text = SpannableStringBuilder(URL)  
62 - token.editText?.text = SpannableStringBuilder(TOKEN) 56 + viewModel.reset()
  57 + url.editText?.text = SpannableStringBuilder(MainViewModel.URL)
  58 + token.editText?.text = SpannableStringBuilder(MainViewModel.TOKEN)
63 59
64 Toast.makeText( 60 Toast.makeText(
65 this@MainActivity, 61 this@MainActivity,
@@ -103,13 +99,4 @@ class MainActivity : AppCompatActivity() { @@ -103,13 +99,4 @@ class MainActivity : AppCompatActivity() {
103 requestPermissionLauncher.launch(neededPermissions) 99 requestPermissionLauncher.launch(neededPermissions)
104 } 100 }
105 } 101 }
106 -  
107 - companion object {  
108 - const val PREFERENCES_KEY_URL = "url"  
109 - const val PREFERENCES_KEY_TOKEN = "token"  
110 -  
111 - const val URL = "wss://livekit.watercooler.fm"  
112 - const val TOKEN =  
113 - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5ODQyMzE0OTgsImlzcyI6IkFQSU1teGlMOHJxdUt6dFpFb1pKVjlGYiIsImp0aSI6ImZvcnRoIiwibmJmIjoxNjI0MjMxNDk4LCJ2aWRlbyI6eyJyb29tIjoibXlyb29tIiwicm9vbUpvaW4iOnRydWV9fQ.PVx_lXAIGxcD2VRslosrbkigc777GXbu-DQME8hjJKI"  
114 - }  
115 } 102 }