Skip to content

Commit

Permalink
all: add dark mode (fixes #3564) (#3624)
Browse files Browse the repository at this point in the history
Co-authored-by: Gideon Okuro <[email protected]>
Co-authored-by: dogi <[email protected]>
  • Loading branch information
3 people authored Jul 23, 2024
1 parent 1e7202a commit 9edd473
Show file tree
Hide file tree
Showing 78 changed files with 1,245 additions and 236 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1717
versionName "0.17.17"
versionCode 1718
versionName "0.17.18"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import android.os.StrictMode
import android.os.StrictMode.VmPolicy
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
Expand Down Expand Up @@ -93,6 +95,9 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {

context = this
preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
nightMode()
// UNCOMMENT BELOW TO FORCE DARK MODE FOR DARK MODE DEVELOPMENT
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
val builder = VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
builder.detectFileUriExposure()
Expand All @@ -114,6 +119,16 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
onAppStarted()
}

private fun nightMode() {
val preference = PreferenceManager.getDefaultSharedPreferences(this).getString("dark_mode", "Follow System")
val options = listOf(*resources.getStringArray(R.array.dark_mode_options))
when (options.indexOf(preference)) {
0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
2 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}

private fun scheduleAutoSyncWork(syncInterval: Int?) {
val autoSyncWork: PeriodicWorkRequest? = syncInterval?.let { PeriodicWorkRequest.Builder(AutoSyncWorker::class.java, it.toLong(), TimeUnit.SECONDS).build() }
val workManager = WorkManager.getInstance(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ abstract class BaseResourceFragment : Fragment() {
}
val inflater = activity?.layoutInflater
convertView = inflater?.inflate(R.layout.my_library_alertdialog, null)
val alertDialogBuilder = AlertDialog.Builder(requireContext())
val alertDialogBuilder = AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme)
alertDialogBuilder.setView(convertView)
.setTitle(R.string.download_suggestion)
alertDialogBuilder.setPositiveButton(R.string.download_selected) { _: DialogInterface?, _: Int ->
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/org/ole/planet/myplanet/ui/SettingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package org.ole.planet.myplanet.ui
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceChangeListener
Expand Down Expand Up @@ -70,6 +72,7 @@ class SettingActivity : AppCompatActivity() {

@RequiresApi(Build.VERSION_CODES.O)
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
requireContext().setTheme(R.style.PreferencesTheme)
setPreferencesFromResource(R.xml.pref, rootKey)
profileDbHandler = UserProfileDbHandler(requireActivity())
user = profileDbHandler.userModel
Expand All @@ -85,6 +88,17 @@ class SettingActivity : AppCompatActivity() {
}
}

val darkMode = findPreference<Preference>("dark_mode")
if (darkMode != null) {
darkMode.onPreferenceChangeListener = OnPreferenceChangeListener { preference: Preference?, newValue: Any? ->
if (preference?.key == "dark_mode") {
darkMode(newValue.toString())
return@OnPreferenceChangeListener true
}
false
}
}

// Show Available space under the "Freeup Space" preference.
val spacePreference = findPreference<Preference>("freeup_space")
if (spacePreference != null) {
Expand Down Expand Up @@ -181,6 +195,14 @@ class SettingActivity : AppCompatActivity() {
profileDbHandler.onDestory()
}
}

private fun darkMode(key: String) {
when (key) {
"ON" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
"OFF" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
"Follow System" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class HomeCommunityDialogFragment : BottomSheetDialogFragment() {
tab.text = (fragmentTeamDetailBinding.viewPager2.adapter as CommunityPagerAdapter).getPageTitle(position)
}.attach()
fragmentTeamDetailBinding.title.text = communityName
fragmentTeamDetailBinding.title.setTextColor(ContextCompat.getColor(requireContext(), R.color.md_black_1000))
fragmentTeamDetailBinding.subtitle.setTextColor(ContextCompat.getColor(requireContext(), R.color.md_black_1000))
fragmentTeamDetailBinding.title.setTextColor(ContextCompat.getColor(requireContext(), R.color.daynight_textColor))
fragmentTeamDetailBinding.subtitle.setTextColor(ContextCompat.getColor(requireContext(), R.color.daynight_textColor))
fragmentTeamDetailBinding.subtitle.text = TimeUtils.getFormatedDateWithTime(Date().time)
fragmentTeamDetailBinding.appBar.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.secondary_bg))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ open class BaseDashboardFragment : BaseDashboardFragmentPlugin(), NotificationCa
val itemLibraryHomeBinding = ItemLibraryHomeBinding.inflate(LayoutInflater.from(activity))
val v = itemLibraryHomeBinding.root
setTextColor(itemLibraryHomeBinding.title, itemCnt)
val colorResId = if (itemCnt % 2 == 0) R.color.md_white_1000 else R.color.md_grey_300
val colorResId = if (itemCnt % 2 == 0) R.color.dashboard_item else R.color.dashboard_item_alternative
val color = context?.let { ContextCompat.getColor(it, colorResId) }
if (color != null) {
v.setBackgroundColor(color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ open class BaseDashboardFragmentPlugin : BaseContainerFragment() {
if (count % 2 == 0) {
v.setBackgroundResource(R.drawable.light_rect)
} else {
v.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.md_grey_300))
v.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.dashboard_item_alternative))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.WindowInsetsCompat
Expand Down Expand Up @@ -300,7 +301,14 @@ class DashboardActivity : DashboardElementActivity(), OnHomeItemClickListener, N
.build()
val headerBackground = header.headerBackgroundView
headerBackground.setPadding(30, 60, 30, 60)
headerBackground.setColorFilter(ContextCompat.getColor(this, R.color.md_white_1000), PorterDuff.Mode.SRC_IN)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO ||
(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM && currentNightMode == Configuration.UI_MODE_NIGHT_NO)) {
headerBackground.setColorFilter(
ContextCompat.getColor(this, R.color.md_white_1000),
PorterDuff.Mode.SRC_IN
)
}
return header
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.WindowInsetsCompat
Expand Down Expand Up @@ -300,7 +301,14 @@ class DashboardActivity : DashboardElementActivity(), OnHomeItemClickListener, N
.build()
val headerBackground = header.headerBackgroundView
headerBackground.setPadding(30, 60, 30, 60)
headerBackground.setColorFilter(ContextCompat.getColor(this, R.color.md_white_1000), PorterDuff.Mode.SRC_IN)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO ||
(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM && currentNightMode == Configuration.UI_MODE_NIGHT_NO)) {
headerBackground.setColorFilter(
ContextCompat.getColor(this, R.color.md_white_1000),
PorterDuff.Mode.SRC_IN
)
}
return header
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.ole.planet.myplanet.ui.news
import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Build
import android.view.LayoutInflater
import android.view.MenuItem
Expand Down Expand Up @@ -250,6 +249,7 @@ class AdapterNews(var context: Context, private val list: MutableList<RealmNews?
viewHolder.rowNewsBinding.imgNews.visibility = View.GONE
}

@RequiresApi(Build.VERSION_CODES.M)
private fun showReplyButton(holder: RecyclerView.ViewHolder, finalNews: RealmNews?, position: Int) {
val viewHolder = holder as ViewHolderNews
if (listener == null || fromLogin) {
Expand All @@ -258,6 +258,7 @@ class AdapterNews(var context: Context, private val list: MutableList<RealmNews?
viewHolder.rowNewsBinding.btnReply.setOnClickListener { showEditAlert(finalNews?.id, false) }
val replies: List<RealmNews> = mRealm.where(RealmNews::class.java).sort("time", Sort.DESCENDING).equalTo("replyTo", finalNews?.id, Case.INSENSITIVE).findAll()
viewHolder.rowNewsBinding.btnShowReply.text = String.format(context.getString(R.string.show_replies) + " (%d)", replies.size)
viewHolder.rowNewsBinding.btnShowReply.setTextColor(context.getColor(R.color.daynight_textColor))
viewHolder.rowNewsBinding.btnShowReply.visibility = if (replies.isNotEmpty()) {
View.VISIBLE
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.ole.planet.myplanet.ui.onBoarding

import android.content.Context
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.viewpager.widget.PagerAdapter
import org.ole.planet.myplanet.R

Expand All @@ -19,6 +21,7 @@ class OnBoardingAdapter(private val mContext: Context, private val onBoardItems:
return view == `object`
}

@RequiresApi(Build.VERSION_CODES.M)
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val itemView = LayoutInflater.from(mContext).inflate(R.layout.onboard_item, container, false)

Expand All @@ -27,8 +30,10 @@ class OnBoardingAdapter(private val mContext: Context, private val onBoardItems:
imageView.setImageResource(item.imageID)
val tvTitle = itemView.findViewById<TextView>(R.id.tv_header)
tvTitle.text = item.title
tvTitle.setTextColor(mContext.getColor(R.color.daynight_textColor))
val tvContent = itemView.findViewById<TextView>(R.id.tv_desc)
tvContent.text = item.description
tvContent.setTextColor(mContext.getColor(R.color.daynight_textColor))
container.addView(itemView)

return itemView
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ole.planet.myplanet.ui.survey

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
Expand All @@ -25,6 +26,7 @@ class SurveyFragment : BaseRecyclerFragment<RealmStepExam?>() {
}!!
}

@SuppressLint("ResourceAsColor")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
spn = requireView().findViewById(R.id.spn_sort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.PorterDuff
import android.net.Uri
Expand All @@ -18,6 +19,7 @@ import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import com.google.android.material.textfield.TextInputLayout
import org.ole.planet.myplanet.R
Expand Down Expand Up @@ -115,7 +117,11 @@ abstract class ProcessUserDataActivity : PermissionActivity(), SuccessListener {
val green = Color.green(newColor)
val blue = Color.blue(newColor)
val alphaWhite = Color.argb(alpha, red, green, blue)
logo.setColorFilter(alphaWhite, PorterDuff.Mode.SRC_ATOP)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO ||
(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM && currentNightMode == Configuration.UI_MODE_NIGHT_NO)) {
logo.setColorFilter(alphaWhite, PorterDuff.Mode.SRC_ATOP)
}
}

fun setUrlParts(url: String, password: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.ole.planet.myplanet.views

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.view.ContextThemeWrapper
import androidx.preference.ListPreference
import org.ole.planet.myplanet.R

class RoundedListPreference : ListPreference {

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
defStyleAttr
)

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

constructor(context: Context) : super(context)

override fun onClick() {
val currentIndex = findIndexOfValue(value)
val dialog = AlertDialog.Builder(ContextThemeWrapper(context, R.style.CustomAlertDialogStyle))
.setTitle(title)
.setSingleChoiceItems(entryValues, currentIndex) { dialog: DialogInterface, index: Int ->
if (callChangeListener(entryValues[index].toString())) {
setValueIndex(index)
}
dialog.dismiss()
}
.setNegativeButton(R.string.cancel) { dialog: DialogInterface, _: Int -> dialog.dismiss() }.create()
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

dialog.show()
}

}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/alertdialog_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/secondary_bg"/>
<corners android:radius="10dp"/>
</shape>
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/dialog_window_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/secondary_bg" />
</shape>
</item>
</selector>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/light_rect.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<solid android:color="@color/dashboard_item" />
<corners android:radius="0dp" />
</shape>
</shape>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/non_selected_item_dot.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
android:width="8dp"
android:height="8dp" />

<solid android:color="@color/colorPrimaryLight" />
</shape>
<solid android:color="@color/mainColorLight" />
</shape>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/oval_white.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
android:shape="oval"

>
<solid android:color="@color/md_white_1000"/>
</shape>
<solid android:color="@color/card_bg"/>
</shape>
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/selected_item_dot.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
android:width="8dp"
android:height="8dp" />

<solid android:color="@color/colorPrimary" />
</shape>
<solid android:color="@color/mainColor" />
</shape>
Loading

0 comments on commit 9edd473

Please sign in to comment.