Fixes and Linted

This commit is contained in:
Bnyro 2022-05-19 23:53:55 +02:00
parent 4f59a85ad9
commit b5f1c49e5a
3 changed files with 156 additions and 163 deletions

View File

@ -35,9 +35,10 @@ class MainActivity : AppCompatActivity() {
val TAG = "MainActivity" val TAG = "MainActivity"
lateinit var bottomNavigationView: BottomNavigationView lateinit var bottomNavigationView: BottomNavigationView
lateinit var toolbar: Toolbar lateinit var toolbar: Toolbar
lateinit var navController : NavController lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
DynamicColors.applyToActivitiesIfAvailable(application)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
RetrofitInstance.url = sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks/")!! RetrofitInstance.url = sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks/")!!
@ -50,28 +51,10 @@ class MainActivity : AppCompatActivity() {
updateAccentColor(this) updateAccentColor(this)
updateThemeMode(this) updateThemeMode(this)
updateLanguage(this)
DynamicColors.applyToActivitiesIfAvailable(application)
val languageName = sharedPreferences.getString("language", "sys")
if (languageName != "") {
var locale = if (languageName != "sys" && "$languageName".length < 3 ){
Locale(languageName)
} else if ("$languageName".length > 3) {
Locale(languageName?.substring(0,2), languageName?.substring(4,6))
} else {
Locale.getDefault()
}
val res = resources
val dm = res.displayMetrics
val conf = res.configuration
conf.setLocale(locale)
Locale.setDefault(locale)
res.updateConfiguration(conf, dm)
}
val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo=connectivityManager.activeNetworkInfo val networkInfo = connectivityManager.activeNetworkInfo
val isConnected = networkInfo != null && networkInfo.isConnected val isConnected = networkInfo != null && networkInfo.isConnected
if (!isConnected) { if (!isConnected) {
@ -102,12 +85,12 @@ class MainActivity : AppCompatActivity() {
true true
} }
R.id.subscriptions -> { R.id.subscriptions -> {
//navController.backQueue.clear() // navController.backQueue.clear()
navController.navigate(R.id.subscriptions) navController.navigate(R.id.subscriptions)
true true
} }
R.id.library -> { R.id.library -> {
//navController.backQueue.clear() // navController.backQueue.clear()
navController.navigate(R.id.library) navController.navigate(R.id.library)
true true
} }
@ -125,12 +108,12 @@ class MainActivity : AppCompatActivity() {
) )
toolbar.title = appName toolbar.title = appName
toolbar.setNavigationOnClickListener{ toolbar.setNavigationOnClickListener {
//settings activity stuff // settings activity stuff
val intent = Intent(this, SettingsActivity::class.java) val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent) startActivity(intent)
true true
} }
toolbar.setOnMenuItemClickListener { toolbar.setOnMenuItemClickListener {
when (it.itemId) { when (it.itemId) {
@ -142,134 +125,128 @@ class MainActivity : AppCompatActivity() {
false false
} }
} }
} }
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
val action: String? = intent?.action val action: String? = intent?.action
val data: Uri? = intent?.data val data: Uri? = intent?.data
Log.d(TAG, "dafaq"+data.toString()) Log.d(TAG, "dafaq" + data.toString())
if (data != null) { if (data != null) {
Log.d("dafaq",data.host+" ${data.path} ") Log.d("dafaq", data.host + " ${data.path} ")
if(data.host != null){ if (data.host != null) {
if(data.path != null){ if (data.path != null) {
//channel // channel
if(data.path!!.contains("/channel/") || data.path!!.contains("/c/") || data.path!!.contains("/user/")){ if (data.path!!.contains("/channel/") || data.path!!.contains("/c/") || data.path!!.contains("/user/")) {
var channel = data.path var channel = data.path
channel = channel!!.replace("/c/","") channel = channel!!.replace("/c/", "")
channel = channel!!.replace("/user/","") channel = channel!!.replace("/user/", "")
val bundle = bundleOf("channel_id" to channel) val bundle = bundleOf("channel_id" to channel)
navController.navigate(R.id.channel,bundle) navController.navigate(R.id.channel, bundle)
}else if(data.path!!.contains("/playlist")){ } else if (data.path!!.contains("/playlist")) {
var playlist = data.query!! var playlist = data.query!!
if (playlist.contains("&")) if (playlist.contains("&")) {
{ var playlists = playlist.split("&")
var playlists = playlist.split("&") for (v in playlists) {
for (v in playlists){ if (v.contains("list=")) {
if (v.contains("list=")){ playlist = v
playlist = v break
break
}
} }
} }
playlist = playlist.replace("list=","")
val bundle = bundleOf("playlist_id" to playlist)
navController.navigate(R.id.playlistFragment,bundle)
}else if(data.path!!.contains("/shorts/") || data.path!!.contains("/embed/") || data.path!!.contains("/v/")){
var watch = data.path!!.replace("/shorts/","").replace("/v/","").replace("/embed/","")
var bundle = Bundle()
bundle.putString("videoId",watch)
var frag = PlayerFragment()
frag.arguments = bundle
supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
Handler().postDelayed({
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}, 100)
}else if(data.path!!.contains("/watch") && data.query != null){
Log.d("dafaq",data.query!!)
var watch = data.query!!
if (watch.contains("&"))
{
var watches = watch.split("&")
for (v in watches){
if (v.contains("v=")){
watch = v
break
}
}
}
var bundle = Bundle()
bundle.putString("videoId",watch.replace("v=",""))
var frag = PlayerFragment()
frag.arguments = bundle
supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
Handler().postDelayed({
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}, 100)
}else{
var watch = data.path!!.replace("/","")
var bundle = Bundle()
bundle.putString("videoId",watch)
var frag = PlayerFragment()
frag.arguments = bundle
supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
Handler().postDelayed({
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}, 100)
} }
playlist = playlist.replace("list=", "")
val bundle = bundleOf("playlist_id" to playlist)
navController.navigate(R.id.playlistFragment, bundle)
} else if (data.path!!.contains("/shorts/") || data.path!!.contains("/embed/") || data.path!!.contains("/v/")) {
var watch = data.path!!.replace("/shorts/", "").replace("/v/", "").replace("/embed/", "")
var bundle = Bundle()
bundle.putString("videoId", watch)
var frag = PlayerFragment()
frag.arguments = bundle
supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
Handler().postDelayed({
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}, 100)
} else if (data.path!!.contains("/watch") && data.query != null) {
Log.d("dafaq", data.query!!)
var watch = data.query!!
if (watch.contains("&")) {
var watches = watch.split("&")
for (v in watches) {
if (v.contains("v=")) {
watch = v
break
}
}
}
var bundle = Bundle()
bundle.putString("videoId", watch.replace("v=", ""))
var frag = PlayerFragment()
frag.arguments = bundle
supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
Handler().postDelayed({
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}, 100)
} else {
var watch = data.path!!.replace("/", "")
var bundle = Bundle()
bundle.putString("videoId", watch)
var frag = PlayerFragment()
frag.arguments = bundle
supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
Handler().postDelayed({
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd()
motionLayout.transitionToStart()
}, 100)
} }
}
} }
} }
} }
override fun onBackPressed() { override fun onBackPressed() {
try{ try {
val mainMotionLayout = findViewById<MotionLayout>(R.id.mainMotionLayout) val mainMotionLayout = findViewById<MotionLayout>(R.id.mainMotionLayout)
if (mainMotionLayout.progress == 0.toFloat()){ if (mainMotionLayout.progress == 0.toFloat()) {
mainMotionLayout.transitionToEnd() mainMotionLayout.transitionToEnd()
findViewById<ConstraintLayout>(R.id.main_container).isClickable=false findViewById<ConstraintLayout>(R.id.main_container).isClickable = false
val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout) val motionLayout = findViewById<MotionLayout>(R.id.playerMotionLayout)
motionLayout.transitionToEnd() motionLayout.transitionToEnd()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
with(motionLayout) { with(motionLayout) {
getConstraintSet(R.id.start).constrainHeight(R.id.player, 0) getConstraintSet(R.id.start).constrainHeight(R.id.player, 0)
enableTransition(R.id.yt_transition,true) enableTransition(R.id.yt_transition, true)
} }
findViewById<LinearLayout>(R.id.linLayout).visibility=View.VISIBLE findViewById<LinearLayout>(R.id.linLayout).visibility = View.VISIBLE
isFullScreen=false isFullScreen = false
}else{ } else {
navController.popBackStack() navController.popBackStack()
if (navController.currentBackStackEntry == null && (parent as View).id != R.id.settings){ if (navController.currentBackStackEntry == null && (parent as View).id != R.id.settings) {
super.onBackPressed() super.onBackPressed()
} }
} }
}catch (e: Exception){ } catch (e: Exception) {
navController.popBackStack() navController.popBackStack()
moveTaskToBack(true) moveTaskToBack(true)
} }
@ -299,15 +276,17 @@ class MainActivity : AppCompatActivity() {
} }
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
)
} }
} }
private fun unsetFullscreen(){ private fun unsetFullscreen() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode = window.attributes.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
@ -331,7 +310,6 @@ class MainActivity : AppCompatActivity() {
(fragment as? PlayerFragment)?.onUserLeaveHint() (fragment as? PlayerFragment)?.onUserLeaveHint()
} }
} }
} }
fun Fragment.hideKeyboard() { fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) } view?.let { activity?.hideKeyboard(it) }

View File

@ -15,7 +15,6 @@ import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -32,8 +31,9 @@ import java.io.InputStream
import java.util.zip.ZipEntry import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream import java.util.zip.ZipInputStream
class SettingsActivity : AppCompatActivity(), class SettingsActivity :
SharedPreferences.OnSharedPreferenceChangeListener{ AppCompatActivity(),
SharedPreferences.OnSharedPreferenceChangeListener {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
updateAccentColor(this) updateAccentColor(this)
@ -47,8 +47,6 @@ class SettingsActivity : AppCompatActivity(),
view.setAlpha(0F) view.setAlpha(0F)
view.animate().alpha(1F).setDuration(300) view.animate().alpha(1F).setDuration(300)
setContentView(R.layout.activity_settings) setContentView(R.layout.activity_settings)
if (savedInstanceState == null) { if (savedInstanceState == null) {
supportFragmentManager supportFragmentManager
@ -59,12 +57,10 @@ class SettingsActivity : AppCompatActivity(),
PreferenceManager.getDefaultSharedPreferences(this) PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this) .registerOnSharedPreferenceChangeListener(this)
} }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, rootKey: String?) {} override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, rootKey: String?) {}
class SettingsFragment : PreferenceFragmentCompat() { class SettingsFragment : PreferenceFragmentCompat() {
val TAG = "Settings" val TAG = "Settings"
@ -83,23 +79,23 @@ class SettingsActivity : AppCompatActivity(),
// "rw" for read-and-write; // "rw" for read-and-write;
// "rwt" for truncating or overwriting existing file contents. // "rwt" for truncating or overwriting existing file contents.
//val readOnlyMode = "r" // val readOnlyMode = "r"
// uri - I have got from onActivityResult // uri - I have got from onActivityResult
val type = resolver.getType(uri) val type = resolver.getType(uri)
var inputStream: InputStream? = resolver.openInputStream(uri) var inputStream: InputStream? = resolver.openInputStream(uri)
val channels = ArrayList<String>() val channels = ArrayList<String>()
if(type == "application/json"){ if (type == "application/json") {
val json = inputStream?.bufferedReader()?.readLines()?.get(0) val json = inputStream?.bufferedReader()?.readLines()?.get(0)
val jsonObject = JSONTokener(json).nextValue() as JSONObject val jsonObject = JSONTokener(json).nextValue() as JSONObject
Log.e(TAG,jsonObject.getJSONArray("subscriptions").toString()) Log.e(TAG, jsonObject.getJSONArray("subscriptions").toString())
for (i in 0 until jsonObject.getJSONArray("subscriptions").length()) { for (i in 0 until jsonObject.getJSONArray("subscriptions").length()) {
var url = jsonObject.getJSONArray("subscriptions").getJSONObject(i).getString("url") var url = jsonObject.getJSONArray("subscriptions").getJSONObject(i).getString("url")
url = url.replace("https://www.youtube.com/channel/","") url = url.replace("https://www.youtube.com/channel/", "")
Log.e(TAG,url) Log.e(TAG, url)
channels.add(url) channels.add(url)
} }
}else { } else {
if (type == "application/zip") { if (type == "application/zip") {
val zis = ZipInputStream(inputStream) val zis = ZipInputStream(inputStream)
var entry: ZipEntry? = zis.nextEntry var entry: ZipEntry? = zis.nextEntry
@ -132,8 +128,6 @@ class SettingsActivity : AppCompatActivity(),
).show() ).show()
} }
} }
} }
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
@ -166,7 +160,7 @@ class SettingsActivity : AppCompatActivity(),
val sponsorblock = findPreference<Preference>("sponsorblock") val sponsorblock = findPreference<Preference>("sponsorblock")
sponsorblock?.setOnPreferenceClickListener { sponsorblock?.setOnPreferenceClickListener {
val newFragment = SponsorBlockSettings() val newFragment = SponsorBlockSettings()
parentFragmentManager.beginTransaction() parentFragmentManager.beginTransaction()
.replace(R.id.settings, newFragment) .replace(R.id.settings, newFragment)
.commitNow() .commitNow()
true true
@ -176,7 +170,7 @@ class SettingsActivity : AppCompatActivity(),
importFromYt?.setOnPreferenceClickListener { importFromYt?.setOnPreferenceClickListener {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token", "")!! val token = sharedPref?.getString("token", "")!!
//check StorageAccess // check StorageAccess
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Log.d("myz", "" + Build.VERSION.SDK_INT) Log.d("myz", "" + Build.VERSION.SDK_INT)
if (ContextCompat.checkSelfPermission( if (ContextCompat.checkSelfPermission(
@ -186,11 +180,13 @@ class SettingsActivity : AppCompatActivity(),
!= PackageManager.PERMISSION_GRANTED != PackageManager.PERMISSION_GRANTED
) { ) {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
this.requireActivity(), arrayOf( this.requireActivity(),
arrayOf(
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.MANAGE_EXTERNAL_STORAGE Manifest.permission.MANAGE_EXTERNAL_STORAGE
), 1 ),
) //permission request code is just an int 1
) // permission request code is just an int
} else if (token != "") { } else if (token != "") {
getContent.launch("*/*") getContent.launch("*/*")
} else { } else {
@ -201,9 +197,9 @@ class SettingsActivity : AppCompatActivity(),
requireContext(), requireContext(),
Manifest.permission.READ_EXTERNAL_STORAGE Manifest.permission.READ_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
requireContext(), requireContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED ) != PackageManager.PERMISSION_GRANTED
) { ) {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
this.requireActivity(), this.requireActivity(),
@ -230,7 +226,7 @@ class SettingsActivity : AppCompatActivity(),
} }
val accentColor = findPreference<Preference>("accent_color") val accentColor = findPreference<Preference>("accent_color")
accentColor?.setOnPreferenceChangeListener {_, _ -> accentColor?.setOnPreferenceChangeListener { _, _ ->
val refresh = Intent(context, SettingsActivity::class.java) val refresh = Intent(context, SettingsActivity::class.java)
startActivity(refresh) startActivity(refresh)
true true
@ -238,7 +234,7 @@ class SettingsActivity : AppCompatActivity(),
val changeLanguage = findPreference<ListPreference>("language") val changeLanguage = findPreference<ListPreference>("language")
changeLanguage?.setOnPreferenceChangeListener { _, _ -> changeLanguage?.setOnPreferenceChangeListener { _, _ ->
val refresh = Intent(context, SettingsActivity::class.java) val refresh = Intent(context, MainActivity::class.java)
startActivity(refresh) startActivity(refresh)
true true
} }
@ -260,16 +256,16 @@ class SettingsActivity : AppCompatActivity(),
val license = findPreference<Preference>("license") val license = findPreference<Preference>("license")
license?.setOnPreferenceClickListener { license?.setOnPreferenceClickListener {
val licenseString = view?.context?.assets!!.open("gpl3.html").bufferedReader().use{ val licenseString = view?.context?.assets!!.open("gpl3.html").bufferedReader().use {
it.readText() it.readText()
} }
val licenseHtml = if (Build.VERSION.SDK_INT >= 24) { val licenseHtml = if (Build.VERSION.SDK_INT >= 24) {
Html.fromHtml(licenseString,1) Html.fromHtml(licenseString, 1)
} else { } else {
Html.fromHtml(licenseString) Html.fromHtml(licenseString)
} }
AlertDialog.Builder(view?.context!!) AlertDialog.Builder(view?.context!!)
.setPositiveButton(getString(R.string.okay), DialogInterface.OnClickListener{ _,_ -> }) .setPositiveButton(getString(R.string.okay), DialogInterface.OnClickListener { _, _ -> })
.setMessage(licenseHtml) .setMessage(licenseHtml)
.create() .create()
.show() .show()
@ -323,7 +319,6 @@ class SettingsActivity : AppCompatActivity(),
activity?.runOnUiThread(action) activity?.runOnUiThread(action)
} }
private fun subscribe(channels: List<String>) { private fun subscribe(channels: List<String>) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
@ -360,5 +355,4 @@ class SettingsActivity : AppCompatActivity(),
intent = Intent(this, MainActivity::class.java) intent = Intent(this, MainActivity::class.java)
startActivity(intent) startActivity(intent)
} }
}
}

View File

@ -3,6 +3,7 @@ package com.github.libretube
import android.content.Context import android.content.Context
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import java.util.*
fun updateAccentColor(context: Context) { fun updateAccentColor(context: Context) {
val colorAccent = PreferenceManager.getDefaultSharedPreferences(context).getString("accent_color", "red") val colorAccent = PreferenceManager.getDefaultSharedPreferences(context).getString("accent_color", "red")
@ -29,3 +30,23 @@ fun oledMode(context: Context) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
context.setTheme(R.style.Theme_OLED) context.setTheme(R.style.Theme_OLED)
} }
fun updateLanguage(context: Context) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val languageName = sharedPreferences.getString("language", "sys")
if (languageName != "") {
var locale = if (languageName != "sys" && "$languageName".length < 3 ) {
Locale(languageName)
} else if ("$languageName".length > 3) {
Locale(languageName?.substring(0,2), languageName?.substring(4,6))
} else {
Locale.getDefault()
}
val res = context.resources
val dm = res.displayMetrics
val conf = res.configuration
conf.setLocale(locale)
Locale.setDefault(locale)
res.updateConfiguration(conf, dm)
}
}