Skip to content

Commit

Permalink
Add ability to change theme via overflow menu
Browse files Browse the repository at this point in the history
  • Loading branch information
edwnmrtnz committed Jun 6, 2023
1 parent 76b14c7 commit 32e4112
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.edwnmrtnz.trendingrepo.app

import android.app.Application
import com.edwnmrtnz.trendingrepo.ui.ThemeChanger
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
Expand All @@ -11,5 +12,6 @@ class TrendyApplication : Application() {
override fun onCreate() {
super.onCreate()
PicassoInitializer.init(context = this)
ThemeChanger.set()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -66,8 +65,8 @@ class MainActivity : AppCompatActivity(), MainScreenView {

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_refresh -> {
Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show()
R.id.action_change_theme -> {
ThemeChanger.change()
true
}

Expand All @@ -88,7 +87,7 @@ class MainActivity : AppCompatActivity(), MainScreenView {
binding.sflMainShimmer.sflMainShimmer.visibility = View.GONE
binding.llEmptyStateContainer.visibility = View.VISIBLE

binding.tvEmptyStateTitle.text = getString(R.string.title_detault_error)
binding.tvEmptyStateTitle.text = getString(R.string.title_default_error)
binding.tvEmptyStateSubtitle.text = getString(R.string.subtitle_default_error)
return
}
Expand All @@ -104,7 +103,7 @@ class MainActivity : AppCompatActivity(), MainScreenView {
binding.sflMainShimmer.sflMainShimmer.visibility = View.GONE
binding.llEmptyStateContainer.visibility = View.VISIBLE

binding.tvEmptyStateTitle.text = getString(R.string.title_detault_error)
binding.tvEmptyStateTitle.text = getString(R.string.title_default_error)
binding.tvEmptyStateSubtitle.text = getString(R.string.subtitle_error_no_trending)
}
}
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/edwnmrtnz/trendingrepo/ui/ThemeChanger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.edwnmrtnz.trendingrepo.ui

import androidx.appcompat.app.AppCompatDelegate

object ThemeChanger {
private var isNight = false

fun set() {
isNight = false
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}

fun change() {
isNight = !isNight
AppCompatDelegate.setDefaultNightMode(
if (isNight) {
AppCompatDelegate.MODE_NIGHT_YES
} else {
AppCompatDelegate.MODE_NIGHT_NO
}
)
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
tools:context="com.edwnmrtnz.trendingrepo.ui.MainActivity">

<item
android:id="@+id/action_refresh"
android:title="@string/toolbar_force_refresh"
android:id="@+id/action_change_theme"
android:title="@string/toolbar_change_theme"
app:showAsAction="never"/>
</menu>
6 changes: 2 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<resources>
<string name="app_name">TrendyRepo</string>
<string name="action_settings">Settings</string>
<string name="toolbar_force_refresh">Refresh</string>
<string name="action_retry">RETRY</string>
<string name="title_detault_error">Something went wrong…</string>
<string name="title_default_error">Something went wrong…</string>
<string name="subtitle_default_error">An alien is probably blocking your signal</string>
<string name="subtitle_error_no_trending">Seems likes there\'s no trending repositories as of the moment</string>

<string name="toolbar_change_theme">Change Theme</string>
</resources>

This file was deleted.

0 comments on commit 32e4112

Please sign in to comment.