-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added initial theming system + AMOLED mode (based on Tachiyomi)
refactor: Started to adopt dependency injection pattern
- Loading branch information
1 parent
30801a8
commit b33dc97
Showing
63 changed files
with
1,846 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/main/java/me/timschneeberger/rootlessjamesdsp/activity/BaseActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package me.timschneeberger.rootlessjamesdsp.activity | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.app.ActivityCompat | ||
import me.timschneeberger.rootlessjamesdsp.R | ||
import me.timschneeberger.rootlessjamesdsp.delegates.ThemingDelegate | ||
import me.timschneeberger.rootlessjamesdsp.delegates.ThemingDelegateImpl | ||
import me.timschneeberger.rootlessjamesdsp.utils.Constants | ||
|
||
open class BaseActivity : | ||
AppCompatActivity(), | ||
SharedPreferences.OnSharedPreferenceChangeListener, | ||
ThemingDelegate by ThemingDelegateImpl() { | ||
|
||
protected val appPref: SharedPreferences by lazy { | ||
getSharedPreferences(Constants.PREF_APP, Context.MODE_PRIVATE) | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
applyAppTheme(this) | ||
appPref.registerOnSharedPreferenceChangeListener(this) | ||
super.onCreate(savedInstanceState) | ||
} | ||
|
||
override fun onDestroy() { | ||
appPref.unregisterOnSharedPreferenceChangeListener(this) | ||
super.onDestroy() | ||
} | ||
|
||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { | ||
if(key == getString(R.string.key_appearance_pure_black) || | ||
key == getString(R.string.key_appearance_app_theme)) { | ||
ActivityCompat.recreate(this) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 2 additions & 5 deletions
7
app/src/main/java/me/timschneeberger/rootlessjamesdsp/activity/SettingsActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/src/main/java/me/timschneeberger/rootlessjamesdsp/adapter/ThemesPreferenceAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package me.timschneeberger.rootlessjamesdsp.adapter | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.appcompat.view.ContextThemeWrapper | ||
import androidx.recyclerview.widget.RecyclerView | ||
import me.timschneeberger.rootlessjamesdsp.R | ||
import me.timschneeberger.rootlessjamesdsp.databinding.PreferenceThemeItemBinding | ||
import me.timschneeberger.rootlessjamesdsp.delegates.ThemingDelegate | ||
import me.timschneeberger.rootlessjamesdsp.model.preference.AppTheme | ||
import me.timschneeberger.rootlessjamesdsp.utils.Constants | ||
import me.timschneeberger.rootlessjamesdsp.utils.ContextExtensions.getResourceColor | ||
|
||
class ThemesPreferenceAdapter(private val context: Context, | ||
private val clickListener: OnItemClickListener) : | ||
RecyclerView.Adapter<ThemesPreferenceAdapter.ThemeViewHolder>() { | ||
|
||
private var themes = emptyList<AppTheme>() | ||
private val preferences by lazy { context.getSharedPreferences(Constants.PREF_APP, Context.MODE_PRIVATE) } | ||
|
||
private lateinit var binding: PreferenceThemeItemBinding | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ThemeViewHolder { | ||
val isAmoled = preferences.getBoolean(context.getString(R.string.key_appearance_pure_black), false) | ||
val themeResIds = ThemingDelegate.getThemeResIds(themes[viewType], isAmoled) | ||
val themedContext = themeResIds.fold(parent.context) { | ||
context, themeResId -> | ||
ContextThemeWrapper(context, themeResId) | ||
} | ||
|
||
binding = PreferenceThemeItemBinding.inflate(LayoutInflater.from(themedContext), parent, false) | ||
return ThemeViewHolder(binding.root) | ||
} | ||
|
||
override fun getItemViewType(position: Int): Int = position | ||
|
||
override fun getItemCount(): Int = themes.size | ||
|
||
override fun onBindViewHolder(holder: ThemesPreferenceAdapter.ThemeViewHolder, position: Int) { | ||
holder.bind(themes[position]) | ||
} | ||
|
||
@SuppressLint("NotifyDataSetChanged") | ||
fun setItems(themes: List<AppTheme>) { | ||
this.themes = themes | ||
notifyDataSetChanged() | ||
} | ||
|
||
inner class ThemeViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { | ||
|
||
private val selectedColor = view.context.getResourceColor(com.google.android.material.R.attr.colorPrimary) | ||
private val unselectedColor = view.context.getResourceColor(android.R.attr.divider) | ||
|
||
fun bind(appTheme: AppTheme) { | ||
binding.name.text = view.context.getString(appTheme.titleResId!!) | ||
|
||
// For rounded corners | ||
binding.badges.clipToOutline = true | ||
|
||
val storedAppTheme = AppTheme.valueOf(preferences.getString(context.getString(R.string.key_appearance_app_theme), AppTheme.DEFAULT.name) ?: AppTheme.DEFAULT.name) | ||
val isSelected = storedAppTheme == appTheme | ||
binding.themeCard.isChecked = isSelected | ||
binding.themeCard.strokeColor = if (isSelected) selectedColor else unselectedColor | ||
|
||
listOf(binding.root, binding.themeCard).forEach { | ||
it.setOnClickListener { | ||
clickListener.onItemClick(bindingAdapterPosition) | ||
} | ||
} | ||
} | ||
} | ||
|
||
interface OnItemClickListener { | ||
fun onItemClick(position: Int) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
app/src/main/java/me/timschneeberger/rootlessjamesdsp/delegates/ThemingDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package me.timschneeberger.rootlessjamesdsp.delegates | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import me.timschneeberger.rootlessjamesdsp.R | ||
import me.timschneeberger.rootlessjamesdsp.model.preference.AppTheme | ||
import me.timschneeberger.rootlessjamesdsp.utils.Constants | ||
|
||
interface ThemingDelegate { | ||
fun applyAppTheme(activity: Activity) | ||
|
||
companion object { | ||
fun getThemeResIds(appTheme: AppTheme, isAmoled: Boolean): List<Int> { | ||
val resIds = mutableListOf<Int>() | ||
when (appTheme) { | ||
AppTheme.MONET -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_Monet | ||
} | ||
AppTheme.GREEN_APPLE -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_GreenApple | ||
} | ||
AppTheme.LAVENDER -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_Lavender | ||
} | ||
AppTheme.MIDNIGHT_DUSK -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_MidnightDusk | ||
} | ||
AppTheme.STRAWBERRY_DAIQUIRI -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_StrawberryDaiquiri | ||
} | ||
AppTheme.TAKO -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_Tako | ||
} | ||
AppTheme.TEALTURQUOISE -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_TealTurquoise | ||
} | ||
AppTheme.YINYANG -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_YinYang | ||
} | ||
AppTheme.YOTSUBA -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_Yotsuba | ||
} | ||
AppTheme.TIDAL_WAVE -> { | ||
resIds += R.style.Theme_RootlessJamesDSP_TidalWave | ||
} | ||
else -> { | ||
resIds += R.style.Theme_RootlessJamesDSP | ||
} | ||
} | ||
|
||
if (isAmoled) { | ||
resIds += R.style.ThemeOverlay_RootlessJamesDSP_Amoled | ||
} | ||
|
||
return resIds | ||
} | ||
} | ||
} | ||
|
||
// TODO centralize preferences | ||
class ThemingDelegateImpl : ThemingDelegate { | ||
override fun applyAppTheme(activity: Activity) { | ||
val preferences = activity.getSharedPreferences(Constants.PREF_APP, Context.MODE_PRIVATE) | ||
val isAmoled = preferences.getBoolean(activity.getString(R.string.key_appearance_pure_black), false) | ||
val appTheme = AppTheme.valueOf(preferences.getString(activity.getString(R.string.key_appearance_app_theme), AppTheme.DEFAULT.name) ?: AppTheme.DEFAULT.name) | ||
ThemingDelegate.getThemeResIds(appTheme, isAmoled) | ||
.forEach { activity.setTheme(it) } | ||
} | ||
} |
Oops, something went wrong.