Skip to content

Commit

Permalink
Close #69 - Implement multi language support
Browse files Browse the repository at this point in the history
  • Loading branch information
isoguzay committed Mar 15, 2022
1 parent d31cb87 commit 596724b
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.compose.runtime.Composable
import androidx.navigation.*
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable

import com.theteampotato.gifit.favorites.view.FavoritesScreen
import com.theteampotato.gifit.favorites.viewmodel.FavoritesViewModel
import com.theteampotato.gifit.history.view.HistoryScreen
Expand All @@ -15,6 +14,7 @@ import com.theteampotato.gifit.language_selection.view.LanguageSelectionScreen
import com.theteampotato.gifit.language_selection.viewmodel.LanguageSelectionViewModel
import com.theteampotato.gifit.ui.BottomNavScreen
import com.theteampotato.gifit.view.HomeScaffold
import com.theteampotato.gifit.view.LocaleActiveManager

const val LANGUAGE_SELECTION = "language_selection"

Expand All @@ -26,8 +26,9 @@ fun NavigationGraph(
languageSelectionViewModel: LanguageSelectionViewModel,
searchViewModel: SearchViewModel,
historyViewModel: HistoryViewModel,
favoritesViewModel: FavoritesViewModel,
favoritesViewModel: FavoritesViewModel
) {
val localeManager = LocaleActiveManager.current
NavHost(navController = navController, startDestination = startDestination) {
composable(LANGUAGE_SELECTION) {
LanguageSelectionScreen(
Expand All @@ -36,7 +37,8 @@ fun NavigationGraph(
popUpTo(navController.currentDestination?.route!!) { inclusive = true }
}
},
viewModel = languageSelectionViewModel
viewModel = languageSelectionViewModel,
localeManager = localeManager
)
}
composable(BottomNavScreen.FavoritesNavScreen.route) {
Expand Down
37 changes: 22 additions & 15 deletions app/src/main/kotlin/com/theteampotato/gifit/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.theteampotato.gifit.data.LocaleManager
import com.theteampotato.gifit.data.datastore.preferencesDataStore
import com.theteampotato.gifit.favorites.viewmodel.FavoritesViewModel
import com.theteampotato.gifit.history.viewmodel.HistoryViewModel
import com.theteampotato.gifit.home.viewmodel.SearchViewModel
Expand All @@ -30,6 +29,8 @@ import com.theteampotato.gifit.ui.view.GIFitBottomNavBar
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay

val LocaleActiveManager = compositionLocalOf<LocaleManager> { error("No locale manager found!") }

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

Expand All @@ -39,9 +40,13 @@ class MainActivity : AppCompatActivity() {
private val searchViewModel: SearchViewModel by viewModels()
private val splashViewModel: SplashViewModel by viewModels()

lateinit var localeManager: LocaleManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

localeManager = LocaleManager(this, preferencesDataStore)

var keepSplashScreen = true
var selectedLanguage: String?

Expand All @@ -52,24 +57,26 @@ class MainActivity : AppCompatActivity() {
}

lifecycleScope.launchWhenCreated {
//splashViewModel.setIsSelectedLanguage(true)
localeManager.setLocale()

selectedLanguage = splashViewModel.getIsSelectedLanguage()

delay(2000)
keepSplashScreen = false

setContent {
val navController = rememberNavController()
GIFitTheme {
NavigationGraph(
navController = navController,
startDestination = getStartDestination(selectedLanguage != null),
languageSelectionViewModel = languageSelectionViewModel,
searchViewModel = searchViewModel,
historyViewModel = historyViewModel,
favoritesViewModel = favoritesViewModel
)
CompositionLocalProvider(LocaleActiveManager provides localeManager) {
val navController = rememberNavController()
GIFitTheme {
NavigationGraph(
navController = navController,
startDestination = getStartDestination(selectedLanguage != null),
languageSelectionViewModel = languageSelectionViewModel,
searchViewModel = searchViewModel,
historyViewModel = historyViewModel,
favoritesViewModel = favoritesViewModel
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.*
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -16,18 +19,24 @@ import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import androidx.hilt.navigation.compose.hiltViewModel

import com.theteampotato.gifit.data.LocaleManager
import com.theteampotato.gifit.language_selection.R
import com.theteampotato.gifit.language_selection.viewmodel.LanguageSelectionViewModel
import com.theteampotato.gifit.ui.babyBlue
import com.theteampotato.gifit.ui.view.GIFitLanguageCard
import com.theteampotato.gifit.ui.view.GIFitTranslateModelLoader

import kotlinx.coroutines.launch
import timber.log.Timber

@Composable
fun LanguageSelectionScreen(modifier: Modifier = Modifier, navigateToSearch: () -> Unit = {}, viewModel: LanguageSelectionViewModel = hiltViewModel()) {
val supportedLanguageList = remember { mutableStateOf(getSupportedLanguages().sortedBy { it.name }) }
fun LanguageSelectionScreen(
modifier: Modifier = Modifier,
navigateToSearch: () -> Unit = {},
viewModel: LanguageSelectionViewModel = hiltViewModel(),
localeManager: LocaleManager
) {
val supportedLanguageList =
remember { mutableStateOf(getSupportedLanguages().sortedBy { it.name }) }

val selectedIndex = remember { mutableStateOf(-1) }
val isContinueSelected = remember { mutableStateOf(false) }
Expand Down Expand Up @@ -75,13 +84,19 @@ fun LanguageSelectionScreen(modifier: Modifier = Modifier, navigateToSearch: ()
}) {
itemsIndexed(supportedLanguageList.value) { index, supportedLanguage ->
supportedLanguage.apply {
GIFitLanguageCard(text = name, painter = painterResource(iconResourceId), isSelected = selectedIndex.value == index, onClicked = {
selectedIndex.value = index
})
GIFitLanguageCard(
text = name,
painter = painterResource(iconResourceId),
isSelected = selectedIndex.value == index,
onClicked = {
selectedIndex.value = index
})
}
}
}

val coroutineScope = rememberCoroutineScope()

if (selectedIndex.value != -1) {
Column(
modifier = Modifier
Expand All @@ -104,7 +119,11 @@ fun LanguageSelectionScreen(modifier: Modifier = Modifier, navigateToSearch: ()
onSuccess = {
Timber.d("onSuccess()")
isContinueSelected.value = false
selectedSourceLanguage.value = supportedLanguageList.value[selectedIndex.value].languageCode
selectedSourceLanguage.value =
supportedLanguageList.value[selectedIndex.value].languageCode
coroutineScope.launch {
localeManager.setNewLocale(selectedSourceLanguage.value!!)
}
},
onFailure = {
Timber.d("onFailure()")
Expand Down Expand Up @@ -140,17 +159,69 @@ enum class Language(val languageCode: String) {
}

private fun getSupportedLanguages() = listOf(
SupportedLanguage(name = "Finnish", iconResourceId = R.drawable.ic_finland, languageCode = Language.FINNISH.languageCode),
SupportedLanguage(name = "Chinese", iconResourceId = R.drawable.ic_china, languageCode = Language.CHINESE.languageCode),
SupportedLanguage(name = "Danish", iconResourceId = R.drawable.ic_denmark, languageCode = Language.DANISH.languageCode),
SupportedLanguage(name = "French", iconResourceId = R.drawable.ic_france, languageCode = Language.FRENCH.languageCode),
SupportedLanguage(name = "German", iconResourceId = R.drawable.ic_germany, languageCode = Language.GERMAN.languageCode),
SupportedLanguage(name = "Spanish", iconResourceId = R.drawable.ic_spain, languageCode = Language.SPANISH.languageCode),
SupportedLanguage(name = "Turkish", iconResourceId = R.drawable.ic_turkey, languageCode = Language.TURKISH.languageCode),
SupportedLanguage(name = "Italian", iconResourceId = R.drawable.ic_italy, languageCode = Language.ITALIAN.languageCode),
SupportedLanguage(name = "Japanese", iconResourceId = R.drawable.ic_japan, languageCode = Language.JAPANESE.languageCode),
SupportedLanguage(name = "Dutch", iconResourceId = R.drawable.ic_netherlands, languageCode = Language.DUTCH.languageCode),
SupportedLanguage(name = "Norwegian", iconResourceId = R.drawable.ic_norway, languageCode = Language.NORWEGIAN.languageCode),
SupportedLanguage(name = "Korean", iconResourceId = R.drawable.ic_south_korea, languageCode = Language.KOREAN.languageCode),
SupportedLanguage(name = "Swedish", iconResourceId = R.drawable.ic_sweden, languageCode = Language.SWEDISH.languageCode)
SupportedLanguage(
name = "Finnish",
iconResourceId = R.drawable.ic_finland,
languageCode = Language.FINNISH.languageCode
),
SupportedLanguage(
name = "Chinese",
iconResourceId = R.drawable.ic_china,
languageCode = Language.CHINESE.languageCode
),
SupportedLanguage(
name = "Danish",
iconResourceId = R.drawable.ic_denmark,
languageCode = Language.DANISH.languageCode
),
SupportedLanguage(
name = "French",
iconResourceId = R.drawable.ic_france,
languageCode = Language.FRENCH.languageCode
),
SupportedLanguage(
name = "German",
iconResourceId = R.drawable.ic_germany,
languageCode = Language.GERMAN.languageCode
),
SupportedLanguage(
name = "Spanish",
iconResourceId = R.drawable.ic_spain,
languageCode = Language.SPANISH.languageCode
),
SupportedLanguage(
name = "Turkish",
iconResourceId = R.drawable.ic_turkey,
languageCode = Language.TURKISH.languageCode
),
SupportedLanguage(
name = "Italian",
iconResourceId = R.drawable.ic_italy,
languageCode = Language.ITALIAN.languageCode
),
SupportedLanguage(
name = "Japanese",
iconResourceId = R.drawable.ic_japan,
languageCode = Language.JAPANESE.languageCode
),
SupportedLanguage(
name = "Dutch",
iconResourceId = R.drawable.ic_netherlands,
languageCode = Language.DUTCH.languageCode
),
SupportedLanguage(
name = "Norwegian",
iconResourceId = R.drawable.ic_norway,
languageCode = Language.NORWEGIAN.languageCode
),
SupportedLanguage(
name = "Korean",
iconResourceId = R.drawable.ic_south_korea,
languageCode = Language.KOREAN.languageCode
),
SupportedLanguage(
name = "Swedish",
iconResourceId = R.drawable.ic_sweden,
languageCode = Language.SWEDISH.languageCode
)
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.theteampotato.gifit.home.view

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel

import com.theteampotato.gifit.home.R
import com.theteampotato.gifit.home.viewmodel.SearchViewModel
import com.theteampotato.gifit.ui.view.*

import com.theteampotato.gifit.ui.view.GIFitLoader
import com.theteampotato.gifit.ui.view.GIFitSearchBar
import com.theteampotato.gifit.ui.view.ResultCard
import timber.log.Timber

@Composable
Expand All @@ -22,7 +25,8 @@ fun SearchScreen(searchTextArgument: String? = null, viewModel: SearchViewModel

val isFavoriteState = rememberSaveable() { mutableStateOf(false) }

val searchResult = viewModel.searchKeyword(text = searchText.value.ifBlank { null })?.collectAsState(null)
val searchResult =
viewModel.searchKeyword(text = searchText.value.ifBlank { null })?.collectAsState(null)

LaunchedEffect(key1 = searchTextArgument) {
Timber.d("Composition")
Expand All @@ -35,13 +39,18 @@ fun SearchScreen(searchTextArgument: String? = null, viewModel: SearchViewModel

Column {
GIFitSearchBar(
modifier = Modifier.padding(top = 35.dp, start = horizontalPadding, end = horizontalPadding),
modifier = Modifier.padding(
top = 35.dp,
start = horizontalPadding,
end = horizontalPadding
),
text = searchBarText.value,
onSearchQueryChanged = { searchBarText.value = it },
onSearchQueryEntered = {
searchText.value = searchBarText.value
isSearched.value = true
}
},
placeholderText = stringResource(id = R.string.search) + "..."
)

searchResult?.value?.let {
Expand All @@ -50,7 +59,11 @@ fun SearchScreen(searchTextArgument: String? = null, viewModel: SearchViewModel
isSearched.value = false

ResultCard(
modifier = Modifier.padding(top = 15.dp, start = horizontalPadding, end = horizontalPadding),
modifier = Modifier.padding(
top = 15.dp,
start = horizontalPadding,
end = horizontalPadding
),
imageURL = it.gifURL ?: "",
translatedText = it.translatedText,
isFavorite = it.isFavorite,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.theteampotato.gifit.data

enum class LanguageType(val languageCode: String) {
ENGLISH("en"),
FINNISH("fi"),
CHINESE("zh"),
DANISH("da"),
FRENCH("fr"),
GERMAN("de"),
SPANISH("es"),
TURKISH("tr"),
ITALIAN("it"),
JAPANESE("ja"),
DUTCH("nl"),
NORWEGIAN("no"),
KOREAN("ko"),
SWEDISH("sv")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import androidx.datastore.preferences.preferencesDataStore
val Context.preferencesDataStore: DataStore<Preferences> by preferencesDataStore(name = "preferences")

val SELECTED_LANGUAGE = stringPreferencesKey("selected_language")
val APP_LANGUAGE = stringPreferencesKey("app_language")
Loading

0 comments on commit 596724b

Please sign in to comment.