Skip to content

Commit

Permalink
Fix seance color can be too dark when dark mode is enabled (#196)
Browse files Browse the repository at this point in the history
Make the color lean toward white when dark theme is on or toward black when it's off
  • Loading branch information
Sonphil authored Aug 15, 2019
1 parent 3a87886 commit 0352a7e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ca.etsmtl.applets.etsmobile.extension

import android.Manifest
import android.content.Context
import android.content.res.Configuration
import android.net.ConnectivityManager
import android.util.TypedValue
import androidx.annotation.AttrRes
Expand All @@ -16,6 +17,16 @@ import model.UserCredentials
* Created by Sonphil on 18-05-18.
*/

/**
* Returns true when dark theme is on, false otherwise.
*/
inline val Context.isDarkMode: Boolean
get() {
val uiMode = resources.configuration.uiMode

return uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}

/**
* Check if the device is connected
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.etsmtl.applets.etsmobile.extension

import android.content.Context
import android.graphics.Color
import androidx.core.graphics.ColorUtils
import model.Seance
Expand All @@ -8,8 +9,16 @@ import model.Seance
* Created by Sonphil on 13-08-19.
*/

fun Seance.generateColor() = ColorUtils.blendARGB(
sigleCours.hashCode(),
Color.BLACK,
0.3f
)
fun Seance.generateColor(context: Context): Int {
val secondColor = if (context.isDarkMode) {
Color.WHITE
} else {
Color.BLACK
}

return ColorUtils.blendARGB(
sigleCours.hashCode(),
secondColor,
0.3f
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ class DashboardFragment : DaggerFragment() {

super.onPause()
}

override fun onDestroyView() {
rvCards.adapter = null

super.onDestroyView()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import android.transition.TransitionInflater
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.core.view.isVisible
import ca.etsmtl.applets.etsmobile.R
import ca.etsmtl.applets.etsmobile.extension.getColorCompat
import ca.etsmtl.applets.etsmobile.extension.isDarkMode
import ca.etsmtl.applets.etsmobile.extension.toast
import ca.etsmtl.applets.etsmobile.presentation.BaseActivity
import kotlinx.android.synthetic.main.activity_grades_details.*
Expand Down Expand Up @@ -71,7 +71,7 @@ class GradesDetailsActivity : BaseActivity() {

setupToolbar()

if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
if (isDarkMode) {
window.statusBarColor = getColorCompat(R.color.etsRougeClair)
}

Expand Down Expand Up @@ -133,7 +133,7 @@ class GradesDetailsActivity : BaseActivity() {
}
}

if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
if (isDarkMode) {
window.statusBarColor = getColorCompat(R.color.colorPrimaryDark)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ScheduleFragment : DaggerFragment() {
private fun Seance.toWeekViewEvent(): WeekViewEvent<Seance> {
val style = WeekViewEvent.Style
.Builder()
.setBackgroundColor(generateColor())
.setBackgroundColor(generateColor(requireContext()))
.build()

return WeekViewEvent.Builder<Seance>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ScheduleWeekInnerListAdapter : RecyclerView.Adapter<ScheduleWeekInnerListA
dateFin.timeInMilliseconds,
DateUtils.FORMAT_SHOW_TIME
)
holder.separatorSchedule.setBackgroundColor(generateColor())
holder.separatorSchedule.setBackgroundColor(generateColor(holder.itemView.context))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChan
private fun handleDarkThemePreferenceChange(newValue: Any?) {
if (newValue is String) {
context?.applyDarkThemePref(newValue)

activity?.recreate()
}
}

Expand Down

0 comments on commit 0352a7e

Please sign in to comment.