From 74d307f1f5b111108297a74a77fc8f6ef5a5bcf3 Mon Sep 17 00:00:00 2001 From: Cyb3rKo Date: Mon, 14 Jun 2021 21:43:40 +0200 Subject: [PATCH] Optimize code and resources --- app/build.gradle | 3 + app/src/main/AndroidManifest.xml | 3 - .../logviewerforopenhab/MainActivity.kt | 123 +++++++------- .../appintro/AppIntro3rdFragment.kt | 28 ++-- .../appintro/AppIntro4thFragment.kt | 25 ++- .../appintro/MyAppIntro.kt | 3 - .../fragments/AnimationCreditsFragment.kt | 2 +- .../fragments/MainFragment.kt | 157 ++++++++---------- .../fragments/PreferenceFragment.kt | 9 - .../fragments/WebViewFragment.kt | 134 ++++++++------- app/src/main/res/layout/fragment_web_view.xml | 2 +- app/src/main/res/layout/nav_header_main.xml | 1 - .../main/res/menu/activity_main_drawer.xml | 1 - app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/colors.xml | 2 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/values/styles.xml | 1 + 17 files changed, 229 insertions(+), 267 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 672e978..1df698b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -52,6 +52,9 @@ android { packagingOptions { pickFirst 'META-INF/library_release.kotlin_module' } + buildFeatures { + viewBinding true + } } dependencies { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1326981..5a5fea4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,9 +4,6 @@ - - - navController.navigate(R.id.nav_settings) R.id.drawer_about -> navController.navigate(R.id.nav_about) R.id.drawer_end_user_consent -> { - var dialogMessage = getString(R.string.end_user_consent_2_message_1) - dialogMessage += mySPR.getString(CONSENT_DATE, getString(R.string.end_user_consent_2_date_not_found)) + - getString(R.string.end_user_consent_2_message_2) + - mySPR.getString(CONSENT_TIME, getString(R.string.end_user_consent_2_time_not_found)) - val spannableString = SpannableString(dialogMessage) - val drawerMenu = navView.menu - val clickableSpan1 = object : ClickableSpan() { - override fun onClick(view: View) { - showLicenseDialog(this@MainActivity, PRIVACY_POLICY) - } - } - val clickableSpan2 = object : ClickableSpan() { - override fun onClick(view: View) { - showLicenseDialog(this@MainActivity, TERMS_OF_USE) - } - } - var currentText = getString(R.string.end_user_consent_2_privacy_policy) - var index = dialogMessage.indexOf(currentText) - spannableString.setSpan( - clickableSpan1, index, index + currentText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE - ) - currentText = getString(R.string.end_user_consent_2_terms_of_use) - index = dialogMessage.indexOf(currentText) - spannableString.setSpan( - clickableSpan2, index, index + currentText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE - ) - currentText = getString(R.string.end_user_consent_2_date) - index = dialogMessage.indexOf(currentText) - for (i in 0..1) { - spannableString.setSpan(UnderlineSpan(), index, index + currentText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) - currentText = getString(R.string.end_user_consent_2_date) - index = dialogMessage.indexOf(currentText) - } - - MaterialDialog(this).show { - title(R.string.end_user_consent_2_title) - message(0, spannableString) { - messageTextView.movementMethod = LinkMovementMethod.getInstance() - } - positiveButton(android.R.string.ok) { - drawerMenu.findItem(R.id.drawer_end_user_consent).isChecked = false - } - negativeButton(R.string.end_user_consent_2_button_2) { - val analytics = FirebaseAnalytics.getInstance(applicationContext) - analytics.resetAnalyticsData() - analytics.setAnalyticsCollectionEnabled(false) - val crashlytics = FirebaseCrashlytics.getInstance() - crashlytics.deleteUnsentReports() - crashlytics.setCrashlyticsCollectionEnabled(false) - editor.clear().commit() - finish() - startActivity(Intent(applicationContext, this@MainActivity::class.java)) - } - onCancel { - drawerMenu.findItem(R.id.drawer_end_user_consent).isChecked = false - } - } + showEndUserConsent() } } it.isChecked = true @@ -153,8 +96,66 @@ class MainActivity : AppCompatActivity() { requestReview() } - private fun restoreConnections() { - showConnections(mySPR, getListOfConnections(mySPR), this) + private fun restoreConnections() = showConnections(mySPR, getListOfConnections(mySPR), this) + + private fun showEndUserConsent() { + var dialogMessage = getString(R.string.end_user_consent_2_message_1) + dialogMessage += mySPR.getString(CONSENT_DATE, getString(R.string.end_user_consent_2_date_not_found)) + + getString(R.string.end_user_consent_2_message_2) + + mySPR.getString(CONSENT_TIME, getString(R.string.end_user_consent_2_time_not_found)) + val spannableString = SpannableString(dialogMessage) + val drawerMenu = navView.menu + val clickableSpan1 = object : ClickableSpan() { + override fun onClick(view: View) { + showLicenseDialog(this@MainActivity, PRIVACY_POLICY) + } + } + val clickableSpan2 = object : ClickableSpan() { + override fun onClick(view: View) { + showLicenseDialog(this@MainActivity, TERMS_OF_USE) + } + } + var currentText = getString(R.string.end_user_consent_2_privacy_policy) + var index = dialogMessage.indexOf(currentText) + spannableString.setSpan( + clickableSpan1, index, index + currentText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE + ) + currentText = getString(R.string.end_user_consent_2_terms_of_use) + index = dialogMessage.indexOf(currentText) + spannableString.setSpan( + clickableSpan2, index, index + currentText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE + ) + currentText = getString(R.string.end_user_consent_2_date) + index = dialogMessage.indexOf(currentText) + repeat(2) { + spannableString.setSpan(UnderlineSpan(), index, index + currentText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + currentText = getString(R.string.end_user_consent_2_time) + index = dialogMessage.indexOf(currentText) + } + + MaterialDialog(this).show { + title(R.string.end_user_consent_2_title) + message(text = spannableString) { + messageTextView.movementMethod = LinkMovementMethod.getInstance() + } + positiveButton(android.R.string.ok) { + drawerMenu.findItem(R.id.drawer_end_user_consent).isChecked = false + } + negativeButton(R.string.end_user_consent_2_button_2) { + val analytics = FirebaseAnalytics.getInstance(applicationContext) + analytics.resetAnalyticsData() + analytics.setAnalyticsCollectionEnabled(false) + val crashlytics = FirebaseCrashlytics.getInstance() + crashlytics.deleteUnsentReports() + crashlytics.setCrashlyticsCollectionEnabled(false) + editor.clear().commit() + finish() + startActivity(Intent(applicationContext, this@MainActivity::class.java)) + } + onCancel { + drawerMenu.findItem(R.id.drawer_end_user_consent).isChecked = false + } + } } private fun requestReview() { diff --git a/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/AppIntro3rdFragment.kt b/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/AppIntro3rdFragment.kt index fb4f1e8..5715496 100644 --- a/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/AppIntro3rdFragment.kt +++ b/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/AppIntro3rdFragment.kt @@ -4,39 +4,33 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Button -import android.widget.CheckBox import androidx.fragment.app.Fragment import com.cyb3rko.logviewerforopenhab.PRIVACY_POLICY import com.cyb3rko.logviewerforopenhab.R import com.cyb3rko.logviewerforopenhab.TERMS_OF_USE +import com.cyb3rko.logviewerforopenhab.databinding.FragmentAppintro3Binding import com.cyb3rko.logviewerforopenhab.showLicenseDialog import com.github.appintro.SlidePolicy import es.dmoral.toasty.Toasty class AppIntro3rdFragment : Fragment(), SlidePolicy { + private var _binding: FragmentAppintro3Binding? = null + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! - private lateinit var button1: Button - private lateinit var button2: Button - private lateinit var checkBox1: CheckBox - private lateinit var checkBox2: CheckBox - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) - : View = inflater.inflate(R.layout.fragment_appintro3, container, false) + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + _binding = FragmentAppintro3Binding.inflate(inflater, container, false) + return binding.root + } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - button1 = view.findViewById(R.id.terms_of_use_button) - button2 = view.findViewById(R.id.privacy_policy_button) - checkBox1 = view.findViewById(R.id.terms_of_use_check) - checkBox2 = view.findViewById(R.id.privacy_policy_check) - - button1.setOnClickListener { showLicenseDialog(context, TERMS_OF_USE) } - button2.setOnClickListener { showLicenseDialog(context, PRIVACY_POLICY) } + binding.termsOfUseButton.setOnClickListener { showLicenseDialog(context, TERMS_OF_USE) } + binding.privacyPolicyButton.setOnClickListener { showLicenseDialog(context, PRIVACY_POLICY) } } override val isPolicyRespected: Boolean - get() = (checkBox1.isChecked && checkBox2.isChecked) + get() = (binding.termsOfUseCheck.isChecked && binding.privacyPolicyCheck.isChecked) override fun onUserIllegallyRequestedNextPage() { Toasty.error(requireContext(), getString(R.string.intro_fragment3_toast), Toasty.LENGTH_SHORT).show() diff --git a/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/AppIntro4thFragment.kt b/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/AppIntro4thFragment.kt index fe2e7f1..2e28320 100644 --- a/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/AppIntro4thFragment.kt +++ b/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/AppIntro4thFragment.kt @@ -1,37 +1,36 @@ package com.cyb3rko.logviewerforopenhab.appintro +import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.CheckBox import androidx.fragment.app.Fragment import com.cyb3rko.logviewerforopenhab.ANALYTICS_COLLECTION import com.cyb3rko.logviewerforopenhab.CRASHLYTICS_COLLECTION -import com.cyb3rko.logviewerforopenhab.R import com.cyb3rko.logviewerforopenhab.SHARED_PREFERENCE +import com.cyb3rko.logviewerforopenhab.databinding.FragmentAppintro4Binding class AppIntro4thFragment : Fragment() { + private var _binding: FragmentAppintro4Binding? = null + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! - private lateinit var checkBox1: CheckBox - private lateinit var checkBox2: CheckBox - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) - : View = inflater.inflate(R.layout.fragment_appintro4, container, false) + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + _binding = FragmentAppintro4Binding.inflate(inflater, container, false) + return binding.root + } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - checkBox1 = view.findViewById(R.id.analytics_check) - checkBox2 = view.findViewById(R.id.crashlytics_check) - - val mySPR = requireContext().getSharedPreferences(SHARED_PREFERENCE, 0) + val mySPR = requireContext().getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE) val editor = mySPR.edit() - checkBox1.setOnCheckedChangeListener { _, b -> + binding.analyticsCheck.setOnCheckedChangeListener { _, b -> editor.putBoolean(ANALYTICS_COLLECTION, b).apply() } - checkBox2.setOnCheckedChangeListener { _, b -> + binding.crashlyticsCheck.setOnCheckedChangeListener { _, b -> editor.putBoolean(CRASHLYTICS_COLLECTION, b).apply() } } diff --git a/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/MyAppIntro.kt b/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/MyAppIntro.kt index e048081..46d919e 100644 --- a/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/MyAppIntro.kt +++ b/app/src/main/java/com/cyb3rko/logviewerforopenhab/appintro/MyAppIntro.kt @@ -6,9 +6,6 @@ import android.os.Bundle import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.Fragment import com.cyb3rko.logviewerforopenhab.* -import com.cyb3rko.logviewerforopenhab.CONSENT_TIME -import com.cyb3rko.logviewerforopenhab.FIRST_START -import com.cyb3rko.logviewerforopenhab.SHARED_PREFERENCE import com.github.appintro.AppIntro import com.github.appintro.AppIntroFragment import com.github.appintro.AppIntroPageTransformerType diff --git a/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/AnimationCreditsFragment.kt b/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/AnimationCreditsFragment.kt index 77c98ef..cb08655 100644 --- a/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/AnimationCreditsFragment.kt +++ b/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/AnimationCreditsFragment.kt @@ -18,7 +18,7 @@ import com.cyb3rko.logviewerforopenhab.R class AnimationCreditsFragment : Fragment() { - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val information = listOf( Triple("error lottie", "koh", "https://lottiefiles.com/17373-error-lottie"), Triple("Face scanning", "Aneesh Ravi", "https://lottiefiles.com/4432-face-scanning"), diff --git a/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/MainFragment.kt b/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/MainFragment.kt index 21bcb55..ab29fb5 100644 --- a/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/MainFragment.kt +++ b/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/MainFragment.kt @@ -7,58 +7,39 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.inputmethod.InputMethodManager -import android.widget.* import androidx.core.view.get import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController import com.cyb3rko.logviewerforopenhab.* +import com.cyb3rko.logviewerforopenhab.databinding.FragmentMainBinding import com.google.android.material.button.MaterialButton -import com.google.android.material.button.MaterialButtonToggleGroup -import com.google.android.material.textfield.TextInputEditText -import com.google.android.material.textfield.TextInputLayout class MainFragment : Fragment() { + private var _binding: FragmentMainBinding? = null + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! + private lateinit var myContext: Context - private lateinit var connectButton: Button - private lateinit var connectCheck: CheckBox - private lateinit var editButton: Button private lateinit var editor: SharedPreferences.Editor - private lateinit var hostnameIPAddress: TextInputLayout - private lateinit var hostnameIPAddressCheck: CheckBox private lateinit var hostnameIPAddressString: String - private lateinit var hostnameIPAddressText: TextInputEditText - private lateinit var httpToggles: MaterialButtonToggleGroup private lateinit var link: String - private lateinit var linkView: TextView private lateinit var mySPR: SharedPreferences - private lateinit var port: TextInputLayout - private lateinit var portCheck: CheckBox private var portInt = 0 - private lateinit var portText: TextInputEditText override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val v = inflater.inflate(R.layout.fragment_main, container, false) - connectButton = v.findViewById(R.id.connect_button) - connectCheck = v.findViewById(R.id.connect_check) - editButton = v.findViewById(R.id.edit_button) - hostnameIPAddressCheck = v.findViewById(R.id.hostname_ip_address_check) - portCheck = v.findViewById(R.id.port_check) - hostnameIPAddress = v.findViewById(R.id.hostname_ip_address) - httpToggles = v.findViewById(R.id.http_toggles) - port = v.findViewById(R.id.port) - hostnameIPAddressText = v.findViewById(R.id.hostname_ip_address_text) - portText = v.findViewById(R.id.port_text) - linkView = v.findViewById(R.id.link_view) - - mySPR = v.context.getSharedPreferences(SHARED_PREFERENCE, 0) + _binding = FragmentMainBinding.inflate(inflater, container, false) + val root = binding.root + myContext = requireContext() + + mySPR = myContext.getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE) editor = mySPR.edit() editor.apply() setEditButtonClickListener() - setConnectButtonClickListener(v) + setConnectButtonClickListener(root) setConnectCheckClickListener() - return v + return root } override fun onStart() { @@ -68,45 +49,45 @@ class MainFragment : Fragment() { private fun statusRestoring() { if (!mySPR.getBoolean(HTTPS_ACTIVATED, false)) { - httpToggles.check((httpToggles[0] as MaterialButton).id) + binding.httpToggles.check((binding.httpToggles[0] as MaterialButton).id) } else { - httpToggles.check((httpToggles[1] as MaterialButton).id) + binding.httpToggles.check((binding.httpToggles[1] as MaterialButton).id) } - connectCheck.isChecked = mySPR.getBoolean(CONNECT_CHECK, false) + binding.connectCheck.isChecked = mySPR.getBoolean(CONNECT_CHECK, false) if (mySPR.getString(HOSTNAME_STRING, "") == "" || mySPR.getString(HOSTNAME_STRING, "") == "0") { - hostnameIPAddressText.setText("") - hostnameIPAddress.isEnabled = true + binding.hostnameIpAddressText.setText("") + binding.hostnameIpAddress.isEnabled = true } else { - hostnameIPAddressText.setText(mySPR.getString(HOSTNAME_STRING, "0")) + binding.hostnameIpAddressText.setText(mySPR.getString(HOSTNAME_STRING, "0")) hostnameIPAddressString = mySPR.getString(HOSTNAME_STRING, "0")!! - hostnameIPAddress.isEnabled = false + binding.hostnameIpAddress.isEnabled = false } - hostnameIPAddressCheck.isChecked = mySPR.getBoolean(HOSTNAME_CHECK, true) + binding.hostnameIpAddressCheck.isChecked = mySPR.getBoolean(HOSTNAME_CHECK, true) if (mySPR.getInt(PORT_INT, 0) == 0) { - portText.setText("") - hostnameIPAddress.isEnabled = true + binding.portText.setText("") + binding.hostnameIpAddress.isEnabled = true } else { - portText.setText(mySPR.getInt(PORT_INT, 0).toString()) - port.isEnabled = false + binding.portText.setText(mySPR.getInt(PORT_INT, 0).toString()) + binding.port.isEnabled = false portInt = mySPR.getInt(PORT_INT, 9001) } - portCheck.isChecked = mySPR.getBoolean(PORT_CHECK, true) + binding.portCheck.isChecked = mySPR.getBoolean(PORT_CHECK, true) - val hostname = hostnameIPAddressText.text.toString() - val port = portText.text.toString() + val hostname = binding.hostnameIpAddressText.text.toString() + val port = binding.portText.text.toString() if (hostname.isNotEmpty() && port.isNotEmpty()) { linkGeneration(hostname, port) - httpToggles[0].isEnabled = false - httpToggles[1].isEnabled = false - hostnameIPAddressCheck.isEnabled = false - portCheck.isEnabled = false - connectCheck.isEnabled = true - connectButton.text = getString(R.string.main_connect_button_2) - editButton.isEnabled = true + binding.httpToggles[0].isEnabled = false + binding.httpToggles[1].isEnabled = false + binding.hostnameIpAddressCheck.isEnabled = false + binding.portCheck.isEnabled = false + binding.connectCheck.isEnabled = true + binding.connectButton.text = getString(R.string.main_connect_button_2) + binding.editButton.isEnabled = true } } @@ -121,31 +102,31 @@ class MainFragment : Fragment() { "http" } else "https" link = "$prefix://$hostnameIPAddressString:$portInt" - linkView.text = link + binding.linkView.text = link } private fun setConnectButtonClickListener(v: View) { - connectButton.setOnClickListener { view -> - val tempHostname = hostnameIPAddressText.text.toString().trim() - val tempPort = portText.text.toString().trim() + binding.connectButton.setOnClickListener { view -> + val tempHostname = binding.hostnameIpAddressText.text.toString().trim() + val tempPort = binding.portText.text.toString().trim() if (tempHostname.isNotEmpty()) { - hostnameIPAddress.error = "" - if (linkView.text.toString().isEmpty()) { + binding.hostnameIpAddress.error = "" + if (binding.linkView.text.toString().isEmpty()) { linkGeneration(tempHostname, tempPort) - httpToggles[0].isEnabled = false - httpToggles[1].isEnabled = false - hostnameIPAddress.isEnabled = false - port.isEnabled = false - hostnameIPAddressCheck.isEnabled = false - portCheck.isEnabled = false - connectCheck.isEnabled = true - editButton.isEnabled = true + binding.httpToggles[0].isEnabled = false + binding.httpToggles[1].isEnabled = false + binding.hostnameIpAddress.isEnabled = false + binding.port.isEnabled = false + binding.hostnameIpAddressCheck.isEnabled = false + binding.portCheck.isEnabled = false + binding.connectCheck.isEnabled = true + binding.editButton.isEnabled = true - val httpMode = (httpToggles[1] as MaterialButton).isChecked + val httpMode = (binding.httpToggles[1] as MaterialButton).isChecked editor.putBoolean(HTTPS_ACTIVATED, httpMode) - if (hostnameIPAddressCheck.isChecked) { + if (binding.hostnameIpAddressCheck.isChecked) { editor.putString(HOSTNAME_STRING, hostnameIPAddressString) editor.putBoolean(HOSTNAME_CHECK, true) } else { @@ -153,24 +134,24 @@ class MainFragment : Fragment() { editor.putBoolean(HOSTNAME_CHECK, false) } - if (portCheck.isChecked) { + if (binding.portCheck.isChecked) { editor.putBoolean(PORT_CHECK, true) - if (portText.text.toString().trim().isNotEmpty()) { + if (binding.portText.text.toString().trim().isNotEmpty()) { editor.putInt(PORT_INT, portInt) } else { - portText.setText(9001.toString()) + binding.portText.setText(9001.toString()) editor.putInt(PORT_INT, 9001) } } else { - portText.setText(9001.toString()) + binding.portText.setText(9001.toString()) editor.putInt(PORT_INT, 0) editor.putBoolean(PORT_CHECK, false) } editor.putString(LINK, link).apply() - connectButton.text = getString(R.string.main_connect_button_2) + binding.connectButton.text = getString(R.string.main_connect_button_2) } else { if (view != null) { val imm = view.context.applicationContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager @@ -179,11 +160,11 @@ class MainFragment : Fragment() { findNavController().navigate(R.id.nav_webview) if (mySPR.getBoolean(CONNECTION_OVERVIEW_ENABLED, true)) { - storeAndShowConnection((httpToggles[1] as MaterialButton).isChecked, tempHostname, tempPort.toInt()) + storeAndShowConnection((binding.httpToggles[1] as MaterialButton).isChecked, tempHostname, tempPort.toInt()) } } } else { - hostnameIPAddress.error = getString(R.string.error_fill_out) + binding.hostnameIpAddress.error = getString(R.string.error_fill_out) } } } @@ -210,22 +191,22 @@ class MainFragment : Fragment() { } private fun setEditButtonClickListener() { - editButton.setOnClickListener { - httpToggles[0].isEnabled = true - httpToggles[1].isEnabled = true - hostnameIPAddress.isEnabled = true - hostnameIPAddressCheck.isEnabled = true - port.isEnabled = true - portCheck.isEnabled = true - connectCheck.isEnabled = false - connectButton.text = getString(R.string.main_connect_button_1) - editButton.isEnabled = false - linkView.text = "" + binding.editButton.setOnClickListener { + binding.httpToggles[0].isEnabled = true + binding.httpToggles[1].isEnabled = true + binding.hostnameIpAddress.isEnabled = true + binding.hostnameIpAddressCheck.isEnabled = true + binding.port.isEnabled = true + binding.portCheck.isEnabled = true + binding.connectCheck.isEnabled = false + binding.connectButton.text = getString(R.string.main_connect_button_1) + binding.editButton.isEnabled = false + binding.linkView.text = "" } } private fun setConnectCheckClickListener() { - connectCheck.setOnCheckedChangeListener { _, b -> + binding.connectCheck.setOnCheckedChangeListener { _, b -> editor.putBoolean(CONNECT_CHECK, b) editor.putBoolean(AUTO_START, b).apply() } diff --git a/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/PreferenceFragment.kt b/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/PreferenceFragment.kt index e9269c6..db1f7e6 100644 --- a/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/PreferenceFragment.kt +++ b/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/PreferenceFragment.kt @@ -15,15 +15,6 @@ import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.list.isItemChecked import com.afollestad.materialdialogs.list.listItemsMultiChoice import com.cyb3rko.logviewerforopenhab.* -import com.cyb3rko.logviewerforopenhab.ANALYTICS_COLLECTION -import com.cyb3rko.logviewerforopenhab.CONNECTION_OVERVIEW_ENABLED -import com.cyb3rko.logviewerforopenhab.CRASHLYTICS_COLLECTION -import com.cyb3rko.logviewerforopenhab.DATA_DELETION -import com.cyb3rko.logviewerforopenhab.ORIENTATION -import com.cyb3rko.logviewerforopenhab.SHARED_PREFERENCE -import com.cyb3rko.logviewerforopenhab.getListOfConnections -import com.cyb3rko.logviewerforopenhab.hideConnections -import com.cyb3rko.logviewerforopenhab.showConnections import com.google.firebase.analytics.FirebaseAnalytics import com.google.firebase.crashlytics.FirebaseCrashlytics import es.dmoral.toasty.Toasty diff --git a/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/WebViewFragment.kt b/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/WebViewFragment.kt index 05dc13f..c082637 100644 --- a/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/WebViewFragment.kt +++ b/app/src/main/java/com/cyb3rko/logviewerforopenhab/fragments/WebViewFragment.kt @@ -1,6 +1,7 @@ package com.cyb3rko.logviewerforopenhab.fragments import android.annotation.SuppressLint +import android.content.Context import android.content.SharedPreferences import android.content.pm.ActivityInfo import android.os.Bundle @@ -17,16 +18,9 @@ import androidx.navigation.fragment.findNavController import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.customview.customView import com.cyb3rko.logviewerforopenhab.* -import com.cyb3rko.logviewerforopenhab.FIRST_START_WEB -import com.cyb3rko.logviewerforopenhab.LINK -import com.cyb3rko.logviewerforopenhab.ORIENTATION -import com.cyb3rko.logviewerforopenhab.SHARED_PREFERENCE -import com.cyb3rko.logviewerforopenhab.TEXTSIZE_AUTO -import com.cyb3rko.logviewerforopenhab.TEXTSIZE_LANDSCAPE -import com.cyb3rko.logviewerforopenhab.TEXTSIZE_PORTRAIT +import com.cyb3rko.logviewerforopenhab.databinding.FragmentWebViewBinding import com.getkeepsafe.taptargetview.TapTarget import com.getkeepsafe.taptargetview.TapTargetSequence -import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.firebase.analytics.FirebaseAnalytics import com.google.firebase.analytics.ktx.analytics import com.google.firebase.analytics.ktx.logEvent @@ -36,35 +30,70 @@ import es.dmoral.toasty.Toasty import kotlinx.android.synthetic.main.fragment_web_view.* class WebViewFragment : Fragment() { + private var _binding: FragmentWebViewBinding? = null + // This property is only valid between onCreateView and onDestroyView. + private val binding get() = _binding!! + private lateinit var myContext: Context - private lateinit var backButton: FloatingActionButton - private lateinit var textButton: FloatingActionButton - private lateinit var viewButton: FloatingActionButton private lateinit var mySPR: SharedPreferences private lateinit var editor: SharedPreferences.Editor private lateinit var textSizeType: String private lateinit var webSettings: WebSettings - private lateinit var webView: WebView private var viewLocked = true @SuppressLint("SetJavaScriptEnabled") override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val v = inflater.inflate(R.layout.fragment_web_view, container, false) - webView = v.findViewById(R.id.webview) - viewButton = v.findViewById(R.id.view_button) - textButton = v.findViewById(R.id.text_button) - backButton = v.findViewById(R.id.back_button) + _binding = FragmentWebViewBinding.inflate(inflater, container, false) + val root = binding.root + myContext = requireContext() - mySPR = v.context.getSharedPreferences(SHARED_PREFERENCE, 0) + mySPR = myContext.getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE) editor = mySPR.edit() editor.apply() - webSettings = webView.settings - webView.webViewClient = object : WebViewClient() { + setUpWebview() + root.isVerticalScrollBarEnabled = false + setTouchable(false) + + textSizeType = when (mySPR.getString(ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT.toString())?.toInt()) { + ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED -> TEXTSIZE_AUTO + ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE -> TEXTSIZE_LANDSCAPE + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT -> TEXTSIZE_PORTRAIT + else -> { "" } + } + webSettings.textZoom = mySPR.getInt(textSizeType, 60) + + setToolbarVisibility(activity, View.GONE) + return root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding.webview.loadUrl(mySPR.getString(LINK, "")!!) + + val handler = Handler(Looper.getMainLooper()) + val runnable = object : Runnable { + override fun run() { + if (viewLocked) { + binding.webview.scrollBy(0, 10000) + } + handler.postDelayed(this, 1000) + } + } + handler.postDelayed(runnable, 500) + setViewButtonClickListener() + setTextButtonClickListener(view) + setBackButtonClickListener() + showTapTargetSequence(view) + } + + private fun setUpWebview() { + webSettings = binding.webview.settings + binding.webview.webViewClient = object : WebViewClient() { override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) { view_button.visibility = View.GONE text_button.visibility = View.GONE - webView.visibility = View.GONE + binding.webview.visibility = View.GONE animation_view.visibility = View.VISIBLE when (errorCode) { -8 -> { @@ -131,39 +160,6 @@ class WebViewFragment : Fragment() { } webSettings.javaScriptEnabled = true webSettings.domStorageEnabled = true - v.isVerticalScrollBarEnabled = false - setTouchable(false) - - textSizeType = when (mySPR.getString(ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT.toString())?.toInt()) { - ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED -> TEXTSIZE_AUTO - ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE -> TEXTSIZE_LANDSCAPE - ActivityInfo.SCREEN_ORIENTATION_PORTRAIT -> TEXTSIZE_PORTRAIT - else -> { "" } - } - webSettings.textZoom = mySPR.getInt(textSizeType, 60) - - setToolbarVisibility(activity, View.GONE) - return v - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - webView.loadUrl(mySPR.getString(LINK, "")!!) - - val handler = Handler(Looper.getMainLooper()) - val runnable = object : Runnable { - override fun run() { - if (viewLocked) { - webView.scrollBy(0, 10000) - } - handler.postDelayed(this, 1000) - } - } - handler.postDelayed(runnable, 500) - setViewButtonClickListener() - setTextButtonClickListener(view) - setBackButtonClickListener() - showTapTargetSequence(view) } private fun logUnknownError(errorCode: Int, description: String) { @@ -174,31 +170,31 @@ class WebViewFragment : Fragment() { } private fun setViewButtonClickListener() { - viewButton.setOnClickListener { view -> + binding.viewButton.setOnClickListener { if (viewLocked) { - viewButton.setImageResource(R.drawable._ic_lock_2) + binding.viewButton.setImageResource(R.drawable._ic_lock_2) viewLocked = false setTouchable(true) - Toasty.info(view.context, getString(R.string.lock_button_1), Toasty.LENGTH_SHORT).show() + Toasty.info(myContext, getString(R.string.lock_button_1), Toasty.LENGTH_SHORT).show() } else { - viewButton.setImageResource(R.drawable._ic_lock) + binding.viewButton.setImageResource(R.drawable._ic_lock) viewLocked = true setTouchable(false) - webView.scrollBy(0, 10000) + binding.webview.scrollBy(0, 10000) - Toasty.info(view.context, getString(R.string.lock_button_2), Toasty.LENGTH_SHORT).show() + Toasty.info(myContext, getString(R.string.lock_button_2), Toasty.LENGTH_SHORT).show() } } } private fun setTextButtonClickListener(v: View) { - textButton.setOnClickListener { - val content = LinearLayout(v.context) + binding.textButton.setOnClickListener { + val content = LinearLayout(myContext) content.orientation = LinearLayout.VERTICAL - val sizeView = TextView(v.context) - val discreteSeekBar = DiscreteSeekBar(v.context) + val sizeView = TextView(myContext) + val discreteSeekBar = DiscreteSeekBar(myContext) val currentTextSize = mySPR.getInt(textSizeType, 60) sizeView.text = String.format(getString(R.string.text_size_dialog_text), currentTextSize, currentTextSize) @@ -225,14 +221,14 @@ class WebViewFragment : Fragment() { }) content.addView(discreteSeekBar) - MaterialDialog(requireContext()).show { + MaterialDialog(myContext).show { title(R.string.text_size_dialog_title) customView(0, content) positiveButton(R.string.text_size_dialog_button_1) { val textSize = discreteSeekBar.progress webSettings.textZoom = textSize editor.putInt(textSizeType, textSize).apply() - Toasty.info(v.context, getString(R.string.text_size_changed) + textSize, Toasty.LENGTH_SHORT).show() + Toasty.info(myContext, getString(R.string.text_size_changed) + textSize, Toasty.LENGTH_SHORT).show() } negativeButton(R.string.text_size_dialog_button_2) } @@ -240,7 +236,7 @@ class WebViewFragment : Fragment() { } private fun setBackButtonClickListener() { - backButton.setOnClickListener { + binding.backButton.setOnClickListener { findNavController().navigate(R.id.nav_menu) setToolbarVisibility(activity, View.VISIBLE) } @@ -309,9 +305,9 @@ class WebViewFragment : Fragment() { @SuppressLint("ClickableViewAccessibility") private fun setTouchable(b: Boolean) { if (b) { - webView.setOnTouchListener { _, _ -> false } + binding.webview.setOnTouchListener { _, _ -> false } } else { - webView.setOnTouchListener { _, _ -> true } + binding.webview.setOnTouchListener { _, _ -> true } } } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_web_view.xml b/app/src/main/res/layout/fragment_web_view.xml index 74b79b3..f97e5a3 100644 --- a/app/src/main/res/layout/fragment_web_view.xml +++ b/app/src/main/res/layout/fragment_web_view.xml @@ -107,6 +107,6 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent" - android:text="Retry" /> + android:text="@string/webview_retry_button_desc" /> \ No newline at end of file diff --git a/app/src/main/res/layout/nav_header_main.xml b/app/src/main/res/layout/nav_header_main.xml index c916de8..8bb9f17 100644 --- a/app/src/main/res/layout/nav_header_main.xml +++ b/app/src/main/res/layout/nav_header_main.xml @@ -1,6 +1,5 @@ diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index d3a9fc4..33a200d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -22,6 +22,7 @@ lock font icon back arrow + Retry Connection timed out Connection refusedt diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 5009758..6eaef3e 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,6 @@ #ff5722 #bf360c #FF6332 + + #FFFFFF diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d3a9fc4..33a200d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -22,6 +22,7 @@ lock font icon back arrow + Retry Connection timed out Connection refusedt diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 1d6618b..a1c57e3 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -12,6 +12,7 @@