Dark theme refactor (#173)

This commit is contained in:
Mikołaj Pich 2018-10-30 22:06:29 +01:00 committed by Rafał Borcz
parent 745894f3a3
commit 0a7f09077f
16 changed files with 130 additions and 27 deletions

View File

@ -12,5 +12,8 @@ class PreferencesRepository @Inject constructor(private val sharedPref: SharedPr
val showPresent: Boolean
get() = sharedPref.getBoolean("attendance_present", true)
val currentTheme: Int
get() = sharedPref.getString("theme", "1")?.toInt() ?: 1
}

View File

@ -16,6 +16,7 @@ import io.github.wulkanowy.ui.modules.exam.ExamFragment
import io.github.wulkanowy.ui.modules.grade.GradeFragment
import io.github.wulkanowy.ui.modules.more.MoreFragment
import io.github.wulkanowy.ui.modules.timetable.TimetableFragment
import io.github.wulkanowy.utils.getThemeAttrColor
import io.github.wulkanowy.utils.safelyPopFragment
import io.github.wulkanowy.utils.setOnViewChangeListener
import kotlinx.android.synthetic.main.activity_main.*
@ -73,7 +74,8 @@ class MainActivity : BaseActivity(), MainView {
AHBottomNavigationItem(R.string.more_title, R.drawable.ic_menu_main_more_24dp, 0)
))
accentColor = ContextCompat.getColor(context, R.color.colorPrimary)
inactiveColor = ContextCompat.getColor(context, android.R.color.black)
inactiveColor = getThemeAttrColor(android.R.attr.textColorSecondary)
defaultBackgroundColor = getThemeAttrColor(R.attr.bottomNavBackground)
titleState = ALWAYS_SHOW
currentItem = startMenuIndex
isBehaviorTranslationEnabled = false

View File

@ -1,11 +1,14 @@
package io.github.wulkanowy.ui.modules.settings
import android.content.SharedPreferences
import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate
import com.takisoft.preferencex.PreferenceFragmentCompat
import io.github.wulkanowy.R
import io.github.wulkanowy.ui.modules.main.MainView
class SettingsFragment : PreferenceFragmentCompat(), MainView.TitledView {
class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener,
MainView.TitledView {
companion object {
fun newInstance() = SettingsFragment()
@ -17,4 +20,23 @@ class SettingsFragment : PreferenceFragmentCompat(), MainView.TitledView {
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.scheme_preferences)
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String) {
when(key) {
"theme" -> {
AppCompatDelegate.setDefaultNightMode(sharedPreferences?.getString("theme", "1")?.toInt() ?: 1)
activity?.recreate()
}
}
}
override fun onResume() {
super.onResume()
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
override fun onPause() {
super.onPause()
preferenceScreen.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
}
}

View File

@ -5,6 +5,7 @@ import io.github.wulkanowy.ui.base.BaseActivity
import io.github.wulkanowy.ui.modules.login.LoginActivity
import io.github.wulkanowy.ui.modules.main.MainActivity
import javax.inject.Inject
import androidx.appcompat.app.AppCompatDelegate
class SplashActivity : BaseActivity(), SplashView {
@ -26,6 +27,10 @@ class SplashActivity : BaseActivity(), SplashView {
finish()
}
override fun setCurrentThemeMode(mode: Int) {
AppCompatDelegate.setDefaultNightMode(mode)
}
override fun onDestroy() {
presenter.onDetachView()
super.onDestroy()

View File

@ -1,16 +1,22 @@
package io.github.wulkanowy.ui.modules.splash
import io.github.wulkanowy.data.ErrorHandler
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.data.repositories.SessionRepository
import io.github.wulkanowy.ui.base.BasePresenter
import javax.inject.Inject
class SplashPresenter @Inject constructor(private val sessionRepository: SessionRepository,
errorHandler: ErrorHandler)
: BasePresenter<SplashView>(errorHandler) {
class SplashPresenter @Inject constructor(
private val sessionRepository: SessionRepository,
private val preferencesRepository: PreferencesRepository,
errorHandler: ErrorHandler
) : BasePresenter<SplashView>(errorHandler) {
override fun onAttachView(view: SplashView) {
super.onAttachView(view)
view.run { if (sessionRepository.isSessionSaved) openMainView() else openLoginView() }
view.run {
setCurrentThemeMode(preferencesRepository.currentTheme)
if (sessionRepository.isSessionSaved) openMainView() else openLoginView()
}
}
}

View File

@ -7,4 +7,6 @@ interface SplashView : BaseView {
fun openLoginView()
fun openMainView()
fun setCurrentThemeMode(mode: Int)
}

View File

@ -0,0 +1,15 @@
package io.github.wulkanowy.utils
import android.content.Context
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
@ColorInt
fun Context.getThemeAttrColor(@AttrRes colorAttr: Int): Int {
val array = this.obtainStyledAttributes(null, intArrayOf(colorAttr))
try {
return array.getColor(0, 0)
} finally {
array.recycle()
}
}

View File

@ -1,4 +1,3 @@
<!-- drawable/calendar_text.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"

View File

@ -59,7 +59,7 @@
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="@android:color/white"
android:background="?android:attr/windowBackground"
android:elevation="10dp"
android:orientation="horizontal">

View File

@ -59,7 +59,7 @@
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="@android:color/white"
android:background="?android:attr/windowBackground"
android:elevation="10dp"
android:orientation="horizontal">

View File

@ -59,7 +59,7 @@
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="@android:color/white"
android:background="?android:attr/windowBackground"
android:elevation="10dp"
android:orientation="horizontal">

View File

@ -15,7 +15,8 @@
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
app:srcCompat="@drawable/ic_more_settings_24dp" />
app:srcCompat="@drawable/ic_more_settings_24dp"
android:tint="?android:attr/android:textColorSecondary"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/moreItemTitle"

View File

@ -0,0 +1,26 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="WulkanowyTheme" parent="@style/Theme.AppCompat.DayNight">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
<item name="bottomNavBackground">@color/bottom_nav_background</item>
<item name="android:navigationBarColor" tools:targetApi="21">@color/bottom_nav_background</item>
<!--AboutLibraries specific values-->
<item name="about_libraries_window_background">@color/about_libraries_window_background_dark</item>
<item name="about_libraries_card">@color/about_libraries_card_dark</item>
<item name="about_libraries_title_openSource">@color/about_libraries_title_openSource_dark</item>
<item name="about_libraries_title_description">@color/about_libraries_title_openSource_dark</item>
<item name="about_libraries_text_openSource">@color/about_libraries_text_openSource_dark</item>
<item name="about_libraries_special_button_openSource">@color/about_libraries_special_button_openSource_dark</item>
<item name="about_libraries_dividerDark_openSource">@color/about_libraries_dividerDark_openSource_dark</item>
<item name="about_libraries_dividerLight_openSource">@color/about_libraries_dividerLight_openSource_dark</item>
</style>
<style name="WulkanowyTheme.NoActionBar" parent="WulkanowyTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>

View File

@ -1,6 +1,6 @@
<resources>
<style name="WulkanowyTheme" parent="@style/Theme.AppCompat.Light">
<style name="WulkanowyTheme" parent="@style/Theme.AppCompat.DayNight">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
@ -15,6 +15,16 @@
<item name="android:colorBackground">@android:color/white</item>
<item name="bottomNavBackground">@color/bottom_nav_background_inverse</item>
<item name="android:windowAnimationStyle">@null</item>
<!-- AboutLibraries specific values -->
<item name="about_libraries_window_background">@color/about_libraries_window_background</item>
<item name="about_libraries_card">@color/about_libraries_card</item>
<item name="about_libraries_title_openSource">@color/about_libraries_title_openSource</item>
<item name="about_libraries_title_description">@color/about_libraries_title_openSource</item>
<item name="about_libraries_text_openSource">@color/about_libraries_text_openSource</item>
<item name="about_libraries_special_button_openSource">@color/about_libraries_special_button_openSource</item>
<item name="about_libraries_dividerDark_openSource">@color/about_libraries_dividerDark_openSource</item>
<item name="about_libraries_dividerLight_openSource">@color/about_libraries_dividerLight_openSource</item>
</style>
<style name="WulkanowyTheme.SplashScreen" parent="WulkanowyTheme.NoActionBar">

View File

@ -11,11 +11,19 @@
android:key="start_menu"
android:summary="%s"
android:title="@string/pref_view_list"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false"/>
<SwitchPreference
android:defaultValue="true"
android:key="attendance_present"
android:title="@string/pref_view_present"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false"/>
<ListPreference
android:defaultValue="1"
android:entries="@array/theme_entries"
android:entryValues="@array/theme_values"
android:key="theme"
android:summary="%s"
android:title="@string/pref_view_theme_dark"
app:iconSpaceReserved="false"/>
</PreferenceCategory>
</PreferenceScreen>

View File

@ -1,6 +1,7 @@
package io.github.wulkanowy.ui.modules.splash
import io.github.wulkanowy.data.ErrorHandler
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.data.repositories.SessionRepository
import org.junit.Before
import org.junit.Test
@ -20,12 +21,15 @@ class SplashPresenterTest {
@Mock
lateinit var errorHandler: ErrorHandler
@Mock
lateinit var preferencesRepository: PreferencesRepository
private lateinit var presenter: SplashPresenter
@Before
fun initPresenter() {
MockitoAnnotations.initMocks(this)
presenter = SplashPresenter(sessionRepository, errorHandler)
presenter = SplashPresenter(sessionRepository, preferencesRepository, errorHandler)
}
@Test