z1tr0t3c / blistar-android

Mit der blistar-App bleibst du immer auf dem neuesten Stand!
https://zitrotec.de/x/blista
0 stars 0 forks source link

Some code design suggestions #1

Open FluentCoding opened 3 years ago

FluentCoding commented 3 years ago

Small thing I've noticed - you are repeating the code segment that is handling the download of the files a lot. You should generally t ry to abstract these code segments into one method (and make a parameter for each thing that changes inside a code segment). For example:

    R.id.navigation_vplan1 -> {
            val myWebView: WebView = findViewById(R.id.webview)
            myWebView.settings.javaScriptEnabled = true
            webview.webViewClient = WebViewClient()
            myWebView.loadUrl("https://edu.blista.de/moodle/mod/resource/view.php?id=39")

            webview.setDownloadListener({ url, userAgent, contentDisposition, mimeType, contentLength ->
                val request = DownloadManager.Request(Uri.parse(url))
                request.setMimeType(mimeType)
                request.addRequestHeader("cookie", CookieManager.getInstance().getCookie(url))
                request.addRequestHeader("User-Agent", userAgent)
                request.setDescription("Downloading file...")
                request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType))
                request.allowScanningByMediaScanner()
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                request.setDestinationInExternalFilesDir(this@MainActivity, Environment.DIRECTORY_DOWNLOADS, ".png")
                val dm = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
                dm.enqueue(request)
                Toast.makeText(applicationContext, "Lade Datei...", Toast.LENGTH_LONG).show()
            })

            myWebView.setWebViewClient(object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
                    if (url != null && url.startsWith("mailto:")) {
                        view.context.startActivity(
                            Intent(Intent.ACTION_VIEW, Uri.parse(url))
                        )
                        return true
                    } else if (url != null && url.startsWith("tel:")) {
                        view.context.startActivity(
                            Intent(Intent.ACTION_VIEW, Uri.parse(url))
                        )
                        return true
                    } else {
                        return false
                    }
                }
            })

            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_vplan2 -> {

            val myWebView: WebView = findViewById(R.id.webview)
            myWebView.settings.javaScriptEnabled = true
            webview.webViewClient = WebViewClient()
            myWebView.loadUrl("https://edu.blista.de/moodle/mod/resource/view.php?id=41")

            webview.setDownloadListener({ url, userAgent, contentDisposition, mimeType, contentLength ->
                val request = DownloadManager.Request(Uri.parse(url))
                request.setMimeType(mimeType)
                request.addRequestHeader("cookie", CookieManager.getInstance().getCookie(url))
                request.addRequestHeader("User-Agent", userAgent)
                request.setDescription("Downloading file...")
                request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType))
                request.allowScanningByMediaScanner()
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                request.setDestinationInExternalFilesDir(this@MainActivity, Environment.DIRECTORY_DOWNLOADS, ".png")
                val dm = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
                dm.enqueue(request)
                Toast.makeText(applicationContext, "Lade Datei...", Toast.LENGTH_LONG).show()
            })

            myWebView.setWebViewClient(object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
                    if (url != null && url.startsWith("mailto:")) {
                        view.context.startActivity(
                            Intent(Intent.ACTION_VIEW, Uri.parse(url))
                        )
                        return true
                    } else if (url != null && url.startsWith("tel:")) {
                        view.context.startActivity(
                            Intent(Intent.ACTION_VIEW, Uri.parse(url))
                        )
                        return true
                    } else {
                        return false
                    }
                }
            })

            return@OnNavigationItemSelectedListener true
}

You could easily make a method for the lambda you insert into the webview downloadListener because the logic simply the same. You could also make a method that handles the loading of a webview (and make a parameter because the url changes everytime).

There are many other places in the code where you duplicated the code so I would suggest you to skim over your code and find these occurrences. Also, maybe you should change some variable names so that they conform to the naming convention that your project (or all general java projects) use and use a more understandable name: navigation_vplan2 is snake_case which doesn't fit to the other namings in this project. You might want to use navigationVPlan2 instead. Also, you should try to avoid numbers in your variable. In general, vplan2 seems to be a weird name so someone like me who doesn't know what this project aims to do should be able to directly know what role this variable has.

Have a nice day!

z1tr0t3c commented 3 years ago

Thank you for your suggestions!