Skip to content

Commit

Permalink
[ABW-3756] Add preferences for AuthorizedDapp (#206)
Browse files Browse the repository at this point in the history
* Add preferences for AuthorizedDapp

* bump version

* add to AuthorizedDappDetailed

* Updates on Swift code

* Remove serde from AuthorizedDappDetailed
  • Loading branch information
matiasbzurovski authored Aug 29, 2024
1 parent d7501c8 commit dda428b
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 35 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
Expand Up @@ -3,4 +3,4 @@ resolver = "2"
members = [
"crates/sargon",
]
default-members = ["crates/sargon"]
default-members = ["crates/sargon"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// File.swift
//
//
// Created by Matias Bzurovski on 29/8/24.
//

import Foundation
import SargonUniFFI

extension AuthorizedDappDetailed {
public mutating func showDeposits(_ show: Bool) {
preferences.deposits = show ? .visible : .hidden
}

public var isDepositsVisible: Bool {
preferences.deposits == .visible
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ extension AuthorizedDapp {
public func jsonData() -> Data {
authorizedDappToJsonBytes(authorizedDapp: self)
}

public mutating func showDeposits(_ show: Bool) {
preferences.deposits = show ? .visible : .hidden
}

public var isDepositsVisible: Bool {
preferences.deposits == .visible
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ final class AuthorizedDappDetailedTests: Test<AuthorizedDappDetailed> {
XCTAssertEqual(sut.id, sut.dappDefinitionAddress)
}
}

func test_show_deposits() {
var sut = SUT.sample
XCTAssertTrue(sut.isDepositsVisible)
sut.showDeposits(false)
XCTAssertFalse(sut.isDepositsVisible)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ final class AuthorizedDappTests: Test<AuthorizedDapp> {
XCTAssertEqual(sut.id, sut.dAppDefinitionAddress)
}
}

func test_show_deposits() {
var sut = SUT.sample
XCTAssertTrue(sut.isDepositsVisible)
sut.showDeposits(false)
XCTAssertFalse(sut.isDepositsVisible)
}

func test_codable() throws {
let raw = """
Expand Down
2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "1.1.7"
version = "1.1.8"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ impl ProfileNetwork {
.clone()
.and_then(|x| DisplayName::new(x).ok()),
detailed_authorized_personas,
dapp.preferences.clone(),
))
}
}
Expand Down Expand Up @@ -294,5 +295,7 @@ mod tests {
.shared_persona_data,
&Persona::sample_mainnet_batman().persona_data
);

assert_eq!(&details.preferences, &dapp.preferences);
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
use crate::prelude::*;

#[derive(
Serialize,
Deserialize,
Clone,
Debug,
PartialEq,
Hash,
Eq,
derive_more::Display,
uniffi::Record,
Clone, Debug, PartialEq, Hash, Eq, derive_more::Display, uniffi::Record,
)]
#[display("{dapp_definition_address}")]
#[serde(rename_all = "camelCase")]
pub struct AuthorizedDappDetailed {
#[serde(rename = "networkID")]
pub network_id: NetworkID,

#[serde(rename = "dAppDefinitionAddress")]
pub dapp_definition_address: AccountAddress,

pub display_name: Option<DisplayName>,

pub detailed_authorized_personas: DetailedAuthorizedPersonas,

pub preferences: AuthorizedDappPreferences,
}

impl AuthorizedDappDetailed {
Expand All @@ -31,12 +22,14 @@ impl AuthorizedDappDetailed {
dapp_definition_address: impl Into<AccountAddress>,
display_name: impl Into<Option<DisplayName>>,
detailed_authorized_personas: DetailedAuthorizedPersonas,
preferences: AuthorizedDappPreferences,
) -> Self {
Self {
network_id: network_id.into(),
dapp_definition_address: dapp_definition_address.into(),
display_name: display_name.into(),
detailed_authorized_personas,
preferences,
}
}
}
Expand All @@ -48,6 +41,7 @@ impl HasSampleValues for AuthorizedDappDetailed {
AccountAddress::sample(),
DisplayName::sample(),
DetailedAuthorizedPersonas::sample(),
AuthorizedDappPreferences::sample(),
)
}

Expand All @@ -57,6 +51,7 @@ impl HasSampleValues for AuthorizedDappDetailed {
AccountAddress::sample_other(),
DisplayName::sample_other(),
DetailedAuthorizedPersonas::sample_other(),
AuthorizedDappPreferences::sample_other(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub struct AuthorizedDapp {
/// bad duplication of data (which might go stale), instead we refer to the
/// necessary data by IDs.
pub references_to_authorized_personas: ReferencesToAuthorizedPersonas,

/// The preferences the user has configured for this Dapp.
#[serde(default)]
pub preferences: AuthorizedDappPreferences,
}

impl IsNetworkAware for AuthorizedDapp {
Expand All @@ -51,11 +55,13 @@ impl AuthorizedDapp {
dapp_definition_address: {}
display_name: {}
references_to_authorized_personas: {}
preferences: {}
"#,
self.network_id,
self.dapp_definition_address,
self.display_name.clone().unwrap_or("<NONE>".to_owned()),
self.references_to_authorized_personas,
self.preferences,
)
}

Expand All @@ -64,6 +70,7 @@ impl AuthorizedDapp {
dapp_definition_address: DappDefinitionAddress,
display_name: impl Into<Option<String>>,
references_to_authorized_personas: ReferencesToAuthorizedPersonas,
preferences: AuthorizedDappPreferences,
) -> Self {
assert_eq!(dapp_definition_address.network_id(), network_id, "Discrepancy, found an DappDefinitionAddress on other network than {network_id}");
assert!(references_to_authorized_personas.ids().iter().all(|i| i.network_id() == network_id), "Discrepancy, found an (Authorized)Persona(Simple) on other network than {network_id}");
Expand All @@ -72,6 +79,7 @@ impl AuthorizedDapp {
dapp_definition_address,
display_name: display_name.into(),
references_to_authorized_personas,
preferences,
}
}
}
Expand All @@ -98,52 +106,56 @@ impl AuthorizedDapp {
pub fn sample_mainnet_dashboard() -> Self {
Self::new(
NetworkID::Mainnet,
"account_rdx12x0xfz2yumu2qsh6yt0v8xjfc7et04vpsz775kc3yd3xvle4w5d5k5"
"account_rdx12x0xfz2yumu2qsh6yt0v8xjfc7et04vpsz775kc3yd3xvle4w5d5k5"
.parse()
.expect("Valid Dapp Def Address"),
"Radix Dashboard".to_owned(),
ReferencesToAuthorizedPersonas::from_iter([
"Radix Dashboard".to_owned(),
ReferencesToAuthorizedPersonas::from_iter([
AuthorizedPersonaSimple::sample_mainnet(),
AuthorizedPersonaSimple::sample_mainnet_other()
])
)
]),
AuthorizedDappPreferences::sample(),
)
}
pub fn sample_mainnet_gumballclub() -> Self {
Self::new(
NetworkID::Mainnet,
"account_rdx12xuhw6v30chdkhcu7qznz9vu926vxefr4h4tdvc0mdckg9rq4afx9t"
"account_rdx12xuhw6v30chdkhcu7qznz9vu926vxefr4h4tdvc0mdckg9rq4afx9t"
.parse()
.expect("Valid Dapp Def Address"),
"Gumball Club".to_owned(),
ReferencesToAuthorizedPersonas::from_iter([
"Gumball Club".to_owned(),
ReferencesToAuthorizedPersonas::from_iter([
AuthorizedPersonaSimple::sample_mainnet_other()
])
)
]),
AuthorizedDappPreferences::sample(),
)
}
pub fn sample_stokenet_devconsole() -> Self {
Self::new(
NetworkID::Stokenet,
"account_tdx_2_128evrrwfp8gj9240qq0m06ukhwaj2cmejluxxreanzjwq62vmlf8r4"
"account_tdx_2_128evrrwfp8gj9240qq0m06ukhwaj2cmejluxxreanzjwq62vmlf8r4"
.parse()
.expect("Valid Dapp Def Address"),
"Dev Console".to_owned(),
ReferencesToAuthorizedPersonas::from_iter([
"Dev Console".to_owned(),
ReferencesToAuthorizedPersonas::from_iter([
AuthorizedPersonaSimple::sample_stokenet(),
AuthorizedPersonaSimple::sample_stokenet_other()
])
)
]),
AuthorizedDappPreferences::sample(),
)
}
pub fn sample_stokenet_sandbox() -> Self {
Self::new(
NetworkID::Stokenet,
"account_tdx_2_12yf9gd53yfep7a669fv2t3wm7nz9zeezwd04n02a433ker8vza6rhe"
"account_tdx_2_12yf9gd53yfep7a669fv2t3wm7nz9zeezwd04n02a433ker8vza6rhe"
.parse()
.expect("Valid Dapp Def Address"),
"Sandbox".to_owned(),
ReferencesToAuthorizedPersonas::from_iter([
"Sandbox".to_owned(),
ReferencesToAuthorizedPersonas::from_iter([
AuthorizedPersonaSimple::sample_stokenet_other()
])
)
]),
AuthorizedDappPreferences::sample(),
)
}

pub fn sample_mainnet() -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod authorized_dapp;
mod authorized_dapp_uniffi_fn;
mod authorized_persona_simple;
mod authorized_persona_simple_uniffi_fn;
mod preferences;
mod references_to_authorized_personas;
mod shared_persona_data;
mod shared_persona_data_uniffi_fn;
Expand All @@ -13,6 +14,7 @@ pub use authorized_dapp::*;
pub use authorized_dapp_uniffi_fn::*;
pub use authorized_persona_simple::*;
pub use authorized_persona_simple_uniffi_fn::*;
pub use preferences::*;
pub use references_to_authorized_personas::*;
pub use shared_persona_data::*;
pub use shared_persona_data_uniffi_fn::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::prelude::*;

/// Indicates whether the Wallet should show direct deposit claims for the given Dapp.
#[derive(
Serialize,
Deserialize,
FromRepr,
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
enum_iterator::Sequence,
derive_more::Display,
uniffi::Enum,
)]
#[serde(rename_all = "camelCase")]
pub enum AuthorizedDappPreferenceDeposits {
Hidden,
Visible,
}

impl Default for AuthorizedDappPreferenceDeposits {
fn default() -> Self {
Self::Visible
}
}

impl HasSampleValues for AuthorizedDappPreferenceDeposits {
fn sample() -> Self {
Self::Visible
}

fn sample_other() -> Self {
Self::Hidden
}
}

#[cfg(test)]
mod tests {
use super::*;

#[allow(clippy::upper_case_acronyms)]
type SUT = AuthorizedDappPreferenceDeposits;

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

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

#[test]
fn test_default() {
assert_eq!(SUT::Visible, SUT::default());
}

#[test]
fn json_roundtrip() {
assert_json_value_eq_after_roundtrip(&SUT::Visible, json!("visible"));
assert_json_roundtrip(&SUT::Visible);
}
}
Loading

0 comments on commit dda428b

Please sign in to comment.