Skip to content

Commit

Permalink
Optimize and simplify connections management
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Jun 23, 2021
1 parent a7d53fb commit c444a16
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 76 deletions.
21 changes: 20 additions & 1 deletion app/src/main/java/com/cyb3rko/logviewerforopenhab/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,23 @@ internal data class Connection(
val httpsActivated: Boolean,
val hostName: String,
val port: Int
)
) {
override fun toString(): String {
val http = if (httpsActivated) "https" else "http"
return "$http://$hostName:$port"
}

fun toCaption(): String {
val emojiCode = if (httpsActivated) 0x1F512 else 0x1F513
val emoji = String(Character.toChars(emojiCode))
return "$hostName:$port $emoji"
}

companion object {
fun fromString(string: String): Connection {
val parts1 = string.split("://")
val parts2 = parts1[1].split(":")
return Connection(parts1[0] == "https", parts2[0], parts2[1].toInt())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ class MainActivity : AppCompatActivity() {
requestReview()
}

private fun restoreConnections() = showConnections(mySPR, getListOfConnections(mySPR), this)
private fun restoreConnections() {
try {
showConnections(mySPR, getListOfConnections(mySPR), this)
} catch (e: Exception) {
editor.putString(CONNECTIONS, "empty").apply()
}
}

private fun showEndUserConsent() {
var dialogMessage = getString(R.string.end_user_consent_2_message_1)
Expand Down
48 changes: 16 additions & 32 deletions app/src/main/java/com/cyb3rko/logviewerforopenhab/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.content.Context
import android.content.SharedPreferences
import android.text.Html
import android.text.method.LinkMovementMethod
import android.view.MenuItem
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import com.afollestad.materialdialogs.MaterialDialog
Expand Down Expand Up @@ -50,13 +49,8 @@ internal fun getListOfConnections(mySPR: SharedPreferences): MutableList<Connect
if (storedConnections == "empty" || storedConnections == "") {
return mutableListOf()
}
val tempList = storedConnections?.split(";")
var parts1: List<String>
var parts2: List<String>
tempList?.forEach {
parts1 = it.split("://")
parts2 = parts1[1].split(":")
resultList.add(Connection(parts1[0] == "https", parts2[0], parts2[1].toInt()))
storedConnections?.split(";")?.forEach {
resultList.add(Connection.fromString(it))
}
return resultList
}
Expand All @@ -70,28 +64,16 @@ internal fun showConnections(mySPR: SharedPreferences, connections: MutableList<
val connectionsMenu = navView.menu.findItem(R.id.nav_connections).subMenu
connectionsMenu.clear()

var item: MenuItem
var link: String
var outerParts: List<String>
var parts: List<String>
var http: String
var emojiCode: Int
var emoji: String
connections.forEach { connection ->
http = if (connection.httpsActivated) "https" else "http"
emojiCode = if (connection.httpsActivated) 0x1F512 else 0x1F513
emoji = String(Character.toChars(emojiCode))
item = connectionsMenu.add("${connection.hostName}:${connection.port} $emoji")
val item = connectionsMenu.add(connection.toCaption())
item.setIcon(R.drawable._ic_connection)
item.setOnMenuItemClickListener { menuItem ->
outerParts = menuItem.title.split(" ")
http = if (outerParts[1] == String(Character.toChars(0x1F512))) "https" else "http"
parts = outerParts[0].split(":")
link = "$http://${parts[0]}:${parts[1]}"
editor.putBoolean(HTTPS_ACTIVATED, connection.httpsActivated)
editor.putString(LINK, link)
editor.putString(HOSTNAME_STRING, parts[0])
editor.putInt(PORT_INT, parts[1].toInt()).apply()
item.setOnMenuItemClickListener {
connection.apply {
editor.putBoolean(HTTPS_ACTIVATED, httpsActivated)
editor.putString(LINK, toString())
editor.putString(HOSTNAME_STRING, hostName)
editor.putInt(PORT_INT, port).apply()
}
navController.navigate(R.id.nav_webview)
drawer.close()
true
Expand All @@ -100,10 +82,12 @@ internal fun showConnections(mySPR: SharedPreferences, connections: MutableList<
}
}

internal fun hideConnections(activity: Activity) {
val navView = activity.findViewById<NavigationView>(R.id.nav_view)
val connectionsMenu = navView.menu.findItem(R.id.nav_connections).subMenu
connectionsMenu.clear()
internal fun hideConnections(activity: Activity?) {
if (activity != null) {
val navView = activity.findViewById<NavigationView>(R.id.nav_view)
val connectionsMenu = navView.menu.findItem(R.id.nav_connections).subMenu
connectionsMenu.clear()
}
}

internal fun showLicenseDialog(context: Context?, type: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentMainBinding.inflate(inflater, container, false)
val root = binding.root
myContext = requireContext()

mySPR = myContext.getSharedPreferences(SHARED_PREFERENCE, Context.MODE_PRIVATE)
editor = mySPR.edit()
Expand Down Expand Up @@ -176,16 +175,9 @@ class MainFragment : Fragment() {
if (connections.size >= 3) {
connections.removeAt(0)
}
var newConnections = ""
var http: String
connections.forEach {
http = if (it.httpsActivated) "https" else "http"
newConnections += "$http://${it.hostName}:${it.port};"
}
http = if (httpsActivated) "https" else "http"
newConnections += "$http://${hostname}:${portInt}"
editor.putString(CONNECTIONS, newConnections).commit()
connections.add(newConnection)
val newConnections = connections.joinToString(";")
editor.putString(CONNECTIONS, newConnections).commit()
showConnections(mySPR, connections, activity)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.ListPreference
import androidx.preference.Preference
Expand Down Expand Up @@ -76,47 +77,45 @@ class PreferenceFragment : PreferenceFragmentCompat() {
if (connectionOverviewSwitch.isChecked) {
showConnections(mySPR, getListOfConnections(mySPR), activity)
} else {
hideConnections(requireActivity())
hideConnections(activity)
}
true
}
CONNECTIONS_MANAGE -> {
val rawConnections = getListOfConnections(mySPR)
val connectionList = mutableListOf<String>()
var http: String
rawConnections.forEach {
http = if (it.httpsActivated) "https" else "http"
connectionList.add("$http://${it.hostName}:${it.port}")
}
if (connectionList.isEmpty()) {
if (activity != null) {
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)
}
return true
}
val rawConnections = mutableListOf<String>()
connectionList.forEach { rawConnections.add(it.toString()) }
MaterialDialog(myContext).show {
title(R.string.settings_manage_connections_dialog_title)
message(R.string.settings_manage_connections_dialog1_message)
positiveButton(android.R.string.ok)
}
return true
}
MaterialDialog(myContext).show {
title(R.string.settings_manage_connections_dialog_title)
listItemsMultiChoice(items = connectionList.toList())
positiveButton(R.string.settings_manage_connections_dialog2_button) {
val selection = mutableListOf<Int>()
repeat(connectionList.size) { i ->
if (it.isItemChecked(i)) selection.add(i)
listItemsMultiChoice(items = rawConnections.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 newConnections = connectionList.joinToString(";")
mySPR.edit().putString(CONNECTIONS, newConnections).apply()
showConnections(mySPR, connectionList, myContext as Activity)
}
selection.reversed().forEach { i ->
rawConnections.removeAt(i)
}
var newConnections = ""
rawConnections.forEachIndexed { _, c ->
http = if (c.httpsActivated) "https" else "http"
newConnections += "$http://${c.hostName}:${c.port};"
}
mySPR.edit().putString(CONNECTIONS, newConnections.dropLast(1)).apply()
showConnections(mySPR, rawConnections, myContext as Activity)
}
true
} else {
Toast.makeText(myContext, R.string.settings_manage_connections_error, Toast.LENGTH_SHORT).show()
false
}
true
}
NIGHTMODE -> {
nightModeList.setOnPreferenceChangeListener { _, newValue ->
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<string name="settings_manage_connections_dialog_title">Delete Connections</string>
<string name="settings_manage_connections_dialog1_message">No connections found</string>
<string name="settings_manage_connections_dialog2_button">Save</string>
<string name="settings_manage_connections_error">Error occurred: Activity is null</string>
<string name="settings_nightmode_title">Night Mode</string>
<string name="settings_nightmode_summary">Toggle the dark appearance of the app (logview excluded)</string>
<string name="settings_analytics_title">Analytics Data Collection</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<string name="settings_manage_connections_dialog_title">Delete Connections</string>
<string name="settings_manage_connections_dialog1_message">No connections found</string>
<string name="settings_manage_connections_dialog2_button">Save</string>
<string name="settings_manage_connections_error">Error occurred: Activity is null</string>
<string name="settings_nightmode_title">Night Mode</string>
<string name="settings_nightmode_summary">Toggle the dark appearance of the app (logview excluded)</string>
<string name="settings_analytics_title">Analytics Data Collection</string>
Expand Down

0 comments on commit c444a16

Please sign in to comment.