Skip to content

Commit

Permalink
fragment factory fix
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Sep 8, 2024
1 parent 537b5c2 commit 2cb2b6e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.tonapps.blockchain.ton.TonNetwork
import com.tonapps.blockchain.ton.contract.BaseWalletContract
import com.tonapps.blockchain.ton.contract.WalletFeature
import com.tonapps.blockchain.ton.contract.WalletVersion
import com.tonapps.blockchain.ton.extensions.EmptyPrivateKeyEd25519
import com.tonapps.blockchain.ton.extensions.hex
import com.tonapps.blockchain.ton.extensions.publicKey
import com.tonapps.blockchain.ton.extensions.toAccountId
Expand All @@ -33,6 +34,15 @@ data class WalletEntity(

companion object {

val EMPTY = WalletEntity(
id = "",
publicKey = EmptyPrivateKeyEd25519.publicKey(),
type = Wallet.Type.Default,
version = WalletVersion.V5BETA,
label = Wallet.Label("", "", 0),
ledger = null
)

@JvmField
val CREATOR = object : Parcelable.Creator<WalletEntity> {
override fun createFromParcel(parcel: Parcel) = WalletEntity(parcel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package com.tonapps.tonkeeper.ui.base

import android.content.Context
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.tonapps.extensions.getParcelableCompat
import com.tonapps.wallet.data.account.entities.WalletEntity
import uikit.base.BaseFragment
import uikit.navigation.Navigation
import kotlin.reflect.KClass

abstract class BaseWalletScreen<C: ScreenContext>(
@LayoutRes layoutId: Int,
screenContext: C,
screenContext: C
): BaseFragment(layoutId), BaseWalletVM.Holder {

private companion object {
Expand All @@ -32,7 +35,9 @@ abstract class BaseWalletScreen<C: ScreenContext>(
}

init {
putParcelableArg(ARG_SCREEN_CONTEXT, screenContext)
if (!(screenContext is ScreenContext.Wallet && screenContext.isEmpty)) {
putParcelableArg(ARG_SCREEN_CONTEXT, screenContext)
}
}

override fun onCreateView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ sealed class ScreenContext: Parcelable {
data object None : ScreenContext()

@Parcelize
data class Wallet(val wallet: WalletEntity) : ScreenContext()
data object Ignore : ScreenContext()

@Parcelize
data class Wallet(val wallet: WalletEntity) : ScreenContext() {

val isEmpty: Boolean
get() = wallet.id.isBlank()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.tonapps.tonkeeper.ui.base

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import com.tonapps.wallet.data.account.entities.WalletEntity

class WalletFragmentFactory: FragmentFactory() {

override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
val fragmentClass = loadFragmentClass(classLoader, className)
val constructors = fragmentClass.constructors
if (constructors.size > 1) {
throw IllegalStateException("Fragment class should have only one constructor")
}
val constructor = constructors.first()
val parameters = constructor.parameterTypes
if (parameters.size > 1) {
throw IllegalStateException("Fragment class should have only one constructor with one parameter")
} else if (parameters.isEmpty()) {
return fragmentClass.getConstructor().newInstance()
}
val parameter = parameters.first()
if (parameter == WalletEntity::class.java) {
return fragmentClass.getDeclaredConstructor(WalletEntity::class.java).newInstance(WalletEntity.EMPTY)
}
return super.instantiate(classLoader, className)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TokenPickerView @JvmOverloads constructor(

private val iconView: FrescoView
private val titleView: AppCompatTextView
private val pickerRequestKey = "token.picker.view@$id"
private val pickerRequestKey = "@android:view:$id"
private val pickerCallback: (Bundle) -> Unit = { bundle ->
bundle.getParcelableCompat<TokenEntity>(TokenPickerScreen.TOKEN)?.let {
token = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.tonapps.tonkeeper.ui.screen.transaction.TransactionScreen
import com.tonapps.tonkeeper.extensions.toast
import com.tonapps.tonkeeper.fragment.tonconnect.auth.TCAuthFragment
import com.tonapps.tonkeeper.ui.base.BaseWalletActivity
import com.tonapps.tonkeeper.ui.base.WalletFragmentFactory
import com.tonapps.tonkeeper.ui.screen.backup.main.BackupScreen
import com.tonapps.tonkeeper.ui.screen.battery.BatteryScreen
import com.tonapps.tonkeeper.ui.screen.init.InitArgs
Expand Down Expand Up @@ -61,6 +62,7 @@ class RootActivity: BaseWalletActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
setTheme(viewModel.theme.resId)
supportFragmentManager.fragmentFactory = WalletFragmentFactory()
super.onCreate(savedInstanceState)
legacyRN.setActivity(this)
windowInsetsController.isAppearanceLightStatusBars = viewModel.theme.light
Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8

systemProp.org.gradle.internal.launcher.welcomeMessageEnabled=false

kotlin.code.style=official

android.useAndroidX=true
android.nonTransitiveRClass=true
android.enableR8.fullMode=true
android.experimental.enableScreenshotTest=true

0 comments on commit 2cb2b6e

Please sign in to comment.