fix: adding node from onboarding screen should work now

This commit is contained in:
- 2024-09-30 21:03:23 +02:00
parent 79c7c0a463
commit b99b132db3
10 changed files with 189 additions and 197 deletions

View file

@ -26,9 +26,8 @@ import androidx.lifecycle.lifecycleScope
import com.google.android.material.progressindicator.CircularProgressIndicator
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.mynero.wallet.data.Node
import net.mynero.wallet.fragment.dialog.AddNodeBottomSheetDialog
import net.mynero.wallet.fragment.dialog.NodeSelectionBottomSheetDialog
import net.mynero.wallet.listener.NodeSelectionDialogListenerAdapter
import net.mynero.wallet.livedata.combineLiveDatas
import net.mynero.wallet.model.EnumTorState
import net.mynero.wallet.model.Wallet
@ -97,7 +96,7 @@ class OnboardingActivity : AppCompatActivity() {
walletProxyAddressEditText.isEnabled = usingProxy && !usingBundledTor
walletProxyPortEditText.isEnabled = usingProxy && !usingBundledTor
val node = PrefService.instance?.node // should be using default here
val node = PrefService.instance.node // should be using default here
selectNodeButton.text = getString(R.string.node_button_text, node?.address)
bindListeners()
@ -285,24 +284,14 @@ class OnboardingActivity : AppCompatActivity() {
}
selectNodeButton.setOnClickListener {
val activity = this
supportFragmentManager.let { fragmentManager ->
val dialog = NodeSelectionBottomSheetDialog()
dialog.listener = object : NodeSelectionBottomSheetDialog.NodeSelectionDialogListener {
override fun onNodeSelected() {
val node = PrefService.instance?.node
selectNodeButton.text = getString(R.string.node_button_text, node?.address)
Toast.makeText(
activity,
getString(R.string.node_selected, node?.name ?: node?.host),
Toast.LENGTH_SHORT
).show()
}
override fun onClickedEditNode(node: Node?) {}
override fun onClickedAddNode() {}
}
dialog.show(fragmentManager, "node_selection_dialog")
}
val listener = NodeSelectionDialogListenerAdapter(
activity = this,
setSelectNodeButtonText = { selectNodeButton.text = it },
getProxyAndPortValues = { Pair(walletProxyAddressEditText.text.toString(), walletProxyPortEditText.text.toString()) }
)
val dialog = NodeSelectionBottomSheetDialog(listener)
dialog.show(supportFragmentManager, "node_selection_dialog")
}
useBundledTor.setOnCheckedChangeListener { _, isChecked ->
@ -420,14 +409,13 @@ internal class OnboardingViewModel : ViewModel() {
}
return
}
PrefService.instance?.edit()?.putBoolean(Constants.PREF_USES_PASSWORD, true)
?.apply()
PrefService.instance.edit().putBoolean(Constants.PREF_USES_PASSWORD, true).apply()
}
var restoreHeight = newRestoreHeight
val walletFile = File(mainActivity.applicationInfo.dataDir, Constants.WALLET_NAME)
var wallet: Wallet? = null
if (offset.isNotEmpty()) {
PrefService.instance?.edit()?.putBoolean(Constants.PREF_USES_OFFSET, true)?.apply()
PrefService.instance.edit().putBoolean(Constants.PREF_USES_OFFSET, true).apply()
}
val seedTypeValue = seedType.value ?: return
if (walletSeed.isEmpty()) {
@ -503,7 +491,6 @@ internal class OnboardingViewModel : ViewModel() {
val ok = walletStatus?.isOk
walletFile.delete() // cache is broken for some reason when recovering wallets. delete the file here. this happens in monerujo too.
if (ok == true) {
println("KEK")
MoneroHandlerThread.init(walletFile, passphrase, context)
val intent = Intent(mainActivity, HomeActivity::class.java)
mainActivity.startActivity(intent)
@ -553,14 +540,14 @@ internal class OnboardingViewModel : ViewModel() {
fun setProxyAddress(address: String) {
_proxyAddress.value = address
if (address.isEmpty()) PrefService.instance?.deleteProxy()
if (address.isEmpty()) PrefService.instance.deleteProxy()
val port = _proxyPort.value ?: return
ProxyService.instance?.updateProxy(address, port)
}
fun setProxyPort(port: String) {
_proxyPort.value = port
if (port.isEmpty()) PrefService.instance?.deleteProxy()
if (port.isEmpty()) PrefService.instance.deleteProxy()
val address = _proxyAddress.value ?: return
ProxyService.instance?.updateProxy(address, port)
}

View file

@ -8,7 +8,6 @@ import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
@ -19,22 +18,16 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.progressindicator.CircularProgressIndicator
import net.mynero.wallet.data.Node
import net.mynero.wallet.data.Node.Companion.fromJson
import net.mynero.wallet.fragment.dialog.AddNodeBottomSheetDialog
import net.mynero.wallet.fragment.dialog.EditNodeBottomSheetDialog
import net.mynero.wallet.fragment.dialog.NodeSelectionBottomSheetDialog
import net.mynero.wallet.fragment.dialog.WalletKeysBottomSheetDialog
import net.mynero.wallet.listener.NodeSelectionDialogListenerAdapter
import net.mynero.wallet.model.EnumTorState
import net.mynero.wallet.model.WalletManager
import net.mynero.wallet.service.BalanceService
import net.mynero.wallet.service.HistoryService
import net.mynero.wallet.service.MoneroHandlerThread
import net.mynero.wallet.service.PrefService
import net.mynero.wallet.service.ProxyService
import net.mynero.wallet.util.Constants
import org.json.JSONArray
import java.io.File
import net.mynero.wallet.util.Utils
class SettingsActivity : AppCompatActivity() {
@ -82,14 +75,14 @@ class SettingsActivity : AppCompatActivity() {
proxySettingsLayout.visibility = View.VISIBLE
streetModeSwitch.isChecked =
PrefService.instance?.getBoolean(Constants.PREF_STREET_MODE, false) == true
PrefService.instance.getBoolean(Constants.PREF_STREET_MODE, false) == true
monerochanSwitch.isChecked =
PrefService.instance?.getBoolean(Constants.PREF_MONEROCHAN, Constants.DEFAULT_PREF_MONEROCHAN) == true
PrefService.instance.getBoolean(Constants.PREF_MONEROCHAN, Constants.DEFAULT_PREF_MONEROCHAN) == true
useBundledTor.isChecked = cachedUsingBundledTor
torSwitch.isChecked = cachedUsingProxy
updateProxy(cachedProxyAddress, cachedProxyPort)
val node = PrefService.instance?.node // shouldn't use default value here
val node = PrefService.instance.node // shouldn't use default value here
selectNodeButton.text = getString(R.string.node_button_text, node?.address)
bindListeners()
@ -97,97 +90,33 @@ class SettingsActivity : AppCompatActivity() {
}
private fun bindListeners() {
val activity = this
val onBackPressedCallback: OnBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
refreshProxy()
Utils.refreshProxy(walletProxyAddressEditText.text.toString(), walletProxyPortEditText.text.toString())
finish()
}
}
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
streetModeSwitch.setOnCheckedChangeListener { _: CompoundButton?, b: Boolean ->
PrefService.instance?.edit()?.putBoolean(Constants.PREF_STREET_MODE, b)?.apply()
PrefService.instance.edit().putBoolean(Constants.PREF_STREET_MODE, b).apply()
BalanceService.instance?.refreshBalance()
}
monerochanSwitch.setOnCheckedChangeListener { _: CompoundButton?, b: Boolean ->
PrefService.instance?.edit()?.putBoolean(Constants.PREF_MONEROCHAN, b)?.apply()
PrefService.instance.edit().putBoolean(Constants.PREF_MONEROCHAN, b).apply()
HistoryService.instance?.refreshHistory()
}
selectNodeButton.setOnClickListener {
supportFragmentManager.let { fragmentManager ->
val dialog = NodeSelectionBottomSheetDialog()
dialog.listener = object : NodeSelectionBottomSheetDialog.NodeSelectionDialogListener {
override fun onNodeSelected() {
val node = PrefService.instance?.node
selectNodeButton.text = getString(R.string.node_button_text, node?.address)
refreshProxy()
val listener = NodeSelectionDialogListenerAdapter(
activity = this,
setSelectNodeButtonText = { selectNodeButton.text = it },
getProxyAndPortValues = { Pair(walletProxyAddressEditText.text.toString(), walletProxyPortEditText.text.toString()) }
)
runOnUiThread {
Toast.makeText(
activity,
activity.getString(R.string.node_selected, node?.name ?: node?.host),
Toast.LENGTH_SHORT
).show()
}
}
override fun onClickedEditNode(node: Node?) {
val editNodeDialog = EditNodeBottomSheetDialog()
editNodeDialog.listener = object : EditNodeBottomSheetDialog.EditNodeListener {
override fun onNodeDeleted(node: Node?) {
try {
val nodesArray = PrefService.instance?.getString(Constants.PREF_CUSTOM_NODES, "[]")
val jsonArray = JSONArray(nodesArray)
for (i in 0 until jsonArray.length()) {
val nodeJsonObject = jsonArray.getJSONObject(i)
val savedNode = fromJson(nodeJsonObject)
if (savedNode?.toNodeString() == node?.toNodeString()) jsonArray.remove(i)
}
saveNodesAndReopen(jsonArray)
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onNodeEdited(oldNode: Node?, newNode: Node?) {
try {
val nodesArray = PrefService.instance?.getString(Constants.PREF_CUSTOM_NODES, "[]")
val jsonArray = JSONArray(nodesArray)
for (i in 0 until jsonArray.length()) {
val nodeJsonObject = jsonArray.getJSONObject(i)
val savedNode = fromJson(nodeJsonObject)
if (savedNode?.toNodeString() == oldNode?.toNodeString()) jsonArray.put(
i,
newNode?.toJson()
)
}
saveNodesAndReopen(jsonArray)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
editNodeDialog.node = node
editNodeDialog.show(fragmentManager, "edit_node_dialog")
}
override fun onClickedAddNode() {
activity.supportFragmentManager.let { fragmentManager ->
val addNodeDialog = AddNodeBottomSheetDialog()
addNodeDialog.listener = object : AddNodeBottomSheetDialog.AddNodeListener {
override fun onNodeAdded() {}
}
addNodeDialog.show(fragmentManager, "add_node_dialog")
}
}
}
dialog.show(fragmentManager, "node_selection_dialog")
}
val dialog = NodeSelectionBottomSheetDialog(listener)
dialog.show(supportFragmentManager, "node_selection_dialog")
}
useBundledTor.setOnCheckedChangeListener { _, isChecked ->
@ -195,8 +124,7 @@ class SettingsActivity : AppCompatActivity() {
}
displaySeedButton.setOnClickListener {
val usesPassword =
PrefService.instance?.getBoolean(Constants.PREF_USES_PASSWORD, false) == true
val usesPassword = PrefService.instance.getBoolean(Constants.PREF_USES_PASSWORD, false)
if (usesPassword) {
val intent = Intent(this, PasswordActivity::class.java)
askForWalletPasswordAndDisplayWalletKeys.launch(intent)
@ -220,7 +148,7 @@ class SettingsActivity : AppCompatActivity() {
walletProxyPortEditText.isEnabled = useProxy && mViewModel.useBundledTor.value == false
walletProxyAddressEditText.isEnabled = useProxy && mViewModel.useBundledTor.value == false
refreshProxy()
Utils.refreshProxy(walletProxyAddressEditText.text.toString(), walletProxyPortEditText.text.toString())
}
mViewModel.useBundledTor.observe(this) { isChecked ->
@ -268,18 +196,7 @@ class SettingsActivity : AppCompatActivity() {
private fun updateProxy(address: String, port: String) {
walletProxyPortEditText.setText(port)
walletProxyAddressEditText.setText(address)
refreshProxy()
}
private fun refreshProxy() {
val proxyAddress = walletProxyAddressEditText.text.toString()
val proxyPort = walletProxyPortEditText.text.toString()
val savedProxyAddress = ProxyService.instance?.proxyAddress
val savedProxyPort = ProxyService.instance?.proxyPort
val currentWalletProxy = WalletManager.instance?.proxy
val newProxy = "$proxyAddress:$proxyPort"
if (proxyAddress != savedProxyAddress || proxyPort != savedProxyPort || (newProxy != currentWalletProxy && newProxy != ":"))
ProxyService.instance?.updateProxy(proxyAddress, proxyPort)
Utils.refreshProxy(address, port)
}
private fun displaySeedDialog(password: String) {
@ -287,11 +204,6 @@ class SettingsActivity : AppCompatActivity() {
informationDialog.password = password
informationDialog.show(supportFragmentManager, "information_seed_dialog")
}
private fun saveNodesAndReopen(jsonArray: JSONArray) {
PrefService.instance?.edit()?.putString(Constants.PREF_CUSTOM_NODES, jsonArray.toString())
?.apply()
}
}
class SettingsViewModel : ViewModel() {

View file

@ -68,8 +68,8 @@ class NodeSelectionAdapter(val listener: NodeSelectionAdapterListener?) :
}
interface NodeSelectionAdapterListener {
fun onSelectNode(node: Node?)
fun onSelectEditNode(node: Node?): Boolean
fun onSelectNode(node: Node)
fun onSelectEditNode(node: Node): Boolean
}
/**
@ -81,7 +81,7 @@ class NodeSelectionAdapter(val listener: NodeSelectionAdapterListener?) :
view
) {
fun bind(node: Node) {
val currentNode = PrefService.instance?.node
val currentNode = PrefService.instance.node
val match = node == currentNode
if (match) {
itemView.setBackgroundColor(

View file

@ -19,9 +19,7 @@ import net.mynero.wallet.util.Helper.getClipBoardText
import org.json.JSONException
import org.json.JSONObject
class EditNodeBottomSheetDialog : BottomSheetDialogFragment() {
var listener: EditNodeListener? = null
var node: Node? = null
class EditNodeBottomSheetDialog(val listener: EditNodeListener, val node: Node) : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
@ -43,14 +41,13 @@ class EditNodeBottomSheetDialog : BottomSheetDialogFragment() {
val trustedDaemonCheckBox = view.findViewById<CheckBox>(R.id.trusted_node_checkbox)
val pastePasswordImageButton =
view.findViewById<ImageButton>(R.id.paste_password_imagebutton)
if (node == null) return
addressEditText.setText(node?.host)
portEditText.setText("${node?.rpcPort}")
nodeNameEditText.setText(node?.name)
usernameEditText.setText(node?.username)
trustedDaemonCheckBox.isChecked = node?.trusted ?: false
if (node?.password?.isNotEmpty() == true) {
passwordEditText.setText(node?.password)
addressEditText.setText(node.host)
portEditText.setText("${node.rpcPort}")
nodeNameEditText.setText(node.name)
usernameEditText.setText(node.username)
trustedDaemonCheckBox.isChecked = node.trusted ?: false
if (node.password.isNotEmpty()) {
passwordEditText.setText(node.password)
passwordEditText.visibility = View.VISIBLE
pastePasswordImageButton.visibility = View.VISIBLE
}
@ -72,7 +69,7 @@ class EditNodeBottomSheetDialog : BottomSheetDialogFragment() {
addPasteListener(view, usernameEditText, R.id.paste_username_imagebutton)
addPasteListener(view, passwordEditText, R.id.paste_password_imagebutton)
deleteNodeButton.setOnClickListener {
listener?.onNodeDeleted(node)
listener.onNodeDeleted(node)
dismiss()
}
doneEditingButton.setOnClickListener {
@ -106,7 +103,7 @@ class EditNodeBottomSheetDialog : BottomSheetDialogFragment() {
jsonObject.put("network", "mainnet")
jsonObject.put("name", nodeName)
jsonObject.put("trusted", trusted)
listener?.onNodeEdited(node, fromJson(jsonObject))
listener.onNodeEdited(node, fromJson(jsonObject))
dismiss()
} catch (e: JSONException) {
throw RuntimeException(e)
@ -125,7 +122,7 @@ class EditNodeBottomSheetDialog : BottomSheetDialogFragment() {
}
interface EditNodeListener {
fun onNodeDeleted(node: Node?)
fun onNodeDeleted(node: Node)
fun onNodeEdited(oldNode: Node?, newNode: Node?)
}
}

View file

@ -22,8 +22,7 @@ import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
class NodeSelectionBottomSheetDialog : BottomSheetDialogFragment(), NodeSelectionAdapterListener {
var listener: NodeSelectionDialogListener? = null
class NodeSelectionBottomSheetDialog(val listener: NodeSelectionDialogListener) : BottomSheetDialogFragment(), NodeSelectionAdapterListener {
private var adapter: NodeSelectionAdapter? = null
override fun onCreateView(
inflater: LayoutInflater,
@ -42,12 +41,10 @@ class NodeSelectionBottomSheetDialog : BottomSheetDialogFragment(), NodeSelectio
recyclerView.adapter = adapter
val addNodeButton = view.findViewById<Button>(R.id.add_node_button)
addNodeButton.setOnClickListener {
if (listener != null) {
listener?.onClickedAddNode()
}
listener.onClickedAddNode()
dismiss()
}
val nodesArray = PrefService.instance?.getString(Constants.PREF_CUSTOM_NODES, "[]")
val nodesArray = PrefService.instance.getString(Constants.PREF_CUSTOM_NODES, "[]")
val jsonArray: JSONArray = try {
JSONArray(nodesArray)
} catch (e: JSONException) {
@ -79,35 +76,31 @@ class NodeSelectionBottomSheetDialog : BottomSheetDialogFragment(), NodeSelectio
}
}
}
PrefService.instance?.edit()?.putString(Constants.PREF_CUSTOM_NODES, jsonArray.toString())
?.apply()
PrefService.instance.edit().putString(Constants.PREF_CUSTOM_NODES, jsonArray.toString()).apply()
for (defaultNode in DefaultNodes.values()) {
fromJson(defaultNode.json)?.let { nodes.add(it) }
}
adapter?.submitList(nodes)
}
override fun onSelectNode(node: Node?) {
PrefService.instance?.edit()?.putString(Constants.PREF_NODE_2, node?.toJson().toString())
?.apply()
node?.let { DaemonService.instance?.setDaemon(it) }
override fun onSelectNode(node: Node) {
PrefService.instance.edit().putString(Constants.PREF_NODE_2, node.toJson().toString()).apply()
DaemonService.instance?.setDaemon(node)
activity?.runOnUiThread {
adapter?.updateSelectedNode()
}
listener?.onNodeSelected()
listener.onNodeSelected()
}
override fun onSelectEditNode(node: Node?): Boolean {
if (listener != null) {
listener?.onClickedEditNode(node)
}
override fun onSelectEditNode(node: Node): Boolean {
listener.onClickedEditNode(node)
dismiss()
return true
}
interface NodeSelectionDialogListener {
fun onNodeSelected()
fun onClickedEditNode(node: Node?)
fun onClickedEditNode(node: Node)
fun onClickedAddNode()
}
}

View file

@ -0,0 +1,90 @@
package net.mynero.wallet.listener
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import net.mynero.wallet.R
import net.mynero.wallet.data.Node
import net.mynero.wallet.data.Node.Companion.fromJson
import net.mynero.wallet.fragment.dialog.AddNodeBottomSheetDialog
import net.mynero.wallet.fragment.dialog.EditNodeBottomSheetDialog
import net.mynero.wallet.fragment.dialog.NodeSelectionBottomSheetDialog
import net.mynero.wallet.service.PrefService
import net.mynero.wallet.util.Constants
import net.mynero.wallet.util.Utils
import org.json.JSONArray
class NodeSelectionDialogListenerAdapter(
val activity: FragmentActivity,
val setSelectNodeButtonText: (String) -> Unit,
val getProxyAndPortValues: () -> Pair<String, String>,
) : NodeSelectionBottomSheetDialog.NodeSelectionDialogListener {
override fun onNodeSelected() {
val node = PrefService.instance.node
setSelectNodeButtonText(activity.getString(R.string.node_button_text, node?.address))
val (proxyAddress, proxyPort) = getProxyAndPortValues()
Utils.refreshProxy(proxyAddress, proxyPort)
activity.runOnUiThread {
Toast.makeText(
activity,
activity.getString(R.string.node_selected, node?.name ?: node?.host),
Toast.LENGTH_SHORT
).show()
}
}
override fun onClickedEditNode(node: Node) {
val listener = object : EditNodeBottomSheetDialog.EditNodeListener {
override fun onNodeDeleted(node: Node) {
try {
val nodesArray = PrefService.instance.getString(Constants.PREF_CUSTOM_NODES, "[]")
val jsonArray = JSONArray(nodesArray)
for (i in 0 until jsonArray.length()) {
val nodeJsonObject = jsonArray.getJSONObject(i)
val savedNode = fromJson(nodeJsonObject)
if (savedNode?.toNodeString() == node.toNodeString()) jsonArray.remove(i)
}
saveNodes(jsonArray)
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onNodeEdited(oldNode: Node?, newNode: Node?) {
try {
val nodesArray = PrefService.instance.getString(Constants.PREF_CUSTOM_NODES, "[]")
val jsonArray = JSONArray(nodesArray)
for (i in 0 until jsonArray.length()) {
val nodeJsonObject = jsonArray.getJSONObject(i)
val savedNode = fromJson(nodeJsonObject)
if (savedNode?.toNodeString() == oldNode?.toNodeString()) jsonArray.put(
i,
newNode?.toJson()
)
}
saveNodes(jsonArray)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
val editNodeDialog = EditNodeBottomSheetDialog(listener, node)
editNodeDialog.show(activity.supportFragmentManager, "edit_node_dialog")
}
override fun onClickedAddNode() {
val addNodeDialog = AddNodeBottomSheetDialog()
addNodeDialog.listener = object : AddNodeBottomSheetDialog.AddNodeListener {
override fun onNodeAdded() {}
}
addNodeDialog.show(activity.supportFragmentManager, "add_node_dialog")
}
private fun saveNodes(jsonArray: JSONArray) {
PrefService.instance.edit().putString(Constants.PREF_CUSTOM_NODES, jsonArray.toString()).apply()
}
}

View file

@ -60,9 +60,7 @@ class MoneroHandlerThread(val wallet: Wallet, val context: Context) : Thread(nul
@Synchronized
override fun start() {
println("HERE1")
super.start()
println("HERE2")
onRefresh(false)
}
@ -202,7 +200,6 @@ class MoneroHandlerThread(val wallet: Wallet, val context: Context) : Thread(nul
}
fun onRefresh(walletSynced: Boolean) {
println("onRefresh")
if (walletSynced) {
UTXOService.instance?.refreshUtxos()
}

View file

@ -13,16 +13,18 @@ import org.json.JSONException
import org.json.JSONObject
class PrefService(application: MoneroApplication) : ServiceBase(null) {
private val preferences = application.getSharedPreferences(
application.applicationInfo.packageName,
Context.MODE_PRIVATE
)
init {
preferences = application.getSharedPreferences(
application.applicationInfo.packageName,
Context.MODE_PRIVATE
)
instance = this
}
fun edit(): SharedPreferences.Editor? {
return preferences?.edit()
fun edit(): SharedPreferences.Editor {
return preferences.edit()
}
val node: Node?
@ -52,7 +54,7 @@ class PrefService(application: MoneroApplication) : ServiceBase(null) {
if (nodeString.isNotEmpty()) {
val node = fromString(nodeString)
if (node != null) {
edit()?.putString(Constants.PREF_NODE_2, node.toJson().toString())?.apply()
edit().putString(Constants.PREF_NODE_2, node.toJson().toString())?.apply()
return node
}
}
@ -60,22 +62,22 @@ class PrefService(application: MoneroApplication) : ServiceBase(null) {
}
fun getString(key: String?, defaultValue: String): String? {
val value = preferences?.getString(key, "")
val value = preferences.getString(key, "")
if (value?.isEmpty() == true && defaultValue.isNotEmpty()) {
edit()?.putString(key, defaultValue)?.apply()
edit().putString(key, defaultValue)?.apply()
return defaultValue
}
return value
}
fun getBoolean(key: String?, defaultValue: Boolean): Boolean {
val containsKey = preferences?.contains(key)
val value = preferences?.getBoolean(key, false)
if (value == false && defaultValue && containsKey == false) {
edit()?.putBoolean(key, true)?.apply()
val containsKey = preferences.contains(key)
val value = preferences.getBoolean(key, false)
if (!value && defaultValue && !containsKey) {
edit().putBoolean(key, true)?.apply()
return true
}
return value == true
return value
}
fun saveProxy(address: String, port: String): String? {
@ -86,19 +88,17 @@ class PrefService(application: MoneroApplication) : ServiceBase(null) {
val proxyAddress = "$address:$port"
if (proxyAddress == ":") return null
Log.d("PrefService", "Setting proxy. proxyAddress=$proxyAddress")
edit()?.putString(Constants.PREF_PROXY, proxyAddress)?.apply()
edit().putString(Constants.PREF_PROXY, proxyAddress)?.apply()
return proxyAddress
}
fun deleteProxy() {
Log.d("PrefService", "Deleting proxy...")
edit()?.putString(Constants.PREF_PROXY, "")?.apply()
edit().putString(Constants.PREF_PROXY, "")?.apply()
}
companion object {
private var preferences: SharedPreferences? = null
var instance: PrefService? = null
lateinit var instance: PrefService
private set
}
}

View file

@ -11,15 +11,15 @@ class ProxyService(application: Application) : ServiceBase(null) {
val proxyChangeEvents: SingleLiveEvent<String> = SingleLiveEvent()
var samouraiTorManager: SamouraiTorManager? = null
var usingProxy: Boolean = false
get() = PrefService.instance?.getBoolean(Constants.PREF_USES_PROXY, false) == true
get() = PrefService.instance.getBoolean(Constants.PREF_USES_PROXY, false)
set(enabled) {
PrefService.instance?.edit()?.putBoolean(Constants.PREF_USES_PROXY, enabled)?.apply()
PrefService.instance.edit().putBoolean(Constants.PREF_USES_PROXY, enabled).apply()
field = enabled
}
var useBundledTor: Boolean = false
get() = PrefService.instance?.getBoolean(Constants.PREF_USE_BUNDLED_TOR, false) == true
get() = PrefService.instance.getBoolean(Constants.PREF_USE_BUNDLED_TOR, false)
set(enabled) {
PrefService.instance?.edit()?.putBoolean(Constants.PREF_USE_BUNDLED_TOR, enabled)?.apply()
PrefService.instance.edit().putBoolean(Constants.PREF_USE_BUNDLED_TOR, enabled).apply()
field = enabled
}
@ -35,7 +35,7 @@ class ProxyService(application: Application) : ServiceBase(null) {
fun updateProxy(proxyAddress: String, proxyPort: String) {
var finalProxyAddress = proxyAddress
var finalProxyPort = proxyPort
val curretNode = PrefService.instance?.node
val curretNode = PrefService.instance.node
val isNodeLocalIp =
curretNode?.address?.startsWith("10.") == true || curretNode?.address?.startsWith("192.168.") == true || curretNode?.address == "localhost" || curretNode?.address == "127.0.0.1"
curretNode?.trusted?.let { WalletManager.instance?.wallet?.setTrustedDaemon(it) }
@ -50,7 +50,7 @@ class ProxyService(application: Application) : ServiceBase(null) {
if (proxyPort.isEmpty()) finalProxyPort = "9050"
val validIpAddress = Patterns.IP_ADDRESS.matcher(finalProxyAddress).matches()
if (validIpAddress) {
val proxy = PrefService.instance?.saveProxy(finalProxyAddress, finalProxyPort)
val proxy = PrefService.instance.saveProxy(finalProxyAddress, finalProxyPort)
proxy?.let { proxyChangeEvents.postValue(it) }
}
}
@ -61,7 +61,7 @@ class ProxyService(application: Application) : ServiceBase(null) {
}
val proxy: String?
get() = PrefService.instance?.getString(Constants.PREF_PROXY, "")
get() = PrefService.instance.getString(Constants.PREF_PROXY, "")
val proxyAddress: String
get() {

View file

@ -0,0 +1,16 @@
package net.mynero.wallet.util
import net.mynero.wallet.model.WalletManager
import net.mynero.wallet.service.ProxyService
// Collection of various utilities, probably to be refactored later
object Utils {
fun refreshProxy(proxyAddress: String, proxyPort: String) {
val savedProxyAddress = ProxyService.instance?.proxyAddress
val savedProxyPort = ProxyService.instance?.proxyPort
val currentWalletProxy = WalletManager.instance?.proxy
val newProxy = "$proxyAddress:$proxyPort"
if (proxyAddress != savedProxyAddress || proxyPort != savedProxyPort || (newProxy != currentWalletProxy && newProxy != ":"))
ProxyService.instance?.updateProxy(proxyAddress, proxyPort)
}
}