🆕 Add main page

This commit is contained in:
Mylloon 2021-08-29 21:00:57 +02:00
parent 936607a4de
commit 2674c3a705
4 changed files with 164 additions and 21 deletions

View file

@ -12,26 +12,26 @@ import android.os.Handler
import android.os.Looper import android.os.Looper
import android.text.Editable import android.text.Editable
import android.text.TextWatcher 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.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import android.content.pm.PackageInfo import android.content.pm.PackageInfo
import android.net.Uri 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() { class MainActivity : AppCompatActivity() {
private var settingsButton: Menu? = null // before starting the app there is no settings button 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 inSettings: Boolean = false // by default your not in settings page
private var prefs: SharedPreferences? = null // first run detection private var prefs: SharedPreferences? = null // first run detection
private val sharedPref = "com.mylloon.MobiDL" // shared pref name
init {
instance = this
}
companion object { companion object {
private var instance: MainActivity? = null private var instance: MainActivity? = null
@ -41,11 +41,26 @@ class MainActivity : AppCompatActivity() {
} }
} }
init {
instance = this
}
private fun toggleSettingsButtonVisibility() { // Change Settings button visibility private fun toggleSettingsButtonVisibility() { // Change Settings button visibility
val visibility: Boolean = settingsButton?.findItem(R.id.settingsButton)?.isVisible == true val visibility: Boolean = settingsButton?.findItem(R.id.settingsButton)?.isVisible == true
settingsButton?.findItem(R.id.settingsButton)?.isVisible = !visibility settingsButton?.findItem(R.id.settingsButton)?.isVisible = !visibility
} }
private fun getValuesRecyclerView(): MutableList<String> { // 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<String>? = null
data = sharedPref.getStringSet("apps", null)
return data?.toMutableList() ?: mutableListOf<String>()
}
override fun onCreate(savedInstanceState: Bundle?) { // Main function override fun onCreate(savedInstanceState: Bundle?) { // Main function
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
prefs = getSharedPreferences("com.mylloon.MobiDL", MODE_PRIVATE) prefs = getSharedPreferences("com.mylloon.MobiDL", MODE_PRIVATE)
@ -98,14 +113,94 @@ class MainActivity : AppCompatActivity() {
} }
} }
private fun mainPage() { private fun mainPage(toDelete: String? = null) {
setContentView(R.layout.activity_main) if (toDelete == null) {
setContentView(R.layout.activity_main) // display main page
Handler(Looper.getMainLooper()).postDelayed({ Handler(Looper.getMainLooper()).postDelayed({
toggleSettingsButtonVisibility() toggleSettingsButtonVisibility()
}, 50) }, 100) // delay to add button settings
}
val valuesRecyclerView = getValuesRecyclerView() // list of all the element in the main page
class Adapter(private val values: List<String>): RecyclerView.Adapter<Adapter.ViewHolder>() {
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<FloatingActionButton>(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() // user = Credentials().get(0).toString()
// password = Credentials().get(1).toString() // password = Credentials().get(1).toString()
// callScript(user, password) // call script // callScript(user, password, "Plex") // call script
} }
@SuppressLint("SetTextI18n") @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) if (ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED != PackageManager.PERMISSION_GRANTED
) { ) {
Toast.makeText(this@MainActivity, "No permission to use the internet", Toast.LENGTH_SHORT).show() Toast.makeText(this@MainActivity, "No permission to use the internet", Toast.LENGTH_SHORT).show()
this.finishAffinity() this.finishAffinity()
} else { } else {
println("test du script") Scraper(user, password, app = app, debug = true).search()
Scraper(user, password, app = "Plex", debug = true).search()
} }
} }

View file

@ -0,0 +1,11 @@
<vector
android:height="48dp"
android:tint="@color/wisteria"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="48dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="@color/wisteria"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM17,13h-4v4h-2v-4L7,13v-2h4L11,7h2v4h4v2z" />
</vector>

View file

@ -1,9 +1,27 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout <FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout> <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="10dp"
android:clickable="true"
android:src="@drawable/ic_plus"
app:maxImageSize="67dp"
tools:ignore="ContentDescription"
android:focusable="true" />
</FrameLayout>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/text_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp" />
</LinearLayout>