diff --git a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt index fc82288..fe1dbfb 100644 --- a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt +++ b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt @@ -12,26 +12,26 @@ import android.os.Handler import android.os.Looper import android.text.Editable import android.text.TextWatcher -import android.view.Menu -import android.view.MenuItem -import android.widget.Button -import android.widget.EditText -import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import android.content.pm.PackageInfo import android.net.Uri -import android.widget.TextView +import android.view.* +import android.widget.* +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.floatingactionbutton.FloatingActionButton +import android.text.InputType + +import android.widget.EditText +import java.util.* class MainActivity : AppCompatActivity() { private var settingsButton: Menu? = null // before starting the app there is no settings button private var inSettings: Boolean = false // by default your not in settings page private var prefs: SharedPreferences? = null // first run detection - - init { - instance = this - } + private val sharedPref = "com.mylloon.MobiDL" // shared pref name companion object { private var instance: MainActivity? = null @@ -41,11 +41,26 @@ class MainActivity : AppCompatActivity() { } } + init { + instance = this + } + private fun toggleSettingsButtonVisibility() { // Change Settings button visibility val visibility: Boolean = settingsButton?.findItem(R.id.settingsButton)?.isVisible == true settingsButton?.findItem(R.id.settingsButton)?.isVisible = !visibility } + private fun getValuesRecyclerView(): MutableList { // list of the apps (from the storage if exists) + val context = applicationContext() // get app context + + // read apps from the app preference + val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE) + var data: Set? = null + data = sharedPref.getStringSet("apps", null) + + return data?.toMutableList() ?: mutableListOf() + } + override fun onCreate(savedInstanceState: Bundle?) { // Main function super.onCreate(savedInstanceState) prefs = getSharedPreferences("com.mylloon.MobiDL", MODE_PRIVATE) @@ -98,14 +113,94 @@ class MainActivity : AppCompatActivity() { } } - private fun mainPage() { - setContentView(R.layout.activity_main) - Handler(Looper.getMainLooper()).postDelayed({ - toggleSettingsButtonVisibility() - }, 50) + private fun mainPage(toDelete: String? = null) { + if (toDelete == null) { + setContentView(R.layout.activity_main) // display main page + Handler(Looper.getMainLooper()).postDelayed({ + toggleSettingsButtonVisibility() + }, 100) // delay to add button settings + } + val valuesRecyclerView = getValuesRecyclerView() // list of all the element in the main page + + class Adapter(private val values: List): RecyclerView.Adapter() { + override fun getItemCount() = values.size + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { // add viewHolder to the main page + val itemView = LayoutInflater.from(parent.context).inflate(R.layout.list_item_view, parent, false) + return ViewHolder(itemView) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.button?.text = values[position] + } + + inner class ViewHolder(itemView: View?): RecyclerView.ViewHolder(itemView!!) { + var button: Button? = null + init { + button = itemView?.findViewById(R.id.text_list_item) + button?.setOnLongClickListener { + val builder: android.app.AlertDialog.Builder = android.app.AlertDialog.Builder(instance) + builder.setTitle("Remove ${button?.text}?") + builder.setPositiveButton("Remove") { _, _ -> instance?.mainPage(button?.text.toString()) } + builder.setNeutralButton("Cancel") { dialog, _ -> dialog.cancel() } + builder.show() + true + } + } + } + + } + + val recyclerView: RecyclerView = findViewById(R.id.recyclerView) + if (toDelete != null) { + valuesRecyclerView.remove(toDelete) + + val context = applicationContext() // get app context + + // read apps from the app preference + val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE) + sharedPref.edit().putStringSet("apps", valuesRecyclerView.toSet()).apply() + recyclerView.adapter = Adapter(valuesRecyclerView) + } + recyclerView.layoutManager = LinearLayoutManager(this) + recyclerView.adapter = Adapter(valuesRecyclerView) + + + fun addApp(app: String) { + valuesRecyclerView.add(app) + + val context = applicationContext() // get app context + + // read apps from the app preference + val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE) + sharedPref.edit().putStringSet("apps", valuesRecyclerView.toSet()).apply() + recyclerView.adapter = Adapter(valuesRecyclerView) + + } + + val addButton = findViewById(R.id.addButton) + addButton.setOnClickListener { + val builder: android.app.AlertDialog.Builder = android.app.AlertDialog.Builder(this) + builder.setTitle("App Name") + val input = EditText(this) + input.inputType = InputType.TYPE_CLASS_TEXT + builder.setView(input) + builder.setPositiveButton("Validate") { _, _ -> addApp(input.text.toString()) } + builder.setNeutralButton("Cancel") { dialog, _ -> dialog.cancel() } + + builder.show() + } + + // easter egg + addButton.setOnLongClickListener { + Toast.makeText(this@MainActivity, "lâche frère", Toast.LENGTH_SHORT) + .show() + true + } + // user = Credentials().get(0).toString() // password = Credentials().get(1).toString() - // callScript(user, password) // call script + // callScript(user, password, "Plex") // call script } @SuppressLint("SetTextI18n") @@ -134,15 +229,14 @@ class MainActivity : AppCompatActivity() { } } - private fun callScript(user: String, password: String) { + private fun callScript(user: String, password: String, app: String) { if (ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ) { Toast.makeText(this@MainActivity, "No permission to use the internet", Toast.LENGTH_SHORT).show() this.finishAffinity() } else { - println("test du script") - Scraper(user, password, app = "Plex", debug = true).search() + Scraper(user, password, app = app, debug = true).search() } } diff --git a/app/src/main/res/drawable/ic_plus.xml b/app/src/main/res/drawable/ic_plus.xml new file mode 100644 index 0000000..03bdb45 --- /dev/null +++ b/app/src/main/res/drawable/ic_plus.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f80df9c..1a1daa0 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,27 @@ - - \ No newline at end of file + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/list_item_view.xml b/app/src/main/res/layout/list_item_view.xml new file mode 100644 index 0000000..31e3904 --- /dev/null +++ b/app/src/main/res/layout/list_item_view.xml @@ -0,0 +1,20 @@ + + + +