-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppViewModel.kt
61 lines (48 loc) · 1.53 KB
/
AppViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.park.quest.viewmodels
import android.content.Context
import androidx.compose.runtime.*
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.ViewModel
import androidx.navigation.NavHostController
import dagger.hilt.android.lifecycle.HiltViewModel
import java.io.InputStream
import java.util.*
import javax.inject.Inject
/**
* Created by Nirbhay Pherwani on 7/24/2023.
*/
@HiltViewModel
internal class AppViewModel @Inject constructor() : ViewModel() {
lateinit var navController: NavHostController
}
internal val LocalAppViewModel = compositionLocalOf { AppViewModelInfo() }
@Composable
fun ProvideAppViewModel(
navHostController: NavHostController,
content: @Composable () -> Unit
) {
val appViewModel: AppViewModel = hiltViewModel()
appViewModel.navController = navHostController
val appViewModelInfo = AppViewModelInfo(appViewModel)
val state = remember { appViewModelInfo }
CompositionLocalProvider(
LocalAppViewModel provides state
) {
content()
}
}
internal class AppViewModelInfo(
var appViewModel: AppViewModel? = null
)
object Utility {
fun readRawResource(context: Context, resourceId: Int): String? {
return try {
val inputStream: InputStream = context.resources.openRawResource(resourceId)
val scanner: Scanner = Scanner(inputStream).useDelimiter("\\A")
if (scanner.hasNext()) scanner.next() else ""
} catch (e: Exception) {
e.printStackTrace()
""
}
}
}