From 81995a860b78e90620df92a441db07c347127a65 Mon Sep 17 00:00:00 2001 From: Blatzar <46196380+Blatzar@users.noreply.github.com> Date: Wed, 17 Aug 2022 22:20:31 +0200 Subject: [PATCH] Nginx settings testing --- NginxProvider/build.gradle.kts | 10 +- .../src/main/kotlin/com/lagradost/NginxApi.kt | 62 ++++++++++++ .../com/lagradost/NginxProviderPlugin.kt | 23 ++++- .../com/lagradost/NginxSettingsFragment.kt | 60 ++++++++++++ NginxProvider/src/main/res/drawable/nginx.xml | 12 +++ .../src/main/res/drawable/nginx_question.xml | 17 ++++ .../src/main/res/layout/nginx_settings.xml | 96 +++++++++++++++++++ 7 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 NginxProvider/src/main/kotlin/com/lagradost/NginxApi.kt create mode 100644 NginxProvider/src/main/kotlin/com/lagradost/NginxSettingsFragment.kt create mode 100644 NginxProvider/src/main/res/drawable/nginx.xml create mode 100644 NginxProvider/src/main/res/drawable/nginx_question.xml create mode 100644 NginxProvider/src/main/res/layout/nginx_settings.xml diff --git a/NginxProvider/build.gradle.kts b/NginxProvider/build.gradle.kts index 631b0d8..7ef25f4 100644 --- a/NginxProvider/build.gradle.kts +++ b/NginxProvider/build.gradle.kts @@ -1,3 +1,9 @@ +dependencies { + implementation("androidx.legacy:legacy-support-v4:1.0.0") + implementation("com.google.android.material:material:1.4.0") + implementation("androidx.lifecycle:lifecycle-extensions:2.2.0") +} + // use an integer for version numbers version = 1 @@ -22,5 +28,5 @@ cloudstream { "TvSeries", "Movie", ) - - } \ No newline at end of file + requiresResources = true +} \ No newline at end of file diff --git a/NginxProvider/src/main/kotlin/com/lagradost/NginxApi.kt b/NginxProvider/src/main/kotlin/com/lagradost/NginxApi.kt new file mode 100644 index 0000000..eca3abc --- /dev/null +++ b/NginxProvider/src/main/kotlin/com/lagradost/NginxApi.kt @@ -0,0 +1,62 @@ + package com.lagradost + + import com.lagradost.cloudstream3.AcraApplication.Companion.getKey + import com.lagradost.cloudstream3.AcraApplication.Companion.setKey + import com.lagradost.cloudstream3.R + import com.lagradost.cloudstream3.syncproviders.AccountManager + import com.lagradost.cloudstream3.syncproviders.AuthAPI + import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI + import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager + + class NginxApi(index: Int) : InAppAuthAPIManager(index) { + override val name = "Nginx" + override val idPrefix = "nginx" + override val icon = R.drawable.nginx + override val requiresUsername = true + override val requiresPassword = true + override val requiresServer = true + override val createAccountUrl = "https://www.sarlays.com/use-nginx-with-cloudstream/" + + companion object { + const val NGINX_USER_KEY: String = "nginx_user" + } + + override fun getLatestLoginData(): InAppAuthAPI.LoginData? { + return getKey(accountId, NGINX_USER_KEY) + } + + override fun loginInfo(): AuthAPI.LoginInfo? { + val data = getLatestLoginData() ?: return null + return AuthAPI.LoginInfo(name = data.username ?: data.server, accountIndex = accountIndex) + } + + override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { + if (data.server.isNullOrBlank()) return false // we require a server + switchToNewAccount() + setKey(accountId, NGINX_USER_KEY, data) + registerAccount() + initialize() + AccountManager.inAppAuths + + return true + } + + override fun logOut() { + removeAccountKeys() + initializeData() + } + + private fun initializeData() { + val data = getLatestLoginData() ?: run { + NginxProvider.overrideUrl = null + NginxProvider.loginCredentials = null + return + } + NginxProvider.overrideUrl = data.server?.removeSuffix("/") + NginxProvider.loginCredentials = "${data.username ?: ""}:${data.password ?: ""}" + } + + override suspend fun initialize() { + initializeData() + } + } \ No newline at end of file diff --git a/NginxProvider/src/main/kotlin/com/lagradost/NginxProviderPlugin.kt b/NginxProvider/src/main/kotlin/com/lagradost/NginxProviderPlugin.kt index 0d8d099..657d2ca 100644 --- a/NginxProvider/src/main/kotlin/com/lagradost/NginxProviderPlugin.kt +++ b/NginxProvider/src/main/kotlin/com/lagradost/NginxProviderPlugin.kt @@ -1,14 +1,33 @@ - package com.lagradost import com.lagradost.cloudstream3.plugins.CloudstreamPlugin import com.lagradost.cloudstream3.plugins.Plugin import android.content.Context +import androidx.appcompat.app.AppCompatActivity +import com.lagradost.cloudstream3.AcraApplication.Companion.context +import com.lagradost.cloudstream3.utils.Coroutines.ioSafe +import com.lagradost.cloudstream3.AcraApplication.Companion.getActivity +import com.lagradost.cloudstream3.ui.settings.SettingsAccount @CloudstreamPlugin -class NginxProviderPlugin: Plugin() { +class NginxProviderPlugin : Plugin() { + val nginxApi = NginxApi(0) + override fun load(context: Context) { // All providers should be added in this manner. Please don't edit the providers list directly. registerMainAPI(NginxProvider()) + ioSafe { + nginxApi.initialize() + } + } + + init { + this.openSettings = { + val activity = context as? AppCompatActivity + if (activity != null) { + val frag = NginxSettingsFragment(this, nginxApi) + frag.show(activity.supportFragmentManager, nginxApi.name) + } + } } } \ No newline at end of file diff --git a/NginxProvider/src/main/kotlin/com/lagradost/NginxSettingsFragment.kt b/NginxProvider/src/main/kotlin/com/lagradost/NginxSettingsFragment.kt new file mode 100644 index 0000000..5b3147f --- /dev/null +++ b/NginxProvider/src/main/kotlin/com/lagradost/NginxSettingsFragment.kt @@ -0,0 +1,60 @@ +package com.lagradost + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.TextView +import com.google.android.material.bottomsheet.BottomSheetDialogFragment +import com.lagradost.cloudstream3.AcraApplication.Companion.openBrowser +import com.lagradost.cloudstream3.R.string.* +import com.lagradost.cloudstream3.plugins.Plugin +import com.lagradost.cloudstream3.ui.settings.SettingsAccount.Companion.showLoginInfo +import com.lagradost.cloudstream3.ui.settings.SettingsAccount.Companion.addAccount + +class NginxSettingsFragment(private val plugin: Plugin, val nginxApi: NginxApi) : + BottomSheetDialogFragment() { + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + val id = plugin.resources!!.getIdentifier("nginx_settings", "layout", "com.lagradost") + val layout = plugin.resources!!.getLayout(id) + return inflater.inflate(layout, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val infoView = view.findViewById(R.id.nginx_info) + val infoTextView = view.findViewById(R.id.info_main_text) + val infoSubTextView = view.findViewById(R.id.info_sub_text) + + infoTextView.text = getString(nginx_info_title) + infoSubTextView.text = getString(nginx_info_summary) + + val loginView = view.findViewById(R.id.nginx_login) + val loginTextView = view.findViewById(R.id.main_text) + + // object : View.OnClickListener is required to make it compile because otherwise it used invoke-customs + infoView.setOnClickListener(object : View.OnClickListener { + override fun onClick(v: View?) { + openBrowser("https://www.sarlays.com/use-nginx-with-cloudstream/") + } + }) + + + loginTextView.text = getString(login_format).format(nginxApi.name, getString(account)) + loginView.setOnClickListener(object : View.OnClickListener { + override fun onClick(v: View?) { + val info = nginxApi.loginInfo() + if (info != null) { + showLoginInfo(activity, nginxApi, info) + } else { + addAccount(activity, nginxApi) + } + } + }) + } +} \ No newline at end of file diff --git a/NginxProvider/src/main/res/drawable/nginx.xml b/NginxProvider/src/main/res/drawable/nginx.xml new file mode 100644 index 0000000..d952376 --- /dev/null +++ b/NginxProvider/src/main/res/drawable/nginx.xml @@ -0,0 +1,12 @@ + + + diff --git a/NginxProvider/src/main/res/drawable/nginx_question.xml b/NginxProvider/src/main/res/drawable/nginx_question.xml new file mode 100644 index 0000000..c9f053f --- /dev/null +++ b/NginxProvider/src/main/res/drawable/nginx_question.xml @@ -0,0 +1,17 @@ + + + + diff --git a/NginxProvider/src/main/res/layout/nginx_settings.xml b/NginxProvider/src/main/res/layout/nginx_settings.xml new file mode 100644 index 0000000..04b5661 --- /dev/null +++ b/NginxProvider/src/main/res/layout/nginx_settings.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file