Skip to content

Commit

Permalink
Code refactoring. Use AppData instead AppDataHolder.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloNetrebchuk committed Nov 3, 2023
1 parent 432fd4e commit a962fee
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 66 deletions.
22 changes: 4 additions & 18 deletions app/src/main/java/org/openedx/app/AppActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ import org.openedx.app.databinding.ActivityAppBinding
import org.openedx.auth.presentation.signin.SignInFragment
import org.openedx.core.data.storage.CorePreferences
import org.openedx.core.extension.requestApplyInsetsWhenAttached
import org.openedx.core.presentation.global.AppData
import org.openedx.core.presentation.global.AppDataHolder
import org.openedx.core.presentation.global.InsetHolder
import org.openedx.core.presentation.global.WindowSizeHolder
import org.openedx.core.ui.WindowSize
import org.openedx.core.ui.WindowType
import org.openedx.profile.presentation.ProfileRouter
import org.openedx.whatsnew.WhatsNewFileManager
import org.openedx.whatsnew.data.storage.WhatsNewPreferences
import org.openedx.whatsnew.WhatsNewManagerManager
import org.openedx.whatsnew.presentation.whatsnew.WhatsNewFragment

class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder, AppDataHolder {
class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder {

override val topInset: Int
get() = _insetTop
Expand All @@ -40,13 +37,9 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder, AppDataH
override val windowSize: WindowSize
get() = _windowSize

override val appData: AppData
get() = AppData(BuildConfig.VERSION_NAME)

private lateinit var binding: ActivityAppBinding
private val viewModel by viewModel<AppViewModel>()
private val whatsNewFileManager by inject<WhatsNewFileManager>()
private val whatsNewPreferencesManager by inject<WhatsNewPreferences>()
private val whatsNewManager by inject<WhatsNewManagerManager>()
private val corePreferencesManager by inject<CorePreferences>()
private val profileRouter by inject<ProfileRouter>()

Expand Down Expand Up @@ -122,7 +115,7 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder, AppDataH
.commit()
}

shouldShowWhatsNew() -> {
whatsNewManager.shouldShowWhatsNew() -> {
supportFragmentManager.beginTransaction()
.add(R.id.container, WhatsNewFragment())
.commit()
Expand Down Expand Up @@ -170,13 +163,6 @@ class AppActivity : AppCompatActivity(), InsetHolder, WindowSizeHolder, AppDataH
}
}

override fun shouldShowWhatsNew(): Boolean {
val dataVersion = whatsNewFileManager.getNewestData().version
return BuildConfig.VERSION_NAME == dataVersion
&& whatsNewPreferencesManager.lastWhatsNewVersion != dataVersion
&& org.openedx.core.BuildConfig.SHOW_WHATS_NEW
}

companion object {
const val TOP_INSET = "topInset"
const val BOTTOM_INSET = "bottomInset"
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/org/openedx/app/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.openedx.core.module.TranscriptManager
import org.openedx.core.module.download.FileDownloader
import org.openedx.core.presentation.dialog.appreview.AppReviewManager
import org.openedx.core.presentation.global.AppData
import org.openedx.core.presentation.global.WhatsNewGlobalManager
import org.openedx.core.system.AppCookieManager
import org.openedx.core.system.ResourceManager
import org.openedx.core.system.connection.NetworkConnection
Expand All @@ -46,7 +47,7 @@ import org.openedx.profile.presentation.ProfileRouter
import org.openedx.profile.system.notifier.ProfileNotifier
import org.openedx.core.presentation.global.app_upgrade.AppUpgradeRouter
import org.openedx.core.system.notifier.AppUpgradeNotifier
import org.openedx.whatsnew.WhatsNewFileManager
import org.openedx.whatsnew.WhatsNewManagerManager
import org.openedx.whatsnew.WhatsNewRouter
import org.openedx.whatsnew.data.storage.WhatsNewPreferences

Expand Down Expand Up @@ -132,7 +133,8 @@ val appModule = module {
single { (activity: AppCompatActivity) -> AppReviewManager(activity, get(), get()) }

single { TranscriptManager(get()) }
single { WhatsNewFileManager(get()) }
single { WhatsNewManagerManager(get(), get(), get()) }
single<WhatsNewGlobalManager> { get<WhatsNewManagerManager>() }

single { AnalyticsManager(get()) }
single<DashboardAnalytics> { get<AnalyticsManager>() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,13 @@ import org.openedx.core.ui.theme.appShapes
import org.openedx.core.ui.theme.appTypography
import org.openedx.core.ui.windowSizeValue
import org.openedx.core.AppUpdateState
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.openedx.core.presentation.global.AppDataHolder
import org.openedx.core.presentation.global.WhatsNewGlobalManager

class SignInFragment : Fragment() {

private val viewModel: SignInViewModel by viewModel()
private val router: AuthRouter by inject()
private val whatsNewGlobalManager by inject<WhatsNewGlobalManager>()

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -119,7 +118,7 @@ class SignInFragment : Fragment() {
)

LaunchedEffect(loginSuccess) {
val isNeedToShowWhatsNew = (requireActivity() as AppDataHolder).shouldShowWhatsNew()
val isNeedToShowWhatsNew = whatsNewGlobalManager.shouldShowWhatsNew()
if (loginSuccess) {
if (isNeedToShowWhatsNew) {
router.navigateToWhatsNew(parentFragmentManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ fun DefaultDialogBox(
Box(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 4.dp)
.noRippleClickable {
onDismissClock()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.openedx.core.presentation.global.AppData
open class BaseAppReviewDialogFragment : DialogFragment() {

private val reviewPreferences: InAppReviewPreferences by inject()
private val appData: AppData by inject()
val appData: AppData by inject()

fun saveVersionName() {
val versionName = appData.versionName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import org.openedx.core.presentation.global.AppDataHolder
import org.openedx.core.ui.theme.OpenEdXTheme
import org.openedx.core.utils.EmailUtil

Expand Down Expand Up @@ -58,7 +57,7 @@ class FeedbackDialogFragment : BaseAppReviewDialogFragment() {
EmailUtil.showFeedbackScreen(
context = requireContext(),
feedback = feedback,
appVersion = (requireActivity() as AppDataHolder).appData.versionName
appVersion = appData.versionName
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.openedx.core.presentation.global

data class AppData(
val versionName: String
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.openedx.core.presentation.global

interface WhatsNewGlobalManager {
fun shouldShowWhatsNew(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fun AppUpgradeRecommendDialog(
Box(
modifier = modifier
.fillMaxSize()
.padding(horizontal = 12.dp)
.padding(horizontal = 4.dp)
.noRippleClickable {
onNotNowClick()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@ import androidx.compose.ui.window.Dialog
import androidx.fragment.app.Fragment
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.openedx.core.AppUpdateState
import org.openedx.core.R
import org.openedx.core.UIMessage
import org.openedx.core.domain.model.ProfileImage
import org.openedx.core.presentation.global.AppData
import org.openedx.core.presentation.global.AppDataHolder
import org.openedx.core.system.notifier.AppUpgradeEvent
import org.openedx.core.ui.*
import org.openedx.core.ui.theme.OpenEdXTheme
import org.openedx.core.ui.theme.appColors
import org.openedx.core.ui.theme.appShapes
import org.openedx.core.ui.theme.appTypography
import org.openedx.core.AppUpdateState
import org.openedx.core.utils.EmailUtil
import org.openedx.profile.domain.model.Account
import org.openedx.profile.presentation.ProfileRouter
Expand All @@ -59,6 +58,7 @@ class ProfileFragment : Fragment() {

private val viewModel: ProfileViewModel by viewModel()
private val router by inject<ProfileRouter>()
private val appData: AppData by inject()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -85,7 +85,7 @@ class ProfileFragment : Fragment() {
windowSize = windowSize,
uiState = uiState!!,
uiMessage = uiMessage,
appData = (requireActivity() as AppDataHolder).appData,
appData = appData,
refreshing = refreshing,
appUpgradeEvent = appUpgradeEvent,
logout = {
Expand Down
15 changes: 0 additions & 15 deletions whatsnew/src/main/java/org/openedx/whatsnew/WhatsNewFileManager.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.openedx.whatsnew

import android.content.Context
import com.google.gson.Gson
import org.openedx.core.presentation.global.AppData
import org.openedx.core.presentation.global.WhatsNewGlobalManager
import org.openedx.whatsnew.data.model.WhatsNewItem
import org.openedx.whatsnew.data.storage.WhatsNewPreferences

class WhatsNewManagerManager(
private val context: Context,
private val whatsNewPreferences: WhatsNewPreferences,
private val appData: AppData
): WhatsNewGlobalManager {
fun getNewestData(): org.openedx.whatsnew.domain.model.WhatsNewItem {
val jsonString = context.resources.openRawResource(R.raw.whats_new)
.bufferedReader()
.use { it.readText() }
val whatsNewListData = Gson().fromJson(jsonString, Array<WhatsNewItem>::class.java)
return whatsNewListData[0].mapToDomain(context)
}

override fun shouldShowWhatsNew(): Boolean {
val dataVersion = getNewestData().version
return appData.versionName == dataVersion
&& whatsNewPreferences.lastWhatsNewVersion != dataVersion
&& org.openedx.core.BuildConfig.SHOW_WHATS_NEW
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import androidx.fragment.app.Fragment
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.openedx.core.presentation.global.AppDataHolder
import org.openedx.core.presentation.global.AppData
import org.openedx.core.ui.WindowSize
import org.openedx.core.ui.calculateCurrentOffsetForPage
import org.openedx.core.ui.rememberWindowSize
Expand All @@ -72,6 +72,7 @@ class WhatsNewFragment : Fragment() {
private val viewModel: WhatsNewViewModel by viewModel()
private val preferencesManager by inject<WhatsNewPreferences>()
private val router by inject<WhatsNewRouter>()
private val appData: AppData by inject()

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -87,7 +88,7 @@ class WhatsNewFragment : Fragment() {
windowSize = windowSize,
whatsNewItem = whatsNewItem.value,
onCloseClick = {
val versionName = (requireActivity() as AppDataHolder).appData.versionName
val versionName = appData.versionName
preferencesManager.lastWhatsNewVersion = versionName
router.navigateToMain(parentFragmentManager)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package org.openedx.whatsnew.presentation.whatsnew
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import org.openedx.core.BaseViewModel
import org.openedx.whatsnew.WhatsNewFileManager
import org.openedx.whatsnew.WhatsNewManagerManager
import org.openedx.whatsnew.domain.model.WhatsNewItem

class WhatsNewViewModel(
private val whatsNewFileManager: WhatsNewFileManager
private val whatsNewManager: WhatsNewManagerManager
) : BaseViewModel() {

private val _whatsNewItem = mutableStateOf<WhatsNewItem?>(null)
Expand All @@ -19,6 +19,6 @@ class WhatsNewViewModel(
}

private fun getNewestData() {
_whatsNewItem.value = whatsNewFileManager.getNewestData()
_whatsNewItem.value = whatsNewManager.getNewestData()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.openedx.whatsnew.presentation.whatsnew.WhatsNewViewModel

class WhatsNewViewModelTest {

private val whatsNewFileManager = mockk<WhatsNewFileManager>()
private val whatsNewManager = mockk<WhatsNewManagerManager>()

private val whatsNewItem = WhatsNewItem(
version = "1.0.0",
Expand All @@ -19,13 +19,13 @@ class WhatsNewViewModelTest {

@Test
fun `getNewestData success`() = runTest {
every { whatsNewFileManager.getNewestData() } returns whatsNewItem
every { whatsNewManager.getNewestData() } returns whatsNewItem

val viewModel = WhatsNewViewModel(
whatsNewFileManager
whatsNewManager
)

verify(exactly = 1) { whatsNewFileManager.getNewestData() }
verify(exactly = 1) { whatsNewManager.getNewestData() }
assert(viewModel.whatsNewItem.value == whatsNewItem)
}
}

0 comments on commit a962fee

Please sign in to comment.