Theme switching

This commit is contained in:
2021-02-21 00:31:02 +03:00
parent 06aa41cab1
commit 35c45fd34f
34 changed files with 447 additions and 155 deletions
@@ -10,11 +10,14 @@ import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView
import com.meloda.fast.R
import com.meloda.fast.base.BaseFragment
import com.meloda.fast.fragment.ui.presenter.LoginPresenter
import com.meloda.fast.fragment.ui.view.LoginView
import com.meloda.fast.util.AndroidUtils
import com.meloda.fast.util.KeyboardUtils
import kotlin.math.roundToInt
class LoginFragment : BaseFragment(), LoginView {
@@ -23,6 +26,7 @@ class LoginFragment : BaseFragment(), LoginView {
private lateinit var email: EditText
private lateinit var password: EditText
private lateinit var authorize: MaterialButton
private lateinit var card: MaterialCardView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -48,12 +52,14 @@ class LoginFragment : BaseFragment(), LoginView {
email = requireView().findViewById(R.id.loginEmailEditText)
password = requireView().findViewById(R.id.loginPasswordEditText)
authorize = requireView().findViewById(R.id.loginAuthorize)
card = requireView().findViewById(R.id.loginCard)
}
override fun prepareViews() {
prepareEmailEditText()
preparePasswordEditText()
prepareAuthorizeButton()
prepareCardView()
}
private fun prepareEmailEditText() {
@@ -65,7 +71,7 @@ class LoginFragment : BaseFragment(), LoginView {
password.setOnEditorActionListener { _, _, event ->
if (event == null) return@setOnEditorActionListener false
return@setOnEditorActionListener if (event.action == EditorInfo.IME_ACTION_DONE ||
return@setOnEditorActionListener if (event.action == EditorInfo.IME_ACTION_GO ||
(event.action == KeyEvent.ACTION_DOWN && (event.keyCode == KeyEvent.KEYCODE_ENTER || event.keyCode == KeyEvent.KEYCODE_NUMPAD_ENTER))
) {
KeyboardUtils.hideKeyboardFrom(password)
@@ -85,6 +91,14 @@ class LoginFragment : BaseFragment(), LoginView {
}
}
private fun prepareCardView() {
val width = AndroidUtils.dp(resources.displayMetrics.widthPixels).roundToInt()
if (width < 380) {
card.strokeWidth = 0
}
}
override fun showErrorSnackbar(t: Throwable) {
TODO("Not yet implemented")
}
@@ -3,20 +3,19 @@ package com.meloda.fast.fragment
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen
import com.meloda.fast.R
import com.meloda.fast.activity.DropUserDataActivity
import com.meloda.fast.activity.UpdateActivity
import com.meloda.fast.base.BaseActivity
import com.meloda.fast.common.AppGlobal
import com.meloda.fast.common.TaskManager
import com.meloda.fast.extensions.ContextExtensions.color
import com.meloda.fast.extensions.ContextExtensions.drawable
import com.meloda.fast.util.AndroidUtils
class FragmentSettings : PreferenceFragmentCompat(),
class SettingsFragment : PreferenceFragmentCompat(),
Preference.OnPreferenceClickListener,
Preference.OnPreferenceChangeListener {
@@ -40,6 +39,7 @@ class FragmentSettings : PreferenceFragmentCompat(),
}
private var currentPreferenceLayout = 0
private var isRestoringState = false
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.fragment_settings, rootKey)
@@ -49,7 +49,6 @@ class FragmentSettings : PreferenceFragmentCompat(),
private fun init() {
setTitle()
setNavigationIcon()
setPreferencesFromResource(currentPreferenceLayout, null)
val general = findPreference<Preference>(CATEGORY_GENERAL)
@@ -100,14 +99,6 @@ class FragmentSettings : PreferenceFragmentCompat(),
private val rootLayoutClickListener =
Preference.OnPreferenceClickListener { changeRootLayout(it) }
private fun setNavigationIcon() {
val drawable =
if (currentPreferenceLayout == R.xml.fragment_settings) null
else requireContext().drawable(R.drawable.ic_arrow_back)
drawable?.setTint(requireContext().color(R.color.accent))
}
private fun setTitle() {
var title = R.string.navigation_settings
when (currentPreferenceLayout) {
@@ -189,14 +180,8 @@ class FragmentSettings : PreferenceFragmentCompat(),
return true
}
KEY_THEME -> {
AppGlobal.instance.applyNightMode(newValue as String)
(requireActivity() as BaseActivity).apply {
// applyNightMode()
finish()
startActivity(intent)
// recreate()
}
val nightMode = AppGlobal.instance.applyNightMode(newValue as String)
(requireActivity() as AppCompatActivity).delegate.localNightMode = nightMode
return true
}
}