Skip to content

Commit

Permalink
login: smoother mode handling (fixes #4879) (#4882)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
Co-authored-by: Gideon Okuro <[email protected]>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent f3c7b30 commit aab286b
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 37 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 26
targetSdkVersion 34
versionCode 2140
versionName "0.21.40"
versionCode 2141
versionName "0.21.41"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
77 changes: 50 additions & 27 deletions app/src/main/java/org/ole/planet/myplanet/ui/SettingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceChangeListener
Expand All @@ -23,6 +25,7 @@ import io.realm.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.ole.planet.myplanet.MainApplication.Companion.context
import org.ole.planet.myplanet.MainApplication.Companion.mRealm
import org.ole.planet.myplanet.MainApplication.Companion.setThemeMode
import org.ole.planet.myplanet.R
Expand Down Expand Up @@ -107,7 +110,7 @@ class SettingActivity : AppCompatActivity() {

val darkMode = findPreference<Preference>("dark_mode")
darkMode?.setOnPreferenceClickListener {
darkMode()
darkMode(requireActivity())
true
}

Expand Down Expand Up @@ -267,36 +270,56 @@ class SettingActivity : AppCompatActivity() {
dialog.show()
}

private fun darkMode() {
val options = arrayOf(getString(R.string.dark_mode_off), getString(R.string.dark_mode_on), getString(R.string.dark_mode_follow_system))
val currentMode = getCurrentThemeMode()
val checkedItem = when (currentMode) {
ThemeMode.LIGHT -> 0
ThemeMode.DARK -> 1
else -> 2
}
companion object {
fun darkMode(context: Context) {
val options = arrayOf(context.getString(R.string.dark_mode_off), context.getString(R.string.dark_mode_on),context.getString(R.string.dark_mode_follow_system))
val currentMode = getCurrentThemeMode(context)
val checkedItem = when (currentMode) {
ThemeMode.LIGHT -> 0
ThemeMode.DARK -> 1
else -> 2
}

val builder = AlertDialog.Builder(requireContext())
.setTitle(getString(R.string.select_theme_mode))
.setSingleChoiceItems(ArrayAdapter(requireContext(), R.layout.checked_list_item, options), checkedItem) { dialog, which ->
val selectedMode = when (which) {
0 -> ThemeMode.LIGHT
1 -> ThemeMode.DARK
2 -> ThemeMode.FOLLOW_SYSTEM
else -> ThemeMode.FOLLOW_SYSTEM
val builder = AlertDialog.Builder(context, R.style.CustomAlertDialogStyle)
.setTitle(context.getString(R.string.select_theme_mode))
.setSingleChoiceItems(ArrayAdapter(context, R.layout.checked_list_item, options), checkedItem) { dialog, which ->
val selectedMode = when (which) {
0 -> ThemeMode.LIGHT
1 -> ThemeMode.DARK
2 -> ThemeMode.FOLLOW_SYSTEM
else -> ThemeMode.FOLLOW_SYSTEM
}
setThemeMode(context, selectedMode)
dialog.dismiss()
}
setThemeMode(selectedMode)
dialog.dismiss()
}
.setNegativeButton(R.string.cancel, null)
.setNegativeButton(R.string.cancel, null)

val dialog = builder.create()
dialog.show()
}
val dialog = builder.create()
dialog.show()

private fun getCurrentThemeMode(): String {
val sharedPreferences = requireContext().getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
return sharedPreferences.getString("theme_mode", ThemeMode.FOLLOW_SYSTEM) ?: ThemeMode.FOLLOW_SYSTEM
val window = dialog.window
window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}

private fun getCurrentThemeMode(context: Context): String {
val sharedPreferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
return sharedPreferences.getString("theme_mode", ThemeMode.FOLLOW_SYSTEM) ?: ThemeMode.FOLLOW_SYSTEM
}

private fun setThemeMode(context: Context, themeMode: String) {
val sharedPreferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
with(sharedPreferences.edit()) {
putString("theme_mode", themeMode)
apply()
}
AppCompatDelegate.setDefaultNightMode(
when (themeMode) {
ThemeMode.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
ThemeMode.DARK -> AppCompatDelegate.MODE_NIGHT_YES
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.ole.planet.myplanet.callback.SyncListener
import org.ole.planet.myplanet.databinding.*
import org.ole.planet.myplanet.datamanager.*
import org.ole.planet.myplanet.model.*
import org.ole.planet.myplanet.ui.SettingActivity
import org.ole.planet.myplanet.ui.community.HomeCommunityDialogFragment
import org.ole.planet.myplanet.ui.feedback.FeedbackFragment
import org.ole.planet.myplanet.ui.userprofile.*
Expand Down Expand Up @@ -110,6 +111,10 @@ class LoginActivity : SyncActivity(), TeamListAdapter.OnItemClickListener {
}
}
})
val selectDarkModeButton = findViewById<ImageButton>(R.id.themeToggleButton)
selectDarkModeButton?.setOnClickListener{
SettingActivity.SettingFragment.darkMode(this)
}
}

private fun declareElements() {
Expand Down
Binary file added app/src/main/res/drawable/moon_24dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/moon_35.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/sun_24dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/sun_35.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/white_moon_24dp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 14 additions & 1 deletion app/src/main/res/layout-large-land/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,25 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton
android:id="@+id/themeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@null"
android:contentDescription="@string/select_theme_mode"
android:paddingStart="@dimen/_10dp"
android:paddingTop="6dp"
android:paddingEnd="@dimen/_10dp"
app:srcCompat="@drawable/sun_35"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/logoImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center|top"
android:layout_marginTop="20dp"
android:layout_marginTop="110dp"
android:contentDescription="@string/ole_logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/res/layout-night/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,27 @@

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp">
android:layout_height="match_parent">

<ImageButton
android:id="@+id/themeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@null"
android:contentDescription="@string/select_theme_mode"
android:paddingStart="@dimen/_10dp"
android:paddingTop="6dp"
android:paddingEnd="@dimen/_10dp"
app:srcCompat="@drawable/moon_35"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/logoImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center|top"
android:layout_marginTop="20dp"
android:layout_marginTop="100dp"
android:contentDescription="@string/ole_logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/res/layout-normal-land/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,25 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton
android:id="@+id/themeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@null"
android:contentDescription="@string/select_theme_mode"
android:paddingStart="@dimen/_10dp"
android:paddingTop="6dp"
android:paddingEnd="@dimen/_10dp"
app:srcCompat="@drawable/sun_35"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/logoImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center|top"
android:layout_marginTop="10dp"
android:layout_marginTop="110dp"
android:contentDescription="@string/ole_logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/layout-xlarge-land/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton
android:id="@+id/themeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@null"
android:contentDescription="@string/select_theme_mode"
android:paddingStart="@dimen/_10dp"
android:paddingTop="6dp"
android:paddingEnd="@dimen/_10dp"
app:srcCompat="@drawable/sun_35"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/logoImageView"
android:layout_width="100dp"
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,27 @@

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp">
android:layout_height="match_parent">

<ImageButton
android:id="@+id/themeToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="@null"
android:contentDescription="@string/select_theme_mode"
android:paddingStart="@dimen/_10dp"
android:paddingTop="6dp"
android:paddingEnd="@dimen/_10dp"
app:srcCompat="@drawable/sun_35"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/logoImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center|top"
android:layout_marginTop="20dp"
android:layout_marginTop="110dp"
android:contentDescription="@string/ole_logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/res/layout/custom_dialog_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- res/layout/custom_dialog_layout.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<!-- Add your dialog content here -->
<TextView
android:id="@+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog Title"
android:textSize="18sp"
android:textStyle="bold" />
<ListView
android:id="@+id/dialog_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="menu_logout">تسجيل الخروج</string>
<string name="menu_feedback">ملاحظات</string>
<string name="system_name">myPlanet</string>
<string name="select">يختار</string>
<string name="txt_myLibrary">مكتبتي</string>
<string name="txt_myCourses">دوراتي</string>
<string name="txt_myMeetups">تجمعاتي</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="menu_logout">Cerrar sesión</string>
<string name="menu_feedback">Retroalimentación</string>
<string name="system_name">myPlanet</string>
<string name="select">Seleccionar</string>
<string name="txt_myLibrary">miBiblioteca</string>
<string name="txt_myCourses">misCursos</string>
<string name="txt_myMeetups">misEncuentros</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="menu_logout">Déconnexion</string>
<string name="menu_feedback">Retour d\'information</string>
<string name="system_name">myPlanet</string>
<string name="select">Sélectionner</string>
<string name="txt_myLibrary">maBibliothèque</string>
<string name="txt_myCourses">mesCours</string>
<string name="txt_myMeetups">mesRencontres</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ne/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="menu_logout">लगआउट</string>
<string name="menu_feedback">प्रतिक्रिया</string>
<string name="system_name">myPlanet</string>
<string name="select">चयन गर्नुहोस्</string>
<string name="txt_myLibrary">मेरो पुस्तकालय</string>
<string name="txt_myCourses">मेरो पाठ्यक्रमहरू</string>
<string name="txt_myMeetups">मेरो मिठिङहरू</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-so/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<string name="menu_logout">ka bax</string>
<string name="menu_feedback">jawaab celin</string>
<string name="system_name">myPlanet</string>
<string name="select">Dooro</string>
<string name="txt_myLibrary">maktabadayda</string>
<string name="txt_myCourses">coursyadayda</string>
<string name="txt_myMeetups">myMeetups</string>
Expand Down

0 comments on commit aab286b

Please sign in to comment.