Skip to content

Commit

Permalink
로컬/컨피스 선택
Browse files Browse the repository at this point in the history
  • Loading branch information
JuTaK97 committed Aug 6, 2023
1 parent 0688b8d commit cca04e8
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 9 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/com/wafflestudio/snutt2/data/SNUTTStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ class SNUTTStorage @Inject constructor(
)
)

val rnTempConfig = PrefValue<Boolean>(
prefContext,
PrefValueMetaData(
domain = DOMAIN_SCOPE_LOGIN,
key = "rn_temp_config",
type = Boolean::class.java,
defaultValue = true
)
)

fun clearLoginScope() {
prefContext.clear(DOMAIN_SCOPE_LOGIN)
prefContext.clear(DOMAIN_SCOPE_CURRENT_VERSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface UserRepository {

val firstBookmarkAlert: StateFlow<Boolean>

val rnConfig: StateFlow<Boolean>

// login with local id
suspend fun postSignIn(id: String, password: String)

Expand Down Expand Up @@ -89,4 +91,8 @@ interface UserRepository {
suspend fun setCompactMode(compact: Boolean)

suspend fun setFirstBookmarkAlertShown()

suspend fun setRNConfig(state: Boolean)

suspend fun rnUrl(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class UserRepositoryImpl @Inject constructor(

override val firstBookmarkAlert = storage.firstBookmarkAlert.asStateFlow()

override val rnConfig: StateFlow<Boolean>
get() = storage.rnTempConfig.asStateFlow()

override suspend fun postSignIn(id: String, password: String) {
val response = api._postSignIn(PostSignInParams(id, password))
storage.prefKeyUserId.update(response.userId.toOptional())
Expand Down Expand Up @@ -256,6 +259,14 @@ class UserRepositoryImpl @Inject constructor(
storage.firstBookmarkAlert.update(false)
}

override suspend fun setRNConfig(state: Boolean) {
storage.rnTempConfig.update(state)
}

override suspend fun rnUrl(): String {
return api._getRemoteConfig().friends?.src?.get("android") ?: ""
}

private suspend fun getFirebaseToken(): String {
return suspendCoroutine { cont ->
FirebaseMessaging.getInstance().token.addOnCompleteListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,7 @@ interface SNUTTRestApi {
suspend fun _deleteBookmark(
@Body body: PostBookmarkParams
)

@GET("/v1/configs")
suspend fun _getRemoteConfig(): GetRemoteConfigResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wafflestudio.snutt2.lib.network.dto

import RemoteConfigDto

typealias GetRemoteConfigResponse = RemoteConfigDto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@JsonClass(generateAdapter = true)
data class RemoteConfigDto(
@Json(name = "reactNativeBundleFriends") val friends: ReactNativeBundleSrc? = null,
)

data class ReactNativeBundleSrc(
@Json(name = "src") val src: Map<String, String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ val LocalCompactState = compositionLocalOf<Boolean> {
val LocalTableState = compositionLocalOf<TableState> {
throw RuntimeException("")
}

val LocalRNConfigState = compositionLocalOf { false }
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.os.Bundle
import androidx.activity.viewModels
import com.facebook.hermes.reactexecutor.HermesExecutorFactory
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.ReactInstanceManager
import com.facebook.react.ReactRootView
import com.facebook.react.common.LifecycleState
Expand Down Expand Up @@ -35,7 +34,7 @@ class RNModuleActivity : ReactActivity() {
.build()

rootView = ReactRootView(this@RNModuleActivity)
rootView?.startReactApplication(reactInstanceManager, "friends", Bundle().apply { putString("token", rnViewModel.token)} )
rootView?.startReactApplication(reactInstanceManager, "friends", Bundle().apply { putString("token", rnViewModel.token) })
setContentView(rootView)
}
}
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/com/wafflestudio/snutt2/views/RNViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ class RNViewModel @Inject constructor(

init {
viewModelScope.launch(Dispatchers.IO) {
val bundleFile = File(application.applicationContext.cacheDir, "android.jsbundle")
if (bundleFile.canRead().not()) {
val urlConnection = URL("https://snutt-rn-assets.s3.ap-northeast-2.amazonaws.com/android.jsbundle").openConnection() as HttpURLConnection
try {
val bundleFile = File(application.applicationContext.cacheDir, "android.jsbundle")

val url = if (userRepository.rnConfig.value) {
"http://localhost:8081/index.bundle?platform=android"
} else {
userRepository.rnUrl()
}
val urlConnection = URL(url).openConnection() as HttpURLConnection
urlConnection.connect()
val inputStream = urlConnection.inputStream

Expand All @@ -44,8 +50,9 @@ class RNViewModel @Inject constructor(
outputStream.close()
inputStream.close()
urlConnection.disconnect()
_done.postValue(true)
} catch (e: Exception) {
}
_done.postValue(true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fun TimetableConfigPage() {
}

@Composable
private fun PoorSwitch(state: Boolean) {
fun PoorSwitch(state: Boolean) {
val switchOffset by animateDpAsState(
targetValue = if (state) 10.dp else 30.dp
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class UserViewModel @Inject constructor(

val firstBookmarkAlert: StateFlow<Boolean> = userRepository.firstBookmarkAlert

val rnConfig = userRepository.rnConfig

suspend fun fetchUserInfo() {
userRepository.fetchUserInfo()
}
Expand Down Expand Up @@ -151,4 +153,8 @@ class UserViewModel @Inject constructor(
suspend fun setFirstBookmarkAlertShown() {
userRepository.setFirstBookmarkAlertShown()
}

suspend fun toggleRNConfig() {
userRepository.setRNConfig(rnConfig.value.not())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package com.wafflestudio.snutt2.views.logged_in.home.timetable
import android.app.Activity
import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.Text
Expand All @@ -17,6 +20,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -47,9 +51,12 @@ import com.wafflestudio.snutt2.views.LocalTableState
import com.wafflestudio.snutt2.views.NavigationDestination
import com.wafflestudio.snutt2.views.RNModuleActivity
import com.wafflestudio.snutt2.views.logged_in.home.TableListViewModel
import com.wafflestudio.snutt2.views.logged_in.home.settings.PoorSwitch
import com.wafflestudio.snutt2.views.logged_in.home.settings.UserViewModel
import com.wafflestudio.snutt2.views.logged_in.home.showTitleChangeDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File

@Composable
Expand All @@ -65,6 +72,7 @@ fun TimetablePage() {
val userViewModel = hiltViewModel<UserViewModel>()
val newSemesterNotify by tableListViewModel.newSemesterNotify.collectAsState(false)
val firstBookmarkAlert by userViewModel.firstBookmarkAlert.collectAsState()
val config by userViewModel.rnConfig.collectAsState()

var timetableHeight by remember { mutableStateOf(0) }
var topBarHeight by remember { mutableStateOf(0) }
Expand All @@ -84,7 +92,12 @@ fun TimetablePage() {
modifier = Modifier
.weight(1f, fill = false)
.clicks {
showTitleChangeDialog(table.title, table.id, composableStates, tableListViewModel::changeTableName)
showTitleChangeDialog(
table.title,
table.id,
composableStates,
tableListViewModel::changeTableName
)
}
)
Spacer(modifier = Modifier.width(8.dp))
Expand Down Expand Up @@ -144,11 +157,36 @@ fun TimetablePage() {
BookmarkPageIcon(
modifier = centerAlignedModifier
.size(30.dp)
.clicks { navController.navigate(NavigationDestination.Bookmark) { launchSingleTop = true } },
.clicks {
navController.navigate(NavigationDestination.Bookmark) {
launchSingleTop = true
}
},
)
}
}
)
Row(
modifier = Modifier.fillMaxWidth().height(50.dp).clicks {
scope.launch {
userViewModel.toggleRNConfig()
withContext(Dispatchers.Main) {
launch {
context.toast(
if (userViewModel.rnConfig.value) "로컬 번들 사용"
else "컨피그 번들 사용"
)
}
}
}
},
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Text(text = "컨피그", style = if (config.not()) SNUTTTypography.h3 else SNUTTTypography.subtitle1)
PoorSwitch(state = config)
Text(text = "로컬", style = if (config) SNUTTTypography.h3 else SNUTTTypography.subtitle1)
}
Box(
modifier = Modifier
.weight(1f)
Expand Down

0 comments on commit cca04e8

Please sign in to comment.