Skip to content

Commit

Permalink
[ABW-3712] Allow to hide/unhide resources on Profile (#199)
Browse files Browse the repository at this point in the history
* Changes to AppPreferences with commented tests

* WIP

* get all hidden resources

* fix tests

* WIP

* WIP

* bump version

* add flag to Security

* uncomment test

* WIP

* fixes

* fix iOS Tests

* Add bool type macro and use it for Security flags

* move macro to its own file

* missing tests and docs

* Hidden resources kt extesions (#200)

* Add kt extensions for resource preferences

* Remove packaging option

* Update asset preferences kt extensions

* revert JSON changes

---------

Co-authored-by: sergiupuhalschi-rdx <[email protected]>
  • Loading branch information
matiasbzurovski and sergiupuhalschi-rdx authored Aug 16, 2024
1 parent cde0859 commit 5903204
Show file tree
Hide file tree
Showing 29 changed files with 1,003 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "1.1.5"
version = "1.1.6"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import UIKit
extension AppleHostInfoDriver: HostInfoDriver {

public func hostOs() async -> HostOs {
return HostOs.ios(version: UIDevice.current.systemVersion)
return await HostOs.ios(version: UIDevice.current.systemVersion)
}

nonisolated public func hostDeviceName() async -> String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// File.swift
//
//
// Created by Matias Bzurovski on 13/8/24.
//

import Foundation
import SargonUniFFI

extension [AssetPreference] {
public var hiddenAssets: [AssetAddress] {
assetPreferencesGetHiddenAssets(assetPreferences: self)
}

public mutating func hideAsset(asset: AssetAddress) {
self = assetPreferencesHideAsset(assetPreferences: self, asset: asset)
}

public mutating func unhideAsset(asset: AssetAddress) {
self = assetPreferencesUnhideAsset(assetPreferences: self, asset: asset)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// File.swift
//
//
// Created by Matias Bzurovski on 13/8/24.
//

import Foundation
import SargonUniFFI

#if DEBUG
extension [AssetPreference] {
public static let sample: Self = newAssetPreferencesSample()
public static let sampleOther: Self = newAssetPreferencesSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File.swift
//
//
// Created by Matias Bzurovski on 13/8/24.
//

import Foundation
import SargonUniFFI

public typealias AssetPreferences = [AssetPreference]

//extension AssetPreferences: SargonModel {}
31 changes: 31 additions & 0 deletions apple/Tests/TestCases/Profile/AssetPreferencesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// File.swift
//
//
// Created by Matias Bzurovski on 13/8/24.
//

import CustomDump
import Foundation
import Sargon
import SargonUniFFI
import XCTest

final class AssetPreferencesTests: TestCase {
func test_hidden_resources() {
var sut = AssetPreferences()
XCTAssertTrue(sut.hiddenAssets.isEmpty)

// Hide assets
sut.hideAsset(asset: .fungible(.sample))
sut.hideAsset(asset: .nonFungible(.sample))
sut.hideAsset(asset: .fungible(.sampleOther))

XCTAssertEqual(sut.hiddenAssets, [.fungible(.sample), .nonFungible(.sample), .fungible(.sampleOther)])

// Unhide assets
sut.unhideAsset(asset: .fungible(.sampleOther))
sut.unhideAsset(asset: .nonFungible(.sample))
XCTAssertEqual(sut.hiddenAssets, [.fungible(.sample)])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@
},
"transaction" : {
"defaultDepositGuarantee" : "0.975"
},
"resource": {
"resourceFlags": {
"resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd": [
"deletedByUser"
]
}
}
},
"factorSources" : [
Expand Down
20 changes: 10 additions & 10 deletions fixtures/vector/profile_encrypted_by_password_empty.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions fixtures/vector/profile_encrypted_by_password_of_babylon.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion jvm/sargon-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ android {
java.srcDir("${buildDir}/generated/src/release/java")
}
}
packaging { resources.excludes.add("META-INF/*") }
}

cargoNdk {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.AssetAddress
import com.radixdlt.sargon.AssetPreference
import com.radixdlt.sargon.annotation.KoverIgnore
import com.radixdlt.sargon.assetPreferencesGetHiddenAssets
import com.radixdlt.sargon.assetPreferencesHideAsset
import com.radixdlt.sargon.assetPreferencesUnhideAsset

class AssetPreferences private constructor(
private val array: IdentifiedArray<AssetAddress, AssetPreference>
) : IdentifiedArray<AssetAddress, AssetPreference> by array {

constructor(assetPreferences: List<AssetPreference>) : this(
IdentifiedArrayImpl(
elements = assetPreferences,
identifier = { it.assetAddress }
)
)

constructor(vararg assetPreference: AssetPreference) : this(assetPreferences = assetPreference.asList())

@KoverIgnore // False positive in javaClass check
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as AssetPreferences

return array == other.array
}

override fun hashCode(): Int {
return array.hashCode()
}

@KoverIgnore
override fun toString(): String {
return "AssetPreferences(array=$array)"
}

companion object

}

fun List<AssetPreference>.asIdentifiable() = AssetPreferences(assetPreferences = this)

val AssetPreferences.hiddenAssets
get() = assetPreferencesGetHiddenAssets(asList())

fun AssetPreferences.hideAsset(assetAddress: AssetAddress) = assetPreferencesHideAsset(asList(), assetAddress).asIdentifiable()

fun AssetPreferences.unhideAsset(assetAddress: AssetAddress) = assetPreferencesUnhideAsset(asList(), assetAddress).asIdentifiable()
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.radixdlt.sargon.samples

import com.radixdlt.sargon.annotation.UsesSampleValues
import com.radixdlt.sargon.extensions.AssetPreferences
import com.radixdlt.sargon.extensions.asIdentifiable
import com.radixdlt.sargon.newAssetPreferencesSample
import com.radixdlt.sargon.newAssetPreferencesSampleOther

@UsesSampleValues
val AssetPreferences.Companion.sample: Sample<AssetPreferences>
get() = object : Sample<AssetPreferences> {

override fun invoke(): AssetPreferences = newAssetPreferencesSample().asIdentifiable()

override fun other(): AssetPreferences = newAssetPreferencesSampleOther().asIdentifiable()
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.radixdlt.sargon

import com.radixdlt.sargon.extensions.AssetPreferences
import com.radixdlt.sargon.extensions.default
import com.radixdlt.sargon.extensions.toDecimal192
import com.radixdlt.sargon.samples.Sample
import com.radixdlt.sargon.samples.sample
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class AppPreferencesTest: SampleTestable<AppPreferences> {
class AppPreferencesTest : SampleTestable<AppPreferences> {

override val samples: List<Sample<AppPreferences>>
get() = listOf(AppPreferences.sample)
Expand All @@ -24,9 +25,11 @@ class AppPreferencesTest: SampleTestable<AppPreferences> {
security = Security(
isCloudProfileSyncEnabled = true,
isDeveloperModeEnabled = false,
isAdvancedLockEnabled = false,
securityStructuresOfFactorSourceIds = emptyList()
),
transaction = TransactionPreferences(defaultDepositGuarantee = 0.99.toDecimal192())
transaction = TransactionPreferences(defaultDepositGuarantee = 0.99.toDecimal192()),
assets = AssetPreferences().asList()
),
AppPreferences.default()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.radixdlt.sargon

import com.radixdlt.sargon.extensions.AssetPreferences
import com.radixdlt.sargon.extensions.hiddenAssets
import com.radixdlt.sargon.extensions.hideAsset
import com.radixdlt.sargon.extensions.unhideAsset
import com.radixdlt.sargon.samples.Sample
import com.radixdlt.sargon.samples.sample
import com.radixdlt.sargon.samples.sampleMainnet
import com.radixdlt.sargon.samples.sampleStokenet
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class AssetPreferencesTest : SampleTestable<AssetPreferences> {

override val samples: List<Sample<AssetPreferences>>
get() = listOf(AssetPreferences.sample)

@Test
fun testHiddenAssets() {
var sut = AssetPreferences()

assertTrue(sut.hiddenAssets.isEmpty())

// Hide assets
sut = sut.hideAsset(AssetAddress.Fungible(ResourceAddress.sampleMainnet()))
.hideAsset(AssetAddress.Fungible(ResourceAddress.sampleStokenet()))
.hideAsset(AssetAddress.NonFungible(NonFungibleGlobalId.sample()))
.hideAsset(AssetAddress.PoolUnit(PoolAddress.sampleMainnet()))

assertEquals(
listOf(
AssetAddress.Fungible(ResourceAddress.sampleMainnet()),
AssetAddress.Fungible(ResourceAddress.sampleStokenet()),
AssetAddress.NonFungible(NonFungibleGlobalId.sample()),
AssetAddress.PoolUnit(PoolAddress.sampleMainnet())
),
sut.hiddenAssets
)

// Unhide assets
sut = sut.unhideAsset(AssetAddress.Fungible(ResourceAddress.sampleStokenet()))
.unhideAsset(AssetAddress.NonFungible(NonFungibleGlobalId.sample()))

assertEquals(
listOf(
AssetAddress.Fungible(ResourceAddress.sampleMainnet()),
AssetAddress.PoolUnit(PoolAddress.sampleMainnet())
),
sut.hiddenAssets
)
}
}
97 changes: 97 additions & 0 deletions src/core/types/bool_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use crate::prelude::*;

macro_rules! decl_bool_type {
($name:ident, $default_value:expr) => {
#[derive(
Serialize,
Deserialize,
Debug,
PartialEq,
Eq,
Clone,
Hash,
derive_more::Display,
)]
#[serde(transparent)]
pub struct $name(pub bool);

impl Default for $name {
fn default() -> Self {
$name($default_value)
}
}

impl HasSampleValues for $name {
fn sample() -> Self {
$name($default_value)
}

fn sample_other() -> Self {
$name(!$default_value)
}
}

impl Deref for $name {
type Target = bool;

fn deref(&self) -> &Self::Target {
&self.0
}
}

uniffi::custom_newtype!($name, bool);
};
}

pub(crate) use decl_bool_type;

#[cfg(test)]
mod tests {
decl_bool_type!(ExampleTrue, true);
decl_bool_type!(ExampleFalse, false);

use crate::prelude::*;

#[test]
fn equality() {
assert_eq!(ExampleTrue::sample(), ExampleTrue::sample());
assert_eq!(ExampleTrue::sample_other(), ExampleTrue::sample_other());

assert_eq!(ExampleFalse::sample(), ExampleFalse::sample());
assert_eq!(ExampleFalse::sample_other(), ExampleFalse::sample_other());
}

#[test]
fn inequality() {
assert_ne!(ExampleTrue::sample(), ExampleTrue::sample_other());

assert_ne!(ExampleFalse::sample(), ExampleFalse::sample_other());
}

#[test]
fn default() {
assert!(*ExampleTrue::default());

assert!(!*ExampleFalse::default());
}

#[test]
fn debug() {
let str = "ExampleTrue(true)";
let sut = ExampleTrue::sample();
assert_eq!(format!("{:?}", sut), str);
}

#[test]
fn modification() {
let mut example_true = ExampleTrue::default();
assert!(*example_true);
example_true.0 = false;
assert!(!*example_true);

let mut example_false = ExampleFalse::default();
assert!(!*example_false);
example_false.0 = true;
assert!(*example_false);
}
}
Loading

0 comments on commit 5903204

Please sign in to comment.