Integrating Storage and Wallet Functionalities
The page provides instructions on how to integrate storage and wallet functionalities to home page of your App.
Main Activity Directory
The following directories lie inside the android studio project:
│ ├── mainactivity
│ │ ├── MainActivity.kt (Launched at startup and sets up action bar and saved wallet.)
│ │ └── MainViewModel.kt (Core wallet saving classes using files in android)
└── ZusExampleApplication.kt (Application class for initializing static libraries. )
UI/mainactivity/MainActivity.kt
package org.zus.bolt.helloworld.ui.mainactivity
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import kotlinx.coroutines.runBlocking
import org.zus.bolt.helloworld.R
import org.zus.bolt.helloworld.databinding.MainActivityBinding
import org.zus.bolt.helloworld.utils.Utils
class MainActivity : AppCompatActivity() {
private val READ_AND_WRITE_STORAGE_PERMISSION: Int = 1
private lateinit var binding: MainActivityBinding
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var viewModel: MainViewModel
private lateinit var utils: Utils
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
binding = MainActivityBinding.inflate(layoutInflater)
/* ViewModel to store the created wallet and allocation until the app is running. */
viewModel = ViewModelProvider(this)[MainViewModel::class.java]
utils = Utils(this)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
/* Setting app bar for the back button navigation. */
val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
/* Setting wallet json. */
runBlocking {
viewModel.wallet = utils.getWalletModel()
viewModel.setWalletJson(utils.readWalletFromFileJSON())
Log.i("WalletDetails", "json: ${viewModel.wallet?.walletJson}")
}
val permissions = arrayOf(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
)
ActivityCompat.requestPermissions(
this,
permissions,
READ_AND_WRITE_STORAGE_PERMISSION
)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment_content_main)
return navController.navigateUp(appBarConfiguration)
|| super.onSupportNavigateUp()
}
}
Describing Code:
MainActivity Class
specifies what the app does and how it should respond to the user. Its created in Android Studio by default with blank implementation. Developers implement it according to the desired activity lifecycle of an app.Line 19 to 23 defines the following variables in the class: A
viewModel
variable to hold the MainView of the App Autils
variable to access util functions Abinding
variable to bind app data to the view of the app. AappBarConfiguration
to hold main AppView bar.On Line 24 to 26
onCreate()
method is overriden and is called with thesuper
keyword is to call the constructor of its parent class to access the saved App Activity states
Line 27 creates an instance of binding class and binds the main activity state to interacts with views.
Line 29 viewModel temporarliy holds the created wallet until the app is viewable
Line 32 passes the rootview and make it the active view on the screen
Line 33 passes the appbar view and make it the active view on screen .
Line 35 to 55 sets up App bar with the app title and overflow menu for app navigation .Also wallet is set to be use.
Line 57 to 62 overrides the
onSupportNavigateUp()
method which comes fromAppCompatActivity
class. Overriding it helpsNavigationUI
correctly support the up navigation or even the drawer layout menu.
UI/mainactivity/MainViewModel.kt
package org.zus.bolt.helloworld.ui.mainactivity
import androidx.lifecycle.ViewModel
import org.zus.bolt.helloworld.models.bolt.WalletModel
import sdk.StorageSDK
class MainViewModel : ViewModel() {
var wallet: WalletModel? = null
var storageSDK: StorageSDK? = null
fun setWalletJson(walletJson: String) {
wallet?.walletJson = walletJson
}
}
Describing Code :
Line 7 Class MainViewModel
defines the following properties and fields for creating the MainActivity fragment to use.Line 9 A writable
wallet
variable that can hold WalletModel Instance and access its methods.Line 10 A writable
storageSDK
variable that can hold StorageSDK Instance and access its methods.Line 12 to 14 defines function
setWalletjson
which sets Wallet.
UI/ZusExampleApplication.kt
package org.zus.bolt.helloworld.ui
import android.app.Application
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import org.zus.bolt.helloworld.utils.Utils
import sdk.Sdk
import zcncore.Zcncore
private const val TAG_APP = "HelloWorldApplication"
class ZusExampleApplication : Application() {
private val utils: Utils by lazy {
Utils(applicationContext)
}
init {
/* Loading .so files/native libraries in the jNi libs for proper functioning of the app with gosdk. */
try {
System.loadLibrary("c++_shared")
System.loadLibrary("bls384")
Log.i(TAG_APP, "successfully initialized native libraries")
} catch (e: UnsatisfiedLinkError) {
Log.e(TAG_APP, "failed to initialize native libraries UnstatisfiedLinkError", e)
} catch (e: Exception) {
Log.e(TAG_APP, "failed to initialize native libraries Exception", e)
}
}
override fun onCreate() {
super.onCreate()
try {
// Initialize Zcncore and Sdk with chain config at the start of the Application.
Zcncore.init(utils.config)
Sdk.init(utils.config)
if (utils.isWalletExist())
Zcncore.setWalletInfo(utils.readWalletFromFileJSON(), false)
Log.i(TAG_APP, "onCreate: sdk initialized successfully")
} catch (e: Exception) {
Log.e(TAG_APP, "onCreate: sdk initialization failed Exception", e)
}
}
}
Describing Code :
Line 3 to 8 loads certain libraries for properly intializing the zcncore.
Line 12 ZusExampleApplication class primarly initializes the zcncore required to interface with dStorage wallet and storage functions here .
Line 14 initalize zcncore , storage network configuration which is fetched from Utils.kt getConfigFromAssets function.
Line 30 onCreate() method initializes the zcncore libraries at line 31 for Ui to use.
Line 37 to 42 execute thrown errors if storage network and wallet config is not found If not found wallet is set via GoSDK setWalletInfo function.
Last updated