From 702b5e3dae4396d4ed5d23f0c9721b69099ed902 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 3 Sep 2021 00:47:56 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=86=95=20Adding=20Download=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/mylloon/mobidl/MainActivity.kt | 69 ++++++++++++++----- .../main/java/com/mylloon/mobidl/Scraper.kt | 40 +++++++++-- .../main/res/layout/activity_app_infos.xml | 14 +++- .../layout/list_item_view_app_download.xml | 19 +++++ 4 files changed, 120 insertions(+), 22 deletions(-) create mode 100644 app/src/main/res/layout/list_item_view_app_download.xml diff --git a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt index efd8c1e..ec803b7 100644 --- a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt +++ b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt @@ -350,9 +350,9 @@ class MainActivity : AppCompatActivity() { "${returns.size} ${getString(R.string.gotResults)} !", Toast.LENGTH_SHORT ).show() + listInfos = returns + showAppListPage() } - listInfos = returns - showAppListPage() } } } @@ -401,29 +401,66 @@ class MainActivity : AppCompatActivity() { recyclerViewAppList.adapter = Adapter(listInfosNames) } + @SuppressLint("SetTextI18n") private fun showAppInfoPage() { setContentView(R.layout.activity_app_infos) // display app info detailed page if (!settingsButtonVisible) toggleSettingsButtonVisibility() // check if the settings button isn't already showed and show it if necessary inAppInfo = true val link = appMobilismInfos!!["link"] if (link != null) { - runBlocking { // GlobalScope.launch { - val infoApp = Scraper().parseInfos(link) - findViewById(R.id.textViewAppName).text = appMobilismInfos!!["title"] - findViewById(R.id.textViewAppAuthor).text = appMobilismInfos!!["author"] - findViewById(R.id.textViewAppDate).text = appMobilismInfos!!["date"] - findViewById(R.id.textViewAppChangelogs).text = infoApp["changelogs"] + val infoApp: MutableMap + val links: MutableMap> + runBlocking { + infoApp = Scraper().parseInfos(link) + links = Scraper().parseDownloadLinks(infoApp["downloadLinks"]!!) + } + 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 parent page + val itemView = LayoutInflater.from(parent.context) + .inflate(R.layout.list_item_view_app_download, parent, false) + return ViewHolder(itemView) + } - println("\n") - println("Title: ${appMobilismInfos!!["title"]}\n") - println("Author: ${appMobilismInfos!!["author"]}\n") - println("Date of release: ${appMobilismInfos!!["date"]}\n") - println("Changelogs: \n${infoApp["changelogs"]}\n") - println("Download links: \n${infoApp["downloadLinks"]}") - println("\n\n---\n") + 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.download_button) + button?.setOnClickListener { + val splitText = button?.text.toString().split(Regex(" - ")) + startActivity( + Intent( + Intent.ACTION_VIEW, Uri.parse( + links[splitText[0]]!![splitText[1].replace("\\s".toRegex(), "").toInt() - 1] + ) + ) + ) + } + } + } + } + findViewById(R.id.textViewAppName).text = appMobilismInfos!!["title"] + findViewById(R.id.textViewAppAuthor).text = appMobilismInfos!!["author"] + findViewById(R.id.textViewAppDate).text = appMobilismInfos!!["date"] + findViewById(R.id.textViewAppChangelogs).text = infoApp["changelogs"] + + val listDownloads = mutableListOf() + for (arch in links.keys) { + val recyclerViewAppDownloads: RecyclerView = findViewById(R.id.recyclerViewAppDownloads) // get recyclerview + recyclerViewAppDownloads.layoutManager = LinearLayoutManager(this) + for ((count, _) in links[arch]!!.withIndex()) { + listDownloads += "$arch - ${count + 1}" + } + recyclerViewAppDownloads.adapter = Adapter(listDownloads) } } else Toast.makeText(instance, "${getString(R.string.noURL)}...", Toast.LENGTH_SHORT).show() } diff --git a/app/src/main/java/com/mylloon/mobidl/Scraper.kt b/app/src/main/java/com/mylloon/mobidl/Scraper.kt index c99d987..5cf4378 100644 --- a/app/src/main/java/com/mylloon/mobidl/Scraper.kt +++ b/app/src/main/java/com/mylloon/mobidl/Scraper.kt @@ -181,10 +181,10 @@ class Scraper( // elements["downloadLinks"] = null // } elements["changelogs"] = try { - val tmp = Regex("""What's New: ?
(.*)

T""").findAll(htmlPage) + var tmp = Regex("""What's New: ?
(.*)

T""").findAll(htmlPage) .map { it.groupValues[1] }.toList()[0] if (tmp.length < 2) { // if result none, trying other method - Regex("""What's New: ?
(.*)

T""").findAll(htmlPage) + tmp = Regex("""What's New: ?
(.*)

T""").findAll(htmlPage) .map { it.groupValues[1] }.toList()[0] } tmp.replace(Regex("""
\n?"""), "\n") // convert newline html to \n @@ -192,21 +192,51 @@ class Scraper( "No changelog found." } elements["downloadLinks"] = try { - htmlPage = htmlPage.replace(Regex("Download Instructions:
(.*)?

(.*?)?

?
(.*|[\s\S]*)

Trouble downloading|""").findAll(htmlPage) .map { it.groupValues[1] }.toList()[0] if (tmp.length < 2) { // if result none, trying other method - Regex("""Download Instructions: ?
(.*|[\s\S]*)""").findAll(htmlPage) + tmp = Regex("""Download Instructions: ?
(.*|[\s\S]*)""").findAll(htmlPage) .map { it.groupValues[1] }.toList()[0] } tmp = tmp.replace(Regex("""\n|||">(\S*)"""), "") // remove html garbage tmp = tmp.replace(Regex("""
\n?"""), "\n") // convert newline html to \n tmp = tmp.replace(Regex("""Mirrors(?!:)|Mirror(?!s)(?!:)"""), "Mirror:") // add ":" - tmp.split("""">""")[0] + var finalTmp = "" + for (i in tmp.split("\n")) finalTmp += i.split(">")[0] + "\n" + finalTmp.replace("\"", "") } catch (e: Exception) { null } return elements } + fun parseDownloadLinks(links: String): MutableMap> { + val downloadLinks = links.split("\n").filter { it != ""} + val categoryDL = mutableMapOf>() + var inCategory: String? = null + val noCategoryString = "No Category" + val tempNoCategoryDL = mutableListOf() + val tempCategoryDL = mutableListOf() + categoryDL[noCategoryString] = listOf("") // init the no category at index 0 of the map + for (downloadLink in downloadLinks) { + if (("http" in downloadLink) or ("www." in downloadLink)) { // lien + if (inCategory != null) tempCategoryDL.add(downloadLink) // if the link is inside a category + else tempNoCategoryDL.add(downloadLink) // else he don't have category + } else { // else it's a category + if ((inCategory != null) && (tempCategoryDL.size > 0)) { // already in category? + categoryDL[inCategory] = tempCategoryDL.toMutableList() + tempCategoryDL.clear() + } + var previous = "" + if (("Mirror" in downloadLink) and (inCategory != null)) previous = " of $inCategory" + inCategory = "${downloadLink.replace(":", "")}$previous" // first category + } + } + if (tempCategoryDL.isNotEmpty()) categoryDL[inCategory!!] = tempCategoryDL + if (tempNoCategoryDL.isNotEmpty()) categoryDL[noCategoryString] = tempNoCategoryDL else categoryDL.remove(noCategoryString) + + return categoryDL + } + } diff --git a/app/src/main/res/layout/activity_app_infos.xml b/app/src/main/res/layout/activity_app_infos.xml index dea5a9a..ebf18e9 100644 --- a/app/src/main/res/layout/activity_app_infos.xml +++ b/app/src/main/res/layout/activity_app_infos.xml @@ -58,7 +58,7 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/list_item_view_app_download.xml b/app/src/main/res/layout/list_item_view_app_download.xml new file mode 100644 index 0000000..7d1d141 --- /dev/null +++ b/app/src/main/res/layout/list_item_view_app_download.xml @@ -0,0 +1,19 @@ + + + +