Skip to content

Commit

Permalink
Replace Dialog and Slider library with native Material You elements
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Sep 22, 2022
1 parent a50e645 commit 01dea6f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 92 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0'
implementation 'com.afollestad.material-dialogs:core:3.3.0'
implementation 'com.airbnb.android:lottie:4.0.0'
implementation 'com.getkeepsafe.taptargetview:taptargetview:1.13.0'
implementation 'com.github.AppIntro:AppIntro:6.0.0'
implementation 'com.github.GrenderG:Toasty:1.5.0'
implementation 'com.github.cyb3rko:about-icons:1.5.0'
implementation 'com.github.mardous:DiscreteSeekBar:1.1.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'com.google.android.play:core-ktx:1.8.1'
implementation 'com.google.firebase:firebase-analytics-ktx' // BOM versioning
Expand Down
71 changes: 31 additions & 40 deletions app/src/main/java/com/cyb3rko/logviewerforopenhab/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import android.net.Uri
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.UnderlineSpan
import android.view.View
import androidx.appcompat.app.ActionBarDrawerToggle
Expand All @@ -20,10 +18,10 @@ import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onCancel
import com.cyb3rko.logviewerforopenhab.appintro.MyAppIntro
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.navigation.NavigationView
import com.google.android.play.core.review.ReviewManagerFactory
import com.google.firebase.analytics.FirebaseAnalytics
Expand Down Expand Up @@ -112,48 +110,31 @@ class MainActivity : AppCompatActivity() {

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))
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 currentText = getString(R.string.end_user_consent_2_date)
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)
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) {
val drawerMenu = navView.menu

MaterialAlertDialogBuilder(this)
.setTitle(R.string.end_user_consent_2_title)
.setMessage(spannableString)
.setView(R.layout.dialog_end_user_consent)
.setPositiveButton(android.R.string.ok) { _, _ ->
drawerMenu.findItem(R.id.drawer_end_user_consent).isChecked = false
}
negativeButton(R.string.end_user_consent_2_button_2) {
.setNegativeButton(R.string.end_user_consent_2_button_2) { _, _ ->
val analytics = FirebaseAnalytics.getInstance(applicationContext)
analytics.resetAnalyticsData()
analytics.setAnalyticsCollectionEnabled(false)
Expand All @@ -164,10 +145,20 @@ class MainActivity : AppCompatActivity() {
finish()
startActivity(Intent(applicationContext, this@MainActivity::class.java))
}
onCancel {
.setOnCancelListener {
drawerMenu.findItem(R.id.drawer_end_user_consent).isChecked = false
}
}
.create().apply {
setOnShowListener {
window?.findViewById<MaterialButton>(R.id.privacy_policy_button)?.setOnClickListener {
showLicenseDialog(this@MainActivity, PRIVACY_POLICY)
}
window?.findViewById<MaterialButton>(R.id.terms_of_use_button)?.setOnClickListener {
showLicenseDialog(this@MainActivity, TERMS_OF_USE)
}
}
}
.show()
}

private fun requestReview() {
Expand Down Expand Up @@ -215,4 +206,4 @@ class MainActivity : AppCompatActivity() {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.isItemChecked
import com.afollestad.materialdialogs.list.listItemsMultiChoice
import com.cyb3rko.logviewerforopenhab.*
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.crashlytics.FirebaseCrashlytics
import es.dmoral.toasty.Toasty
Expand Down Expand Up @@ -89,31 +87,35 @@ class PreferenceFragment : PreferenceFragmentCompat() {
val connectionList = getListOfConnections(
requireActivity().getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE))
if (connectionList.isEmpty()) {
MaterialDialog(myContext).show {
title(R.string.settings_manage_connections_dialog_title)
message(R.string.settings_manage_connections_dialog1_message)
positiveButton(android.R.string.ok)
}
MaterialAlertDialogBuilder(myContext)
.setTitle(R.string.settings_manage_connections_dialog_title)
.setMessage(R.string.settings_manage_connections_dialog1_message)
.setPositiveButton(android.R.string.ok, null)
.show()
return true
}
val connectionLinks = mutableListOf<String>()
connectionList.forEach { connectionLinks.add(it.toLink()) }
MaterialDialog(myContext).show {
title(R.string.settings_manage_connections_dialog_title)
listItemsMultiChoice(items = connectionLinks.toList())
positiveButton(R.string.settings_manage_connections_dialog2_button) {
val selection = mutableListOf<Int>()
repeat(connectionList.size) { i ->
if (it.isItemChecked(i)) selection.add(i)
}
selection.reversed().forEach { i ->
connectionList.removeAt(i)

val checkedItems = BooleanArray(connectionLinks.size) { false }

MaterialAlertDialogBuilder(myContext)
.setTitle(R.string.settings_manage_connections_dialog_title)
.setMultiChoiceItems(
connectionLinks.toTypedArray(),
checkedItems
) { _, index, checked ->
checkedItems[index] = checked
}
.setPositiveButton(R.string.settings_manage_connections_dialog2_button) { _, _ ->
checkedItems.forEachIndexed { index, b ->
if (b) connectionList.removeAt(index)
val newConnections = connectionList.joinToString(";")
mySPR.edit().putString(CONNECTIONS, newConnections).apply()
showConnections(mySPR, connectionList, myContext as Activity)
}
val newConnections = connectionList.joinToString(";")
mySPR.edit().putString(CONNECTIONS, newConnections).apply()
showConnections(mySPR, connectionList, myContext as Activity)
}
}
.show()
true
} else {
Toast.makeText(myContext, R.string.settings_manage_connections_error, Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.cyb3rko.logviewerforopenhab.*
import com.cyb3rko.logviewerforopenhab.databinding.FragmentWebViewBinding
import com.getkeepsafe.taptargetview.TapTarget
import com.getkeepsafe.taptargetview.TapTargetSequence
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.slider.Slider
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.analytics.ktx.logEvent
import com.google.firebase.ktx.Firebase
import com.mardous.discreteseekbar.DiscreteSeekBar
import es.dmoral.toasty.Toasty

class WebViewFragment : Fragment() {
Expand Down Expand Up @@ -211,44 +210,41 @@ class WebViewFragment : Fragment() {
val content = LinearLayout(myContext)
content.orientation = LinearLayout.VERTICAL
val sizeView = TextView(myContext)
val discreteSeekBar = DiscreteSeekBar(myContext)
val slider = Slider(myContext)
val currentTextSize = mySPR.getInt(textSizeType, 60)

sizeView.text = String.format(getString(R.string.text_size_dialog_text), currentTextSize, currentTextSize)
sizeView.textSize = 18f
sizeView.setPadding(24, 24, 24, 50)
sizeView.gravity = Gravity.CENTER_HORIZONTAL
content.addView(sizeView)
discreteSeekBar.max = 100
discreteSeekBar.min = 1
discreteSeekBar.progress = webSettings.textZoom
discreteSeekBar.setPadding(50, 0, 50, 50)
discreteSeekBar.setOnProgressChangeListener(object : DiscreteSeekBar.OnProgressChangeListener {
override fun onProgressChanged(seekBar: DiscreteSeekBar, value: Int, fromUser: Boolean) {
// nothing to clean up (for PMD)
}

override fun onStartTrackingTouch(seekBar: DiscreteSeekBar) {
// nothing to clean up (for PMD)
}

override fun onStopTrackingTouch(seekBar: DiscreteSeekBar) {
sizeView.text = String.format(getString(R.string.text_size_dialog_text), currentTextSize, discreteSeekBar.progress)
slider.apply {
valueFrom = 1F
valueTo = 100F
stepSize = 1F
value = webSettings.textZoom.toFloat()
setPadding(50, 0, 50, 50)
addOnChangeListener { _, value, _ ->
sizeView.text = String.format(
getString(R.string.text_size_dialog_text),
currentTextSize,
value.toInt()
)
}
})
content.addView(discreteSeekBar)
}
content.addView(slider)

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
MaterialAlertDialogBuilder(myContext)
.setView(content)
.setTitle(R.string.text_size_dialog_title)
.setPositiveButton(R.string.text_size_dialog_button_1) { _, _ ->
val textSize = slider.value.toInt()
webSettings.textZoom = textSize
editor.putInt(textSizeType, textSize).apply()
Toasty.info(myContext, getString(R.string.text_size_changed) + textSize, Toasty.LENGTH_SHORT).show()
}
negativeButton(R.string.text_size_dialog_button_2)
}
.setNegativeButton(R.string.text_size_dialog_button_2, null)
.show()
}
}

Expand Down
25 changes: 25 additions & 0 deletions app/src/main/res/layout/dialog_end_user_consent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="20dp"
android:paddingBottom="8dp"
android:orientation="vertical">

<com.google.android.material.button.MaterialButton
android:id="@+id/privacy_policy_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.Material3.Button.OutlinedButton"
android:text="@string/end_user_consent_privacy_policy" />

<com.google.android.material.button.MaterialButton
android:id="@+id/terms_of_use_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
style="@style/Widget.Material3.Button.OutlinedButton"
android:text="@string/end_user_consent_terms_of_use" />

</androidx.appcompat.widget.LinearLayoutCompat>
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<string name="end_user_consent_button_2">Abbrechen</string>
<string name="end_user_consent_message">Durch die Nutzung dieser App stimmen Sie der Datenschutzerklärung und den Nutzungsbedingungen zu.</string>
<string name="end_user_consent_title">Einverständniserklärung</string>
<string name="end_user_consent_privacy_policy">Datenschutzerklärung</string>
<string name="end_user_consent_terms_of_use">Nutzungsbedingungen</string>
<string name="error_fill_out">Fülle den Hostname aus</string>
<string name="intro_fragment1_description">Dies ist die inoffizielle LogViewer for openHAB-App von Cyb3rKo</string>
<string name="intro_fragment1_title">Willkommen…</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
<string name="end_user_consent_button_2">Cancel</string>
<string name="end_user_consent_message">By using this app you agree to the Privacy Policy and the Terms of Use.</string>
<string name="end_user_consent_title">End User Consent</string>
<string name="end_user_consent_privacy_policy">Privacy Policy</string>
<string name="end_user_consent_terms_of_use">Terms of Use</string>
<string name="error_fill_out">Fill out the hostname</string>
<string name="github_repo" translatable="false">github.com/cyb3rko/logviewer-for-openhab-app</string>
<string name="intro_fragment1_description">This is the unofficial LogViewer for openHAB app made by Cyb3rKo</string>
Expand Down

0 comments on commit 01dea6f

Please sign in to comment.