Skip to content

Commit f9cada5

Browse files
authored
2.1
2 parents 8b157b6 + f79e754 commit f9cada5

File tree

21 files changed

+448
-31
lines changed

21 files changed

+448
-31
lines changed

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/GPS/release/app-GPS-release 2.aab

4.73 MB
Binary file not shown.

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
applicationId 'com.saverio.wordoftheday_en'
1313
minSdkVersion 21
1414
targetSdkVersion 34
15-
versionCode 17
16-
versionName '2.0'
15+
versionCode 18
16+
versionName '2.1'
1717
multiDexEnabled true
1818

1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

app/src/main/java/com/saverio/wordoftheday_en/LoadWord.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ class LoadWord {
4242
.build()
4343

4444
val jsonAPI = retrofit.create(jsonAPI::class.java)
45-
val mcall: Call<Model> = jsonAPI.getInfo()
45+
46+
val language = context.getSharedPreferences("language", Context.MODE_PRIVATE)
47+
.getString("language", "en") ?: "en"
48+
49+
val mcall: Call<Model> = jsonAPI.getInfo(language)
4650
try {
4751
mcall.enqueue(
4852
object : Callback<Model> {
@@ -70,6 +74,11 @@ class LoadWord {
7074
setDataOffline(context, "etymology", model.etymology)
7175
setDataOffline(context, "source", model.source)
7276

77+
val currentLanguage = context.getSharedPreferences(
78+
"language",
79+
Context.MODE_PRIVATE
80+
).getString("language", "en") ?: "en"
81+
7382
if (mainActivity != null) {
7483
mainActivity.hideMessage()
7584
mainActivity.setAllFields(
@@ -79,7 +88,7 @@ class LoadWord {
7988
model.phonetics,
8089
model.word_type,
8190
model.etymology,
82-
model.source
91+
model.source,
8392
)
8493
}
8594

@@ -90,13 +99,15 @@ class LoadWord {
9099
wordType = model.word_type,
91100
phonetics = model.phonetics,
92101
etymology = model.etymology,
93-
source = model.source
102+
source = model.source,
103+
language = currentLanguage
94104
)
95105

96106
// Store in Room
97107
val wordDao = WordDatabase.getDatabase(context).wordDao()
98108
CoroutineScope(Dispatchers.IO).launch {
99-
val existingWordCount = wordDao.countWord(model.word)
109+
val existingWordCount =
110+
wordDao.countWord(model.word, currentLanguage)
100111

101112
if (existingWordCount == 0) {
102113
// Only insert if the word does not exist
@@ -121,7 +132,7 @@ class LoadWord {
121132
notificationNumber
122133
)
123134
}
124-
} else if (model != null && model.date == "null") {
135+
} else if (model != null && model.date === "null") {
125136
//no word
126137
if (attempsTemp <= maxAttempts) {
127138
loadWord(

app/src/main/java/com/saverio/wordoftheday_en/MainActivity.kt

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ class MainActivity : AppCompatActivity() {
4040
super.onCreate(savedInstanceState)
4141
setContentView(R.layout.activity_main)
4242

43+
//The first time ever the app is launched, set the language to the device's language
44+
if (getSharedPreferences("language", Context.MODE_PRIVATE).getString(
45+
"language",
46+
""
47+
) == ""
48+
) {
49+
//get the device's language
50+
val language = Locale.getDefault().language
51+
52+
//check the language is supported
53+
val languages = allLanguages()
54+
if (!languages.getLanguages().contains(language)) {
55+
getSharedPreferences("language", Context.MODE_PRIVATE).edit()
56+
.putString("language", "en").apply()
57+
} else {
58+
getSharedPreferences("language", Context.MODE_PRIVATE).edit()
59+
.putString("language", language).apply()
60+
}
61+
}
62+
4363
checkNetwork()
4464

4565
setSettingsNotChanged()
@@ -70,6 +90,18 @@ class MainActivity : AppCompatActivity() {
7090
wordHistoryButton.isGone = false
7191
}
7292

93+
override fun onResume() {
94+
super.onResume()
95+
96+
checkSettingsChanged()
97+
}
98+
99+
override fun onRestart() {
100+
super.onRestart()
101+
102+
checkSettingsChanged()
103+
}
104+
73105
fun loadWord() {
74106
if (isConnected()) {
75107
loadingWordMessage()
@@ -201,8 +233,25 @@ class MainActivity : AppCompatActivity() {
201233
}
202234

203235
fun setSettingsNotChanged() {
204-
getSharedPreferences("settings_changes", Context.MODE_PRIVATE).edit()
205-
.putBoolean("settings_changes", false).apply()
236+
//getSharedPreferences("settings_changes", Context.MODE_PRIVATE).edit() .putBoolean("settings_changes", false).apply()
237+
}
238+
239+
fun checkSettingsChanged() {
240+
//load the variable "restartRequired" from the sharedPreferences
241+
val restartRequired = getSharedPreferences("settings_changes", Context.MODE_PRIVATE)
242+
.getBoolean("settings_changes", false)
243+
244+
//if the variable is true, finish the activity and start it again
245+
if (restartRequired) {
246+
//set the variable to false
247+
getSharedPreferences("settings_changes", Context.MODE_PRIVATE).edit()
248+
.putBoolean("settings_changes", false).apply()
249+
250+
// Restart the activity
251+
val intent = Intent(this, MainActivity::class.java)
252+
finish()
253+
startActivity(intent)
254+
}
206255
}
207256

208257
fun isConnected(): Boolean {

app/src/main/java/com/saverio/wordoftheday_en/Settings.kt

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@ import android.content.Context
55
import android.content.Intent
66
import android.net.Uri
77
import android.os.Bundle
8-
import android.provider.ContactsContract.CommonDataKinds.Im
98
import android.view.MotionEvent
9+
import android.widget.AdapterView
10+
import android.widget.ArrayAdapter
1011
import android.widget.ImageView
12+
import android.widget.Spinner
13+
import android.content.res.Configuration
14+
import android.os.Build
1115
import android.widget.Switch
1216
import android.widget.TextView
1317
import androidx.appcompat.app.AppCompatActivity
1418
import androidx.constraintlayout.widget.ConstraintLayout
1519
import androidx.core.view.isGone
20+
import androidx.multidex.BuildConfig
21+
import com.saverio.wordoftheday_en.allLanguages
22+
import java.util.Locale
1623

1724
class Settings : AppCompatActivity() {
25+
26+
var ableToChangeLanguage = false
27+
1828
@SuppressLint("ClickableViewAccessibility")
1929
override fun onCreate(savedInstanceState: Bundle?) {
2030
super.onCreate(savedInstanceState)
@@ -83,6 +93,8 @@ class Settings : AppCompatActivity() {
8393
checkSettingsChanged()
8494

8595
loadButtons()
96+
97+
loadLanguages()
8698
}
8799

88100
fun loadButtons() {
@@ -92,6 +104,92 @@ class Settings : AppCompatActivity() {
92104
}
93105
}
94106

107+
fun loadLanguages() {
108+
this.ableToChangeLanguage = false
109+
// Initialize spinner and language data
110+
val languageContainer: Spinner = findViewById(R.id.settingsLanguageSpinner)
111+
val allLanguages = allLanguages()
112+
val languages = allLanguages.getLanguages() // List of all languages
113+
val currentLanguage = getCurrentLanguage() // Current language code
114+
val languageNames = allLanguages.getLanguageNames() // List of all language names
115+
val languageName =
116+
allLanguages.getLanguageName(currentLanguage) // Display name of the current language
117+
val languageNameIndex =
118+
allLanguages.getIndexOfLanguage(currentLanguage) // Index of the current language
119+
120+
// Set up the ArrayAdapter for the Spinner
121+
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, languageNames)
122+
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
123+
languageContainer.adapter = adapter
124+
125+
// Set Spinner to display current language
126+
if (languageNameIndex >= 0) {
127+
this.ableToChangeLanguage = true
128+
languageContainer.setSelection(languageNameIndex)
129+
} else {
130+
//println("Language name index not found")
131+
languageContainer.setSelection(0)
132+
this.ableToChangeLanguage = true
133+
}
134+
135+
// Listen for spinner selection changes
136+
languageContainer.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
137+
override fun onItemSelected(
138+
parent: AdapterView<*>,
139+
view: android.view.View,
140+
position: Int,
141+
id: Long
142+
) {
143+
if (ableToChangeLanguage) {
144+
// Check if the language has changed
145+
if (languages[position] != currentLanguage) {
146+
setSettingsChanged()
147+
checkSettingsChanged()
148+
149+
// Save selected language to SharedPreferences
150+
val selectedLanguage = languages[position]
151+
getSharedPreferences("language", Context.MODE_PRIVATE).edit()
152+
.putString("language", selectedLanguage).apply()
153+
154+
// Set also the app language
155+
setAppLocale(this@Settings, selectedLanguage)
156+
157+
// Set variable "restartRequiredMain" to true
158+
setSettingsChanged()
159+
160+
// Restart the activity
161+
finish()
162+
startActivity(intent)
163+
}
164+
}
165+
}
166+
167+
override fun onNothingSelected(parent: AdapterView<*>) {
168+
// Handle case when nothing is selected if needed
169+
}
170+
}
171+
}
172+
173+
fun setAppLocale(context: Context, languageCode: String) {
174+
val locale = Locale(languageCode)
175+
Locale.setDefault(locale)
176+
177+
val config = Configuration(context.resources.configuration)
178+
config.setLocale(locale)
179+
180+
// Update the configuration for the app's resources
181+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
182+
context.resources.updateConfiguration(config, context.resources.displayMetrics)
183+
context.createConfigurationContext(config) // For API 24+
184+
} else {
185+
context.resources.updateConfiguration(config, context.resources.displayMetrics)
186+
}
187+
}
188+
189+
fun getCurrentLanguage(): String {
190+
return getSharedPreferences("language", Context.MODE_PRIVATE).getString("language", "en")!!
191+
}
192+
95193
/*
96194
fun getAds(): Boolean {
97195
return getSharedPreferences("ads", Context.MODE_PRIVATE).getBoolean("ads", true)

app/src/main/java/com/saverio/wordoftheday_en/Word.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ data class Word(
1212
val wordType: String,
1313
val phonetics: String,
1414
val etymology: String,
15-
val source: String
15+
val source: String,
16+
val language: String
1617
)

app/src/main/java/com/saverio/wordoftheday_en/WordDao.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ interface WordDao {
88
@Insert
99
suspend fun insertWord(word: Word) // Now a suspend function
1010

11-
@Query("SELECT COUNT(*) FROM word_history WHERE word = :word")
12-
suspend fun countWord(word: String): Int // Method to count occurrences of a word
11+
@Query("SELECT COUNT(*) FROM word_history WHERE word = :word AND language = :language")
12+
suspend fun countWord(word: String, language: String): Int // Method to count occurrences of a word
1313

1414
// Fetch all words ordered by 'id' in descending order (latest to oldest)
15-
@Query("SELECT * FROM word_history ORDER BY date DESC")
16-
suspend fun getAllWords(): List<Word> // Now a suspend function
15+
@Query("SELECT * FROM word_history WHERE language = :language ORDER BY date DESC")
16+
suspend fun getAllWords(language: String): List<Word> // Now a suspend function
1717
}

app/src/main/java/com/saverio/wordoftheday_en/WordDatabase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract class WordDatabase : RoomDatabase() {
1919

2020
context.applicationContext,
2121
WordDatabase::class.java,
22-
"word_database"
22+
"words_learnt_database"
2323
).build()
2424
INSTANCE = instance
2525
instance

0 commit comments

Comments
 (0)