From 6a8d3baccb9d4c79892a924427392685092fbb55 Mon Sep 17 00:00:00 2001 From: Ivan Agosto Date: Sun, 31 Mar 2024 16:59:57 -0600 Subject: [PATCH] Update setting view --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 46 +++++++++++++------ .../org/libre/agosto/p2play/HostActivity.kt | 6 +-- .../org/libre/agosto/p2play/MainActivity.kt | 2 +- .../libre/agosto/p2play/ManagerSingleton.kt | 21 ++++++++- .../libre/agosto/p2play/SettingsActivity2.kt | 31 +++++++++++++ .../org/libre/agosto/p2play/SplashActivity.kt | 29 +++++------- .../main/res/layout/activity_reproductor.xml | 4 +- app/src/main/res/layout/settings_activity.xml | 9 ++++ app/src/main/res/values/themes.xml | 2 +- app/src/main/res/xml/root_preferences.xml | 31 +++++++++++++ 11 files changed, 140 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/org/libre/agosto/p2play/SettingsActivity2.kt create mode 100644 app/src/main/res/layout/settings_activity.xml create mode 100644 app/src/main/res/xml/root_preferences.xml diff --git a/app/build.gradle b/app/build.gradle index 739c08b..adbf60d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,6 +42,7 @@ dependencies { implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.android.support:appcompat-v7:23.2.0' implementation 'com.google.android.material:material:1.6.0' + implementation 'androidx.preference:preference:1.2.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.5.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1078c0e..2371a50 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,13 +11,23 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.P2play"> - + + + + + android:exported="true" + android:theme="@style/Theme.P2play.NoActionBar"> @@ -26,26 +36,32 @@ + android:exported="false" + android:theme="@style/Theme.P2play.NoActionBar" /> + android:exported="false" + android:theme="@style/Theme.P2play.NoActionBar" /> + + + - - - + android:theme="@style/Theme.P2play" /> \ No newline at end of file diff --git a/app/src/main/java/org/libre/agosto/p2play/HostActivity.kt b/app/src/main/java/org/libre/agosto/p2play/HostActivity.kt index f9f7e53..875b744 100644 --- a/app/src/main/java/org/libre/agosto/p2play/HostActivity.kt +++ b/app/src/main/java/org/libre/agosto/p2play/HostActivity.kt @@ -6,7 +6,7 @@ import android.os.AsyncTask import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Looper -import android.preference.PreferenceManager +import androidx.preference.PreferenceManager import kotlinx.android.synthetic.main.activity_host.* import org.libre.agosto.p2play.ajax.Auth @@ -35,14 +35,12 @@ class HostActivity : AppCompatActivity() { ManagerSingleton.logout() getKeys(host!!) }else{ - ManagerSingleton.url=host + ManagerSingleton.url = host startApp() } } } - - fun saveHost(host: String){ editor.putString("last_host",host) editor.putString("hostP2play",host) diff --git a/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt b/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt index 7efe98b..9767808 100644 --- a/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt +++ b/app/src/main/java/org/libre/agosto/p2play/MainActivity.kt @@ -304,7 +304,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte // as you specify a parent activity in AndroidManifest.xml. when (item.itemId) { R.id.action_settings -> { - val intent = Intent(this, SettingsActivity::class.java) + val intent = Intent(this, SettingsActivity2::class.java) startActivity(intent) return true } diff --git a/app/src/main/java/org/libre/agosto/p2play/ManagerSingleton.kt b/app/src/main/java/org/libre/agosto/p2play/ManagerSingleton.kt index b50e2c4..99d8fd5 100644 --- a/app/src/main/java/org/libre/agosto/p2play/ManagerSingleton.kt +++ b/app/src/main/java/org/libre/agosto/p2play/ManagerSingleton.kt @@ -1,6 +1,9 @@ package org.libre.agosto.p2play import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import android.os.Handler import org.libre.agosto.p2play.models.TokenModel import org.libre.agosto.p2play.models.UserModel @@ -10,13 +13,29 @@ object ManagerSingleton { var token: TokenModel = TokenModel() var nfsw: Boolean = false var videos_count: Int = 0 - + lateinit var settings: SharedPreferences + lateinit var db: Database fun Toast(text: String?, context: Context) { android.widget.Toast.makeText(context, text, android.widget.Toast.LENGTH_SHORT).show() } fun logout(){ + db.logout() user = UserModel() token = TokenModel() } + + fun reloadSettings () { + val host = settings.getString("hostP2play", "") + val lastHost = settings.getString("last_host", "") + if(host != ""){ + if(lastHost != host){ + logout() + } + url = host + } + + nfsw = settings.getBoolean("show_nsfw", false) + videos_count = settings.getString("videos_count", "15")!!.toInt() + } } \ No newline at end of file diff --git a/app/src/main/java/org/libre/agosto/p2play/SettingsActivity2.kt b/app/src/main/java/org/libre/agosto/p2play/SettingsActivity2.kt new file mode 100644 index 0000000..905f98a --- /dev/null +++ b/app/src/main/java/org/libre/agosto/p2play/SettingsActivity2.kt @@ -0,0 +1,31 @@ +package org.libre.agosto.p2play + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import androidx.preference.PreferenceFragmentCompat + +class SettingsActivity2 : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.settings_activity) + if (savedInstanceState == null) { + supportFragmentManager + .beginTransaction() + .replace(R.id.settings, SettingsFragment()) + .commit() + } + supportActionBar?.setDisplayHomeAsUpEnabled(true) + } + + override fun onDestroy() { + super.onDestroy() + ManagerSingleton.reloadSettings() + } + + class SettingsFragment : PreferenceFragmentCompat() { + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + setPreferencesFromResource(R.xml.root_preferences, rootKey) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/libre/agosto/p2play/SplashActivity.kt b/app/src/main/java/org/libre/agosto/p2play/SplashActivity.kt index 4a2150e..536993e 100644 --- a/app/src/main/java/org/libre/agosto/p2play/SplashActivity.kt +++ b/app/src/main/java/org/libre/agosto/p2play/SplashActivity.kt @@ -7,14 +7,13 @@ import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Handler import android.os.Looper -import android.preference.PreferenceManager +import androidx.preference.PreferenceManager import android.util.Log import org.libre.agosto.p2play.ajax.Auth import java.lang.Exception class SplashActivity : AppCompatActivity() { lateinit var settings: SharedPreferences - lateinit var editor: SharedPreferences.Editor val client: Auth = Auth() val _db = Database(this) @@ -23,16 +22,15 @@ class SplashActivity : AppCompatActivity() { setContentView(R.layout.activity_splash) settings = PreferenceManager.getDefaultSharedPreferences(this) + ManagerSingleton.settings = settings + ManagerSingleton.db = _db - ManagerSingleton.nfsw = settings.getBoolean("show_nfsw", false) - ManagerSingleton.videos_count = settings.getString("videos_count", "15")!!.toInt() - + ManagerSingleton.reloadSettings() val host = settings.getString("hostP2play","") val lastHost = settings.getString("last_host","") if(host != ""){ if(lastHost != host){ - _db.logout() Handler().postDelayed({ startHostActivity() }, 2000) @@ -40,8 +38,7 @@ class SplashActivity : AppCompatActivity() { ManagerSingleton.url = host checkUser() } - } - else{ + } else { Handler().postDelayed({ startHostActivity() }, 2000) @@ -49,7 +46,7 @@ class SplashActivity : AppCompatActivity() { } private fun checkUser(){ - Log.d("was", "Chequed") + Log.d("was", "Checked") try { val token = _db.getToken() val user = _db.getUser() @@ -57,12 +54,11 @@ class SplashActivity : AppCompatActivity() { if (Looper.myLooper() == null) Looper.prepare() - if (token.status == 1 && user.status == 1) { - val client_id = settings.getString("client_id", "")!! - val client_secret = settings.getString("client_secret", "")!! + val clientId = settings.getString("client_id", "")!! + val clientSecret = settings.getString("client_secret", "")!! - val newToken = client.refreshToken(token, client_id, client_secret) + val newToken = client.refreshToken(token, clientId, clientSecret) when (token.status.toString()) { "1" -> { @@ -70,21 +66,18 @@ class SplashActivity : AppCompatActivity() { ManagerSingleton.token = newToken ManagerSingleton.user = user } - else -> _db.logout() + else -> ManagerSingleton.logout() } } else { - _db.logout() + ManagerSingleton.logout() } startApp() - Log.d("Aqui", "81") - } } catch (err: Exception){ err.printStackTrace() - Log.d("Aqui", "89") Handler().postDelayed({ startApp() }, 2000) diff --git a/app/src/main/res/layout/activity_reproductor.xml b/app/src/main/res/layout/activity_reproductor.xml index ae84106..892d06c 100644 --- a/app/src/main/res/layout/activity_reproductor.xml +++ b/app/src/main/res/layout/activity_reproductor.xml @@ -65,14 +65,14 @@ diff --git a/app/src/main/res/layout/settings_activity.xml b/app/src/main/res/layout/settings_activity.xml new file mode 100644 index 0000000..de6591a --- /dev/null +++ b/app/src/main/res/layout/settings_activity.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index b71a40d..f98e297 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -12,7 +12,7 @@ @color/md_theme_light_surface @color/md_theme_light_onSurface @color/md_theme_light_primary - @color/md_theme_light_onPrimary + @color/md_theme_light_secondary @color/md_theme_light_secondary diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml new file mode 100644 index 0000000..1e1ca6b --- /dev/null +++ b/app/src/main/res/xml/root_preferences.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file