Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Application Theme #406

Merged
merged 18 commits into from
Jun 2, 2024
4 changes: 2 additions & 2 deletions app/src/androidTest/java/com/github/se/assocify/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.navigation.compose.rememberNavController
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.se.assocify.model.CurrentUser
import com.github.se.assocify.model.database.UserAPI
import com.github.se.assocify.model.localsave.LoginSave
import com.github.se.assocify.model.localsave.LocalSave
import com.github.se.assocify.navigation.Destination
import com.github.se.assocify.navigation.NavigationActions
import com.github.se.assocify.ui.screens.login.loginGraph
Expand All @@ -36,7 +36,7 @@ import org.junit.runner.RunWith
@Composable
fun LoginApp() {
val navController = rememberNavController()
val mockLoginSave: LoginSave = mockk(relaxed = true)
val mockLoginSave: LocalSave = mockk(relaxed = true)
val navActions = NavigationActions(navController, mockLoginSave)
val supabaseClient: SupabaseClient =
createSupabaseClient(BuildConfig.SUPABASE_URL, BuildConfig.SUPABASE_ANON_KEY) {
Expand Down
18 changes: 13 additions & 5 deletions app/src/androidTest/java/com/github/se/assocify/Epic1Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import com.github.se.assocify.model.entities.Association
import com.github.se.assocify.model.entities.PermissionRole
import com.github.se.assocify.model.entities.RoleType
import com.github.se.assocify.model.entities.User
import com.github.se.assocify.model.localsave.LoginSave
import com.github.se.assocify.model.localsave.LocalSave
import com.github.se.assocify.navigation.Destination
import com.github.se.assocify.navigation.NavigationActions
import com.github.se.assocify.navigation.mainNavGraph
import com.github.se.assocify.ui.theme.ThemeViewModel
import com.kaspersky.components.composesupport.config.withComposeSupport
import com.kaspersky.kaspresso.kaspresso.Kaspresso
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
Expand Down Expand Up @@ -147,10 +148,11 @@ class Epic1Test : TestCase(kaspressoBuilder = Kaspresso.Builder.withComposeSuppo

private val receiptAPI = mockk<ReceiptAPI>(relaxUnitFun = true)

private val loginSave = mockk<LoginSave>(relaxUnitFun = true)
private val loginSave = mockk<LocalSave>(relaxUnitFun = true)

private val accountingCategoriesAPI = mockk<AccountingCategoryAPI>(relaxUnitFun = true)
private val accountingSubCategoryAPI = mockk<AccountingSubCategoryAPI>(relaxUnitFun = true)
private val appThemeViewModel = mockk<ThemeViewModel>(relaxUnitFun = true)

@Before
fun testSetup() {
Expand All @@ -170,7 +172,9 @@ class Epic1Test : TestCase(kaspressoBuilder = Kaspresso.Builder.withComposeSuppo
taskAPI,
receiptAPI,
accountingCategoriesAPI,
accountingSubCategoryAPI)
accountingSubCategoryAPI,
appThemeViewModel,
loginSave)
}
}

Expand Down Expand Up @@ -236,7 +240,9 @@ fun TestAssocifyApp(
taskAPI: TaskAPI,
receiptAPI: ReceiptAPI,
accountingCategoriesAPI: AccountingCategoryAPI,
accountingSubCategoryAPI: AccountingSubCategoryAPI
accountingSubCategoryAPI: AccountingSubCategoryAPI,
appThemeViewModel: ThemeViewModel,
localSave: LocalSave
) {
CurrentUser.userUid = "1"

Expand All @@ -251,6 +257,8 @@ fun TestAssocifyApp(
taskAPI = taskAPI,
receiptsAPI = receiptAPI,
accountingCategoriesAPI = accountingCategoriesAPI,
accountingSubCategoryAPI = accountingSubCategoryAPI)
accountingSubCategoryAPI = accountingSubCategoryAPI,
appThemeViewModel = appThemeViewModel,
localSave = localSave)
}
}
10 changes: 7 additions & 3 deletions app/src/androidTest/java/com/github/se/assocify/Epic4Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import com.github.se.assocify.model.entities.Association
import com.github.se.assocify.model.entities.PermissionRole
import com.github.se.assocify.model.entities.RoleType
import com.github.se.assocify.model.entities.User
import com.github.se.assocify.model.localsave.LoginSave
import com.github.se.assocify.model.localsave.LocalSave
import com.github.se.assocify.navigation.Destination
import com.github.se.assocify.navigation.NavigationActions
import com.github.se.assocify.ui.theme.ThemeViewModel
import com.kaspersky.components.composesupport.config.withComposeSupport
import com.kaspersky.kaspresso.kaspresso.Kaspresso
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
Expand Down Expand Up @@ -163,7 +164,8 @@ class Epic4Test : TestCase(kaspressoBuilder = Kaspresso.Builder.withComposeSuppo

private val accountingCategoryAPI = mockk<AccountingCategoryAPI>(relaxUnitFun = true)

private val loginSave = mockk<LoginSave>(relaxUnitFun = true)
private val loginSave = mockk<LocalSave>(relaxUnitFun = true)
private val appThemeViewModel = mockk<ThemeViewModel>(relaxUnitFun = true)

@Before
fun testSetup() {
Expand All @@ -183,7 +185,9 @@ class Epic4Test : TestCase(kaspressoBuilder = Kaspresso.Builder.withComposeSuppo
taskAPI,
receiptAPI,
accountingCategoryAPI,
accountingSubCategoryAPI)
accountingSubCategoryAPI,
appThemeViewModel,
loginSave)
}
}

Expand Down
19 changes: 17 additions & 2 deletions app/src/androidTest/java/com/github/se/assocify/ThemeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.onSibling
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.se.assocify.model.entities.Theme
import com.github.se.assocify.ui.theme.AssocifyTheme
import com.github.se.assocify.ui.theme.md_theme_dark_primary
import com.github.se.assocify.ui.theme.md_theme_light_primary
Expand All @@ -34,8 +35,9 @@ class ThemeTest : TestCase(kaspressoBuilder = Kaspresso.Builder.withComposeSuppo

@Test
fun darkTheme() {

composeTestRule.setContent {
AssocifyTheme(darkTheme = true, dynamicColor = false) { TestScreen() }
AssocifyTheme(theme = Theme.DARK, dynamicColor = false) { TestScreen() }
}
with(composeTestRule) {
onNodeWithText("Test")
Expand All @@ -48,7 +50,20 @@ class ThemeTest : TestCase(kaspressoBuilder = Kaspresso.Builder.withComposeSuppo
@Test
fun lightTheme() {
composeTestRule.setContent {
AssocifyTheme(darkTheme = false, dynamicColor = false) { TestScreen() }
AssocifyTheme(theme = Theme.LIGHT, dynamicColor = false) { TestScreen() }
}
with(composeTestRule) {
onNodeWithText("Test")
.assertIsDisplayed()
.onSibling()
.assertTextContains(md_theme_light_primary.toString())
}
}

@Test
fun systemTheme() {
composeTestRule.setContent {
AssocifyTheme(theme = Theme.SYSTEM, dynamicColor = false) { TestScreen() }
}
with(composeTestRule) {
onNodeWithText("Test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.se.assocify.model.CurrentUser
import com.github.se.assocify.model.entities.Theme
import com.github.se.assocify.model.localsave.LocalSave
import com.github.se.assocify.navigation.NavigationActions
import com.github.se.assocify.ui.screens.profile.preferences.ProfilePreferencesScreen
import com.github.se.assocify.ui.theme.ThemeViewModel
import com.kaspersky.components.composesupport.config.withComposeSupport
import com.kaspersky.kaspresso.kaspresso.Kaspresso
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -29,15 +33,21 @@ class ProfilePreferencesScreenTest :

private val navActions = mockk<NavigationActions>()
private var goBack = false
private lateinit var themeVM: ThemeViewModel
private val localSave = mockk<LocalSave>()

@Before
fun testSetup() {
CurrentUser.userUid = "1"
CurrentUser.associationUid = "asso"
themeVM = mockk<ThemeViewModel>(relaxed = true)
every { themeVM.theme } returns MutableStateFlow(Theme.LIGHT)

every { navActions.back() } answers { goBack = true }

composeTestRule.setContent { ProfilePreferencesScreen(navActions = navActions) }
composeTestRule.setContent {
ProfilePreferencesScreen(navActions = navActions, themeVM, localSave)
}
}

@Test
Expand All @@ -47,10 +57,10 @@ class ProfilePreferencesScreenTest :

onNodeWithTag("themeTitle").assertIsDisplayed()
onNodeWithTag("themeSegmentedButtonRow").assertIsDisplayed()
listOf("Light", "Dark", "System").forEach {
onNodeWithText(text = it).assertIsDisplayed().assertIsSelectable()
Theme.entries.forEach {
onNodeWithText(text = it.name).assertIsDisplayed().assertIsSelectable()
}
onNodeWithText(text = "Light").assertIsSelected()
onNodeWithText(text = "LIGHT").assertIsSelected()

onNodeWithTag("textSize").assertIsDisplayed()
onNodeWithTag("textSizeSlider").assertIsDisplayed()
Expand Down
13 changes: 7 additions & 6 deletions app/src/main/java/com/github/se/assocify/AssocifyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ import com.github.se.assocify.model.database.EventAPI
import com.github.se.assocify.model.database.ReceiptAPI
import com.github.se.assocify.model.database.TaskAPI
import com.github.se.assocify.model.database.UserAPI
import com.github.se.assocify.model.localsave.LoginSave
import com.github.se.assocify.model.localsave.LocalSave
import com.github.se.assocify.navigation.Destination
import com.github.se.assocify.navigation.NavigationActions
import com.github.se.assocify.navigation.mainNavGraph
import com.github.se.assocify.ui.theme.ThemeViewModel

@Composable
fun AssocifyApp(loginSaver: LoginSave) {
fun AssocifyApp(localSaver: LocalSave, appThemeViewModel: ThemeViewModel) {
val navController = rememberNavController()
val navActions = NavigationActions(navController, loginSaver)

loginSaver.loadUserInfo()
val navActions = NavigationActions(navController, localSaver)

val userAPI =
UserAPI(
Expand Down Expand Up @@ -62,6 +61,8 @@ fun AssocifyApp(loginSaver: LoginSave) {
taskAPI = taskAPI,
receiptsAPI = receiptsAPI,
accountingCategoriesAPI = accountingCategoriesAPI,
accountingSubCategoryAPI = accountingSubCategoryAPI)
accountingSubCategoryAPI = accountingSubCategoryAPI,
appThemeViewModel = appThemeViewModel,
localSave = localSaver)
}
}
13 changes: 10 additions & 3 deletions app/src/main/java/com/github/se/assocify/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import com.github.se.assocify.model.localsave.LoginSave
import com.github.se.assocify.model.localsave.LocalSave
import com.github.se.assocify.ui.theme.AssocifyTheme
import com.github.se.assocify.ui.theme.ThemeViewModel

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
val themeVM = ThemeViewModel()
val localSave = LocalSave(this, themeVM)

setContent {
AssocifyTheme {
val theme by themeVM.theme.collectAsState()
AssocifyTheme(theme = theme) {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
AssocifyApp(LoginSave(this))
AssocifyApp(localSave, themeVM)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ data class UserPreference(
enum class Theme {
LIGHT,
DARK,
SYSTEM
SYSTEM;

companion object {
fun fromString(value: String?): Theme {
return when (value) {
"LIGHT" -> LIGHT
"DARK" -> DARK
else -> SYSTEM
}
}
}
}

/** All the languages supported by the application */
Expand Down
Loading
Loading