Convert services to Kotlin; just need to convert fragment/UI stuff next, then eventually convert to Compose

This commit is contained in:
pokkst 2023-12-06 12:46:39 -06:00
parent e4cae3bbb7
commit 08b989eaab
No known key found for this signature in database
GPG key ID: EC4FAAA66859FAA4
14 changed files with 73 additions and 59 deletions

View file

@ -9,7 +9,7 @@ import net.mynero.wallet.fragment.dialog.PasswordBottomSheetDialog
import net.mynero.wallet.fragment.dialog.PasswordBottomSheetDialog.PasswordListener
import net.mynero.wallet.fragment.dialog.SendBottomSheetDialog
import net.mynero.wallet.livedata.SingleLiveEvent
import net.mynero.wallet.model.WalletManager.Companion.instance
import net.mynero.wallet.model.WalletManager
import net.mynero.wallet.service.AddressService
import net.mynero.wallet.service.BalanceService
import net.mynero.wallet.service.BlockchainService
@ -41,8 +41,8 @@ class MainActivity : AppCompatActivity(), MoneroHandlerThread.Listener, Password
val walletKeysFile = File(applicationInfo.dataDir, Constants.WALLET_NAME + ".keys")
if (walletKeysFile.exists()) {
val promptPassword =
PrefService.getInstance().getBoolean(Constants.PREF_USES_PASSWORD, false)
if (!promptPassword) {
PrefService.instance?.getBoolean(Constants.PREF_USES_PASSWORD, false)
if (promptPassword == false) {
init(walletFile, "")
} else {
val passwordDialog = PasswordBottomSheetDialog()
@ -70,15 +70,17 @@ class MainActivity : AppCompatActivity(), MoneroHandlerThread.Listener, Password
}
fun init(walletFile: File, password: String?) {
val wallet = password?.let { instance?.openWallet(walletFile.absolutePath, it) }
thread = MoneroHandlerThread("WalletService", this, wallet)
val wallet = WalletManager.instance?.openWallet(walletFile.absolutePath, password ?: "")
thread = wallet?.let { MoneroHandlerThread("WalletService", this, it) }
thread?.let { thread ->
TxService(thread)
balanceService = BalanceService(thread)
addressService = AddressService(thread)
historyService = HistoryService(thread)
blockchainService = BlockchainService(thread)
utxoService = UTXOService(thread)
thread?.start()
thread.start()
}
}
override fun onRefresh(walletSynced: Boolean) {

View file

@ -127,9 +127,9 @@ class CoinsInfoAdapter(val listener: CoinsInfoAdapterListener?) :
val globalIdxTextView = itemView.findViewById<TextView>(R.id.utxo_global_index_textview)
val outpointTextView = itemView.findViewById<TextView>(R.id.utxo_outpoint_textview)
val streetModeEnabled =
PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false)
PrefService.instance?.getBoolean(Constants.PREF_STREET_MODE, false)
val balanceString =
if (streetModeEnabled) Constants.STREET_MODE_BALANCE else Wallet.getDisplayAmount(
if (streetModeEnabled == true) Constants.STREET_MODE_BALANCE else Wallet.getDisplayAmount(
coinsInfo.amount
)
amountTextView.text =
@ -148,7 +148,7 @@ class CoinsInfoAdapter(val listener: CoinsInfoAdapterListener?) :
if (selected) {
itemView.backgroundTintList =
ContextCompat.getColorStateList(itemView.context, R.color.oled_colorSecondary)
} else if (coinsInfo.isFrozen || UTXOService.instance.isCoinFrozen(coinsInfo)) {
} else if (coinsInfo.isFrozen || UTXOService.instance?.isCoinFrozen(coinsInfo) == true) {
itemView.backgroundTintList =
ContextCompat.getColorStateList(itemView.context, R.color.oled_frozen_utxo)
} else if (!coinsInfo.isUnlocked) {

View file

@ -80,7 +80,7 @@ class NodeSelectionAdapter(val listener: NodeSelectionAdapterListener?) :
view
) {
fun bind(node: Node) {
val currentNode = PrefService.getInstance().node
val currentNode = PrefService.instance?.node
val match = node == currentNode
if (match) {
itemView.setBackgroundColor(itemView.resources.getColor(R.color.oled_colorSecondary))

View file

@ -92,8 +92,8 @@ class SubaddressAdapter(val listener: SubaddressAdapterListener?) :
val amount = subaddress.amount
if (amount > 0) {
val streetMode =
PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false)
if (streetMode) {
PrefService.instance?.getBoolean(Constants.PREF_STREET_MODE, false)
if (streetMode == true) {
addressAmountTextView.text = itemView.context.getString(
R.string.tx_list_amount_positive,
Constants.STREET_MODE_BALANCE

View file

@ -95,9 +95,9 @@ class TransactionInfoAdapter(val listener: TxInfoAdapterListener?) :
fun bind(txInfo: TransactionInfo) {
val streetModeEnabled =
PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false)
PrefService.instance?.getBoolean(Constants.PREF_STREET_MODE, false)
val displayAmount =
if (streetModeEnabled) Constants.STREET_MODE_BALANCE else getDisplayAmount(
if (streetModeEnabled == true) Constants.STREET_MODE_BALANCE else getDisplayAmount(
txInfo.amount,
Helper.DISPLAY_DIGITS_INFO
)

View file

@ -36,7 +36,7 @@ public class EditAddressLabelBottomSheetDialog extends BottomSheetDialogFragment
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Wallet wallet = WalletManager.getInstance().getWallet();
AddressService addressService = AddressService.getInstance();
AddressService addressService = AddressService.instance;
ImageButton pasteButton = view.findViewById(R.id.paste_password_imagebutton);
EditText labelEditText = view.findViewById(R.id.wallet_password_edittext);
Button saveLabelButton = view.findViewById(R.id.unlock_wallet_button);

View file

@ -46,7 +46,8 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
private final MutableLiveData<PendingTransaction> _pendingTransaction = new MutableLiveData<>(null);
public ArrayList<String> selectedUtxos = new ArrayList<>();
public LiveData<Boolean> sendingMax = _sendingMax;
public LiveData<PendingTransaction> pendingTransaction = _pendingTransaction; private final ActivityResultLauncher<String> cameraPermissionsLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(),
public LiveData<PendingTransaction> pendingTransaction = _pendingTransaction;
public UriData uriData = null; private final ActivityResultLauncher<String> cameraPermissionsLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(),
granted -> {
if (granted) {
onScan();
@ -54,7 +55,6 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
Toast.makeText(getActivity(), getString(R.string.no_camera_permission), Toast.LENGTH_SHORT).show();
}
});
public UriData uriData = null;
public boolean isChurning = false;
public Listener listener = null;
public PendingTransaction.Priority priority;
@ -138,7 +138,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
}
private void bindObservers() {
BalanceService.getInstance().balanceInfo.observe(getViewLifecycleOwner(), balanceInfo -> {
BalanceService.instance.balanceInfo.observe(getViewLifecycleOwner(), balanceInfo -> {
createButton.setEnabled(balanceInfo.getRawUnlocked() != 0);
if (!isChurning) {
sendMaxButton.setEnabled(balanceInfo.getRawUnlocked() != 0);
@ -212,7 +212,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
boolean validAddress = Wallet.isAddressValid(address);
if (validAddress && (!amount.isEmpty() || sendAll)) {
long amountRaw = Wallet.getAmountFromString(amount);
long balance = BalanceService.getInstance().getUnlockedBalanceRaw();
long balance = BalanceService.instance.getUnlockedBalanceRaw();
if ((amountRaw >= balance || amountRaw <= 0) && !sendAll) {
Toast.makeText(activity, getString(R.string.send_amount_invalid), Toast.LENGTH_SHORT).show();
return;
@ -253,7 +253,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
Activity activity = getActivity();
if (activity != null) {
((MoneroApplication) activity.getApplication()).getExecutor().execute(() -> {
boolean success = TxService.getInstance().sendTx(pendingTx);
boolean success = TxService.instance.sendTx(pendingTx);
activity.runOnUiThread(() -> {
if (success) {
Toast.makeText(activity, getString(R.string.sent_tx), Toast.LENGTH_SHORT).show();
@ -275,7 +275,7 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
if (activity != null) {
((MoneroApplication) activity.getApplication()).getExecutor().execute(() -> {
try {
PendingTransaction pendingTx = TxService.getInstance().createTx(address, amount, sendAll, feePriority, selectedUtxos);
PendingTransaction pendingTx = TxService.instance.createTx(address, amount, sendAll, feePriority, selectedUtxos);
if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) {
_pendingTransaction.postValue(pendingTx);
} else {
@ -351,4 +351,6 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment {
}
}

View file

@ -83,9 +83,9 @@ public class HomeFragment extends Fragment implements TransactionInfoAdapter.TxI
TextView unlockedBalanceTextView = view.findViewById(R.id.balance_unlocked_textview);
TextView lockedBalanceTextView = view.findViewById(R.id.balance_locked_textview);
BalanceService balanceService = BalanceService.getInstance();
BalanceService balanceService = BalanceService.instance;
HistoryService historyService = HistoryService.getInstance();
BlockchainService blockchainService = BlockchainService.getInstance();
BlockchainService blockchainService = BlockchainService.instance;
if (balanceService != null) {
balanceService.balanceInfo.observe(getViewLifecycleOwner(), balanceInfo -> {

View file

@ -20,14 +20,16 @@ public class ReceiveViewModel extends ViewModel {
public LiveData<List<Subaddress>> addresses = _addresses;
public void init() {
_address.setValue(AddressService.getInstance().currentSubaddress());
if (AddressService.instance != null) {
_address.setValue(AddressService.instance.currentSubaddress());
}
_addresses.setValue(getSubaddresses());
}
private List<Subaddress> getSubaddresses() {
Wallet wallet = WalletManager.getInstance().getWallet();
ArrayList<Subaddress> subaddresses = new ArrayList<>();
int addressesSize = AddressService.getInstance().getLatestAddressIndex();
int addressesSize = AddressService.instance != null ? AddressService.instance.getLatestAddressIndex() : 0;
for (int i = addressesSize - 1; i >= 0; i--) {
subaddresses.add(wallet.getSubaddressObject(i));
}
@ -35,7 +37,9 @@ public class ReceiveViewModel extends ViewModel {
}
public void getFreshSubaddress() {
_address.setValue(AddressService.getInstance().freshSubaddress());
if (AddressService.instance != null) {
_address.setValue(AddressService.instance.freshSubaddress());
}
_addresses.setValue(getSubaddresses());
}

View file

@ -72,15 +72,7 @@ public class SendFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_send, container, false);
} private final ActivityResultLauncher<String> cameraPermissionsLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(),
granted -> {
if (granted) {
onScan(currentEntryIndex);
} else {
Toast.makeText(getActivity(), getString(R.string.no_camera_permission), Toast.LENGTH_SHORT).show();
currentEntryIndex = -1;
}
});
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
@ -105,7 +97,15 @@ public class SendFragment extends Fragment {
bindListeners();
bindObservers();
init();
} private final ActivityResultLauncher<String> cameraPermissionsLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(),
granted -> {
if (granted) {
onScan(currentEntryIndex);
} else {
Toast.makeText(getActivity(), getString(R.string.no_camera_permission), Toast.LENGTH_SHORT).show();
currentEntryIndex = -1;
}
});
private void init() {
addOutput(true);
@ -170,7 +170,7 @@ public class SendFragment extends Fragment {
}
long amountRaw = Wallet.getAmountFromString(amount);
long balance = BalanceService.getInstance().getUnlockedBalanceRaw();
long balance = BalanceService.instance.getUnlockedBalanceRaw();
if (amountRaw >= balance || amountRaw <= 0) {
Toast.makeText(getActivity(), getString(R.string.send_amount_invalid), Toast.LENGTH_SHORT).show();
return false;
@ -429,7 +429,7 @@ public class SendFragment extends Fragment {
private void createTx(List<Pair<String, String>> dests, boolean sendAll, PendingTransaction.Priority feePriority) {
((MoneroApplication) getActivity().getApplication()).getExecutor().execute(() -> {
try {
PendingTransaction pendingTx = TxService.getInstance().createTx(dests, sendAll, feePriority, new ArrayList<>());
PendingTransaction pendingTx = TxService.instance.createTx(dests, sendAll, feePriority, new ArrayList<>());
if (pendingTx != null && pendingTx.getStatus() == PendingTransaction.Status.Status_Ok) {
mViewModel.setPendingTransaction(pendingTx);
} else {
@ -459,7 +459,7 @@ public class SendFragment extends Fragment {
private void sendTx(PendingTransaction pendingTx) {
((MoneroApplication) getActivity().getApplication()).getExecutor().execute(() -> {
boolean success = TxService.getInstance().sendTx(pendingTx);
boolean success = TxService.instance.sendTx(pendingTx);
Activity activity = getActivity();
if (activity != null) {
activity.runOnUiThread(() -> {
@ -480,4 +480,6 @@ public class SendFragment extends Fragment {
}
}

View file

@ -107,7 +107,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
streetModeSwitch.setChecked(PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false));
streetModeSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
PrefService.getInstance().edit().putBoolean(Constants.PREF_STREET_MODE, b).apply();
BalanceService.getInstance().refreshBalance();
BalanceService.instance.refreshBalance();
});
monerochanSwitch.setChecked(PrefService.getInstance().getBoolean(Constants.PREF_MONEROCHAN, true));
@ -172,7 +172,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
});
TextView statusTextView = view.findViewById(R.id.status_textview);
BlockchainService.getInstance().connectionStatus.observe(getViewLifecycleOwner(), connectionStatus -> {
BlockchainService.instance.connectionStatus.observe(getViewLifecycleOwner(), connectionStatus -> {
if (connectionStatus == Wallet.ConnectionStatus.ConnectionStatus_Connected) {
statusTextView.setText(getResources().getText(R.string.connected));
} else if (connectionStatus == Wallet.ConnectionStatus.ConnectionStatus_Disconnected) {

View file

@ -85,7 +85,7 @@ public class UtxosFragment extends Fragment implements CoinsInfoAdapter.CoinsInf
SendBottomSheetDialog sendDialog = new SendBottomSheetDialog();
sendDialog.listener = this;
sendDialog.isChurning = true;
sendDialog.uriData = UriData.parse(AddressService.getInstance().currentSubaddress().address);
sendDialog.uriData = UriData.parse(AddressService.instance.currentSubaddress().address);
sendDialog.selectedUtxos = selectedKeyImages;
sendDialog.show(getActivity().getSupportFragmentManager(), null);
});

View file

@ -12,8 +12,8 @@ class BalanceInfo(val rawUnlocked: Long, val rawLocked: Long) {
val unlockedDisplay: String
get() {
val streetModeEnabled =
PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false)
return if (streetModeEnabled) {
PrefService.instance?.getBoolean(Constants.PREF_STREET_MODE, false)
return if (streetModeEnabled == true) {
Constants.STREET_MODE_BALANCE
} else {
Wallet.getDisplayAmount(rawUnlocked)
@ -22,8 +22,8 @@ class BalanceInfo(val rawUnlocked: Long, val rawLocked: Long) {
val lockedDisplay: String
get() {
val streetModeEnabled =
PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false)
return if (streetModeEnabled) {
PrefService.instance?.getBoolean(Constants.PREF_STREET_MODE, false)
return if (streetModeEnabled == true) {
Constants.STREET_MODE_BALANCE
} else {
Wallet.getDisplayAmount(rawLocked)

View file

@ -18,7 +18,6 @@ package net.mynero.wallet.model
import android.util.Pair
import net.mynero.wallet.data.Subaddress
import net.mynero.wallet.model.NetworkType.Companion.fromInteger
import net.mynero.wallet.model.WalletManager.Companion.instance
import timber.log.Timber
import java.io.File
import java.text.SimpleDateFormat
@ -150,17 +149,17 @@ class Wallet {
external fun store(path: String?): Boolean
fun close(): Boolean {
disposePendingTransaction()
return instance?.close(this) == true
return WalletManager.instance?.close(this) == true
}
external fun getFilename(): String
// virtual std::string keysFilename() const = 0;
fun init(upperTransactionSizeLimit: Long): Boolean {
var daemonAddress = instance?.getDaemonAddress()
var daemonUsername = instance?.daemonUsername
var daemonPassword = instance?.daemonPassword
var proxyAddress = instance?.proxy
var daemonAddress = WalletManager.instance?.getDaemonAddress()
var daemonUsername = WalletManager.instance?.daemonUsername
var daemonPassword = WalletManager.instance?.daemonPassword
var proxyAddress = WalletManager.instance?.proxy
Timber.d("init(")
if (daemonAddress != null) {
Timber.d(daemonAddress.toString())
@ -474,7 +473,12 @@ class Wallet {
@JvmStatic
fun isAddressValid(address: String): Boolean {
return instance?.networkType?.value?.let { isAddressValid(address, it) } == true
return WalletManager.instance?.networkType?.value?.let {
isAddressValid(
address,
it
)
} == true
}
@JvmStatic