Skip to content

Commit

Permalink
Merge pull request Tribler#13 from MKuijpers/bitcoin_progress_download
Browse files Browse the repository at this point in the history
Bitcoin progress download
  • Loading branch information
PJvanderLaan authored Mar 18, 2020
2 parents 9b412f5 + eed1870 commit 2205f42
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import java.util.*
class WalletManager(walletManagerConfiguration: WalletManagerConfiguration, walletDir: File) {
val kit: WalletAppKit
val params: NetworkParameters
var isDownloading: Boolean = true
var progress: Int = 0;

init {
Log.i("Coin", "Coin: WalletManager attempting to start.")
Expand Down Expand Up @@ -59,6 +61,7 @@ class WalletManager(walletManagerConfiguration: WalletManagerConfiguration, wall
) {
super.progress(pct, blocksSoFar, date)
val percentage = pct.toInt()
progress = percentage
println("Progress: $percentage")
Log.i("Coin", "Progress: $percentage")
}
Expand All @@ -67,13 +70,14 @@ class WalletManager(walletManagerConfiguration: WalletManagerConfiguration, wall
super.doneDownload()
Log.w("Coin", "Download Complete!")
Log.i("Coin", "Balance: ${kit.wallet().balance}")
isDownloading = false
}
})

kit.setBlockingStartup(false)
kit.startAsync()
kit.awaitRunning()
val ad = LegacyAddress.fromString(params, "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2")
val ad = LegacyAddress.fromString(params, "1CN6vbTuKioGThCb2Q7mQF1fow27HZBVJP")

kit.wallet().addWatchedAddress(ad)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.util.concurrent.Executor
/**
* Singleton class for WalletManager which also sets-up Android specific things.
*/
// TODO: Clean up Thread usage.
object WalletManagerAndroid {
private var walletManager: WalletManager? = null
private var application: Application? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock

interface BitcoinViewController {
fun showView(bitcoinViewName: String)
fun showDefaultView()
fun showSharedWalletTransactionView(sharedWalletBlock: TrustChainBlock)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package nl.tudelft.ipv8.android.demo.ui.bitcoin

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import kotlinx.android.synthetic.main.fragment_blockchain_downloading.*
import nl.tudelft.ipv8.android.demo.R
import nl.tudelft.ipv8.android.demo.coin.WalletManagerAndroid
import nl.tudelft.ipv8.android.demo.ui.BaseFragment
import kotlin.concurrent.thread


/**
* A simple [Fragment] subclass.
* Use the [BlockchainDownloading.newInstance] factory method to
* create an instance of this fragment.
*/
class BlockchainDownloading(
override val controller: BitcoinViewController
) : BitcoinView, BaseFragment(R.layout.fragment_blockchain_downloading) {

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
bitcoin_progress_continue.setOnClickListener {
controller.showDefaultView()
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val fragment = inflater.inflate(R.layout.fragment_blockchain_downloading, container, false)
fragment.findViewById<TextView>(R.id.bitcoin_download_percentage).text = "${WalletManagerAndroid.getInstance().progress.toString()}%"
fragment.findViewById<ProgressBar>(R.id.bitcoin_download_progress).progress = WalletManagerAndroid.getInstance().progress
thread {
while (WalletManagerAndroid.getInstance().progress < 100) {
Thread.sleep(500)
fragment.findViewById<TextView>(R.id.bitcoin_download_percentage).text = "${WalletManagerAndroid.getInstance().progress}%"
fragment.findViewById<ProgressBar>(R.id.bitcoin_download_progress).progress = WalletManagerAndroid.getInstance().progress
}
controller.showDefaultView()
}
return fragment
}



companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment BlockchainDownloading.
*/
@JvmStatic
fun newInstance(viewController: BitcoinViewController) = BlockchainDownloading(viewController)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import nl.tudelft.ipv8.util.toHex
* Use the [BitcoinFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class JoinNetworkFragment (
class JoinNetworkFragment(
override val controller: BitcoinViewController
) : BitcoinView, BaseFragment(R.layout.fragment_join_network) {
private val tempBitcoinPk = ByteArray(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup
import nl.tudelft.ipv8.android.demo.CoinCommunity
import nl.tudelft.ipv8.android.demo.R
import nl.tudelft.ipv8.android.demo.coin.WalletManagerAndroid
import nl.tudelft.ipv8.android.demo.coin.CoinUtil
import nl.tudelft.ipv8.android.demo.ui.BaseFragment
import nl.tudelft.ipv8.attestation.trustchain.TrustChainBlock
Expand All @@ -28,6 +29,7 @@ class LandingBitcoinFragment : BaseFragment(R.layout.fragment_landing_bitcoin),
"BitcoinFragment" to BitcoinFragment.newInstance(this),
"JoinNetworkFragment" to JoinNetworkFragment.newInstance(this),
"CreateSWFragment" to CreateSWFragment.newInstance(this),
"BlockchainDownloading" to BlockchainDownloading.newInstance(this),
"MySharedWalletsFragment" to MySharedWalletFragment.newInstance(this)
)

Expand All @@ -37,7 +39,11 @@ class LandingBitcoinFragment : BaseFragment(R.layout.fragment_landing_bitcoin),
}

private fun loadInitialView() {
showView("BitcoinFragment")
if (WalletManagerAndroid.getInstance().isDownloading) {
showView("BlockchainDownloading")
} else {
showView("BitcoinFragment")
}
}

override fun onCreateView(
Expand All @@ -62,13 +68,19 @@ class LandingBitcoinFragment : BaseFragment(R.layout.fragment_landing_bitcoin),
transaction.commit()
}

override fun showDefaultView() {
showView("BitcoinFragment")
}

override fun showSharedWalletTransactionView(sharedWalletBlock: TrustChainBlock) {
val publicKey = sharedWalletBlock.publicKey.toHex()
val parsedTransaction = CoinUtil.parseTransaction(sharedWalletBlock.transaction)
val votingThresholdText = "${parsedTransaction.getInt(CoinCommunity.SW_VOTING_THRESHOLD)} %"
val entranceFeeText = "${parsedTransaction.getDouble(CoinCommunity.SW_ENTRANCE_FEE)} BTC"
val users = "${parsedTransaction.getJSONArray(CoinCommunity.SW_TRUSTCHAIN_PKS).length()} user(s) in this shared wallet"
val fragment = JoinNetworkSteps.newInstance(publicKey, votingThresholdText, entranceFeeText, users)
val users =
"${parsedTransaction.getJSONArray(CoinCommunity.SW_TRUSTCHAIN_PKS).length()} user(s) in this shared wallet"
val fragment =
JoinNetworkSteps.newInstance(publicKey, votingThresholdText, entranceFeeText, users)
val transaction = parentFragmentManager.beginTransaction()
transaction.replace(R.id.landing_bitcoin_container, fragment)
transaction.addToBackStack(null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlockchainDownloading">

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Wait Whilst the Ketting is Downloading"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">

<Button
android:id="@+id/bitcoin_progress_continue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Continue Regardless" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">

<TextView
android:id="@+id/bitcoin_download_percentage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAlignment="center" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="20px">

<ProgressBar
android:id="@+id/bitcoin_download_progress"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="500dp"
android:max="100"
android:minHeight="60dp"
android:progress="10" />

</LinearLayout>
</FrameLayout>
5 changes: 5 additions & 0 deletions demo-android/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
android:name="nl.tudelft.ipv8.android.demo.ui.bitcoin.CreateSWFragment"
android:label="fragment_create_sw"
tools:layout="@layout/fragment_create_sw" />
<fragment
android:id="@+id/blockchainDownloading"
android:name="nl.tudelft.ipv8.android.demo.ui.bitcoin.BlockchainDownloading"
android:label="fragment_blockchain_downloading"
tools:layout="@layout/fragment_blockchain_downloading" />
<fragment
android:id="@+id/mySharedWalletsFragment"
android:name="nl.tudelft.ipv8.android.demo.ui.bitcoin.MySharedWalletsFragment"
Expand Down

0 comments on commit 2205f42

Please sign in to comment.