From 7e5a4f920844ab905459a9b3df0fc5f1640fe250 Mon Sep 17 00:00:00 2001 From: Malcolm MacLeod Date: Mon, 1 Mar 2021 21:09:39 +0200 Subject: [PATCH] ANDROID: UI: Basic sell button --- .../android/unity_wallet/app/build.gradle | 8 +- .../ReceiveFragment.kt | 4 +- .../main_activity_fragments/SendFragment.kt | 95 +++++++++++++++++-- .../app/src/main/res/layout/fragment_send.xml | 20 +++- .../app/src/main/res/values/strings.xml | 1 + 5 files changed, 114 insertions(+), 14 deletions(-) diff --git a/src/frontend/android/unity_wallet/app/build.gradle b/src/frontend/android/unity_wallet/app/build.gradle index f6f0e3db86..8dbc3c1f42 100644 --- a/src/frontend/android/unity_wallet/app/build.gradle +++ b/src/frontend/android/unity_wallet/app/build.gradle @@ -56,8 +56,8 @@ android { resValue "string", "app_name", "Gulden-testnet" buildConfigField "boolean", "TESTNET", "true" buildConfigField "String", "OLD_WALLET_PROTOBUF_FILENAME", '"wallet-protobuf-testnet"' - versionCode 300420 - versionName = "2.3.6" + versionCode 300421 + versionName = "2.3.8" resValue "string", "about_text_app_name", "Gulden testnet $versionName ($versionCode)" resValue "string", "about_text_app_copyright", "© 2021 Gulden.com BV" } @@ -68,8 +68,8 @@ android { resValue "string", "app_name", "Gulden" buildConfigField "boolean", "TESTNET", "false" buildConfigField "String", "OLD_WALLET_PROTOBUF_FILENAME", '"wallet-protobuf"' - versionCode 410 - versionName = "2.3.6" + versionCode 411 + versionName = "2.3.8" resValue "string", "about_text_app_name", "Gulden $versionName ($versionCode)" resValue "string", "about_text_app_copyright", "© 2021 Gulden.com BV" } diff --git a/src/frontend/android/unity_wallet/app/src/main/java/com/gulden/unity_wallet/main_activity_fragments/ReceiveFragment.kt b/src/frontend/android/unity_wallet/app/src/main/java/com/gulden/unity_wallet/main_activity_fragments/ReceiveFragment.kt index 39e49cbc18..ee758d0355 100644 --- a/src/frontend/android/unity_wallet/app/src/main/java/com/gulden/unity_wallet/main_activity_fragments/ReceiveFragment.kt +++ b/src/frontend/android/unity_wallet/app/src/main/java/com/gulden/unity_wallet/main_activity_fragments/ReceiveFragment.kt @@ -55,11 +55,11 @@ class ReceiveFragment : AppBaseFragment() { private fun copyToClipboard() { val clip: ClipData = ClipData.newPlainText(getString(R.string.gulden_address_clipboard_label), currentAddressLabel.text) - val clipboard = getSystemService(activity!!, ClipboardManager::class.java) as ClipboardManager + val clipboard = getSystemService(requireActivity(), ClipboardManager::class.java) as ClipboardManager clipboard.setPrimaryClip(clip) val toast = Toast.makeText(context, getString(R.string.copied_to_clipboard_toast), Toast.LENGTH_SHORT) - toast.setGravity(Gravity.TOP, 0, context!!.dimen(R.dimen.top_toast_offset) ) + toast.setGravity(Gravity.TOP, 0, requireContext().dimen(R.dimen.top_toast_offset) ) toast.show() } diff --git a/src/frontend/android/unity_wallet/app/src/main/java/com/gulden/unity_wallet/main_activity_fragments/SendFragment.kt b/src/frontend/android/unity_wallet/app/src/main/java/com/gulden/unity_wallet/main_activity_fragments/SendFragment.kt index 45da420a86..e462ad63fe 100644 --- a/src/frontend/android/unity_wallet/app/src/main/java/com/gulden/unity_wallet/main_activity_fragments/SendFragment.kt +++ b/src/frontend/android/unity_wallet/app/src/main/java/com/gulden/unity_wallet/main_activity_fragments/SendFragment.kt @@ -8,6 +8,7 @@ package com.gulden.unity_wallet.main_activity_fragments import android.content.ClipboardManager import android.content.Context import android.content.Intent +import android.net.Uri import android.os.Bundle import android.text.Editable import android.text.Html @@ -18,24 +19,31 @@ import android.view.View import android.view.ViewGroup import android.view.inputmethod.InputMethodManager import android.widget.EditText +import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.android.volley.* +import com.android.volley.toolbox.StringRequest +import com.android.volley.toolbox.Volley import com.google.android.gms.common.api.CommonStatusCodes import com.google.android.gms.vision.barcode.Barcode import com.gulden.barcodereader.BarcodeCaptureActivity import com.gulden.jniunifiedbackend.AddressRecord import com.gulden.jniunifiedbackend.ILibraryController +import com.gulden.jniunifiedbackend.IWalletController import com.gulden.jniunifiedbackend.UriRecipient import com.gulden.unity_wallet.* import com.gulden.unity_wallet.ui.AddressBookAdapter import com.gulden.unity_wallet.util.AppBaseFragment +import com.gulden.unity_wallet.R import com.gulden.unity_wallet.util.SwipeToDeleteCallback import kotlinx.android.synthetic.main.add_address_entry.view.* import kotlinx.android.synthetic.main.fragment_send.* import org.jetbrains.anko.support.v4.runOnUiThread +import org.json.JSONObject class SendFragment : AppBaseFragment(), UnityCore.Observer { @@ -47,9 +55,9 @@ class SendFragment : AppBaseFragment(), UnityCore.Observer { private fun handleAddToAddressBookButtonClick(view : View) { - val builder = AlertDialog.Builder(this.context!!) + val builder = AlertDialog.Builder(this.requireContext()) builder.setTitle(getString(R.string.dialog_title_add_address)) - val layoutInflater : LayoutInflater = this.context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + val layoutInflater : LayoutInflater = this.requireContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val viewInflated : View = layoutInflater.inflate(R.layout.add_address_entry, view.rootView as ViewGroup, false) val inputAddress = viewInflated.findViewById(R.id.address) as EditText val inputLabel = viewInflated.findViewById(R.id.addressName) as EditText @@ -96,7 +104,7 @@ class SendFragment : AppBaseFragment(), UnityCore.Observer { viewInflated.address.requestFocus() viewInflated.address.post { - val imm = this.context!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + val imm = this.requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(viewInflated.address, InputMethodManager.SHOW_IMPLICIT) } } @@ -107,7 +115,7 @@ class SendFragment : AppBaseFragment(), UnityCore.Observer { clipboardButton.setOnClickListener { val text = clipboardText() try { - SendCoinsFragment.newInstance(createRecipient(text), false).show(activity!!.supportFragmentManager, SendCoinsFragment::class.java.simpleName) + SendCoinsFragment.newInstance(createRecipient(text), false).show(requireActivity().supportFragmentManager, SendCoinsFragment::class.java.simpleName) } catch (e: InvalidRecipientException) { errorMessage(getString(R.string.clipboard_no_valid_address)) @@ -115,7 +123,7 @@ class SendFragment : AppBaseFragment(), UnityCore.Observer { } imageViewAddToAddressBook.setOnClickListener { - handleAddToAddressBookButtonClick(view!!) + handleAddToAddressBookButtonClick(requireView()) } qrButton.setOnClickListener { @@ -124,18 +132,91 @@ class SendFragment : AppBaseFragment(), UnityCore.Observer { startActivityForResult(intent, BARCODE_READER_REQUEST_CODE) } + sellButton.setOnClickListener { + // Send a post request to blockhut with our wallet/address info; and then launch the site if we get a positive response. + val MyRequestQueue = Volley.newRequestQueue(context) + val failURL = "https://gulden.com/sell" + val request = object : StringRequest(Request.Method.POST,"https://blockhut.com/buysession.php", + Response.Listener { response -> + try + { + var jsonResponse = JSONObject(response); + if (jsonResponse.getInt("status_code") == 200) + { + var sessionID = jsonResponse.getString("sessionid") + val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://blockhut.com/sell.php?sessionid=%s".format(sessionID))) + startActivity(intent) + } + else + { + // Redirect user to the default fallback site + //fixme: Do something with the status message here + //var statusMessage = jsonResponse.getString("status_message") + val intent = Intent(failURL) + startActivity(intent) + } + } + catch (e:Exception) + { + // Redirect user to the default fallback site + //fixme: Do something with the error message here + val intent = Intent(failURL) + startActivity(intent) + } + }, + Response.ErrorListener + { + // If we are sure its a local connectivity issue, alert the user, otherwise send them to the default fallback site + if (it is NetworkError || it is AuthFailureError || it is NoConnectionError) + { + Toast.makeText(context, getString(R.string.error_check_internet_connection), Toast.LENGTH_SHORT).show() + } + else + { + val intent = Intent(failURL) + startActivity(intent) + } + } + ) + // Force values to be send at x-www-form-urlencoded + { + override fun getParams(): MutableMap { + val params = HashMap() + params["address"] = ILibraryController.GetReceiveAddress().toString(); + params["uuid"] = IWalletController.GetUUID(); + params["currency"] = "gulden"; + return params; + } + override fun getHeaders(): MutableMap { + val params = HashMap(); + params.put("Content-Type","application/x-www-form-urlencoded"); + return params; + } + }; + + // Volley request policy, only one time request to avoid duplicate transaction + request.retryPolicy = DefaultRetryPolicy( + DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, + 1, // DefaultRetryPolicy.DEFAULT_MAX_RETRIES = 2 + 1f // DefaultRetryPolicy.DEFAULT_BACKOFF_MULT + ) + + // Add the volley post request to the request queue + MyRequestQueue.add(request) + } + ClipboardManager.OnPrimaryClipChangedListener { checkClipboardEnable() } val addresses = ILibraryController.getAddressBookRecords() val adapter = AddressBookAdapter(addresses) { position, address -> val recipient = UriRecipient(true, address.address, address.name, address.desc, 0) - SendCoinsFragment.newInstance(recipient, false).show(activity!!.supportFragmentManager, SendCoinsFragment::class.java.simpleName) + SendCoinsFragment.newInstance(recipient, false).show(requireActivity().supportFragmentManager, SendCoinsFragment::class.java.simpleName) } addressBookList.adapter = adapter addressBookList.layoutManager = LinearLayoutManager(context) - val swipeHandler = object : SwipeToDeleteCallback(context!!) { + val swipeHandler = object : SwipeToDeleteCallback(requireContext()) { override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { adapter.removeAt(viewHolder.adapterPosition) } diff --git a/src/frontend/android/unity_wallet/app/src/main/res/layout/fragment_send.xml b/src/frontend/android/unity_wallet/app/src/main/res/layout/fragment_send.xml index cc8a393218..12b4a0d9f5 100644 --- a/src/frontend/android/unity_wallet/app/src/main/res/layout/fragment_send.xml +++ b/src/frontend/android/unity_wallet/app/src/main/res/layout/fragment_send.xml @@ -150,8 +150,10 @@ file COPYING app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> +