84 lines
2.7 KiB
Rust
84 lines
2.7 KiB
Rust
|
||
//! `FeeBandPreview` — M-Pesa fee band preview card.
|
||
|
||
use makepad_widgets::*;
|
||
use robius_ussd::{TransactionKind, mpesa_band_for_kind_amount, mpesa_fee_for_kind_amount};
|
||
|
||
#[derive(Script, ScriptHook, Widget)]
|
||
pub struct FeeBandPreview {
|
||
#[deref]
|
||
view: View,
|
||
}
|
||
|
||
impl Widget for FeeBandPreview {
|
||
fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
|
||
self.view.handle_event(cx, event, scope);
|
||
}
|
||
fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep {
|
||
self.view.draw_walk(cx, scope, walk)
|
||
}
|
||
}
|
||
|
||
impl FeeBandPreviewRef {
|
||
/// Updates the band + fee labels for the given kind + amount.
|
||
pub fn update(&self, cx: &mut Cx, kind: TransactionKind, amount: u64) {
|
||
if let Some(inner) = self.borrow() {
|
||
let band_text = match mpesa_band_for_kind_amount(kind, amount) {
|
||
Some(b) => {
|
||
let fee = b.fee;
|
||
if fee == 0 {
|
||
format!("Band: KSh {}–{} → Fee: Free", b.min, b.max)
|
||
} else {
|
||
format!("Band: KSh {}–{} → Fee: KSh {}", b.min, b.max, fee)
|
||
}
|
||
}
|
||
None => {
|
||
if amount == 0 {
|
||
"Band: — → Fee: — (enter amount)".to_string()
|
||
} else {
|
||
format!("KSh {} outside supported range", amount)
|
||
}
|
||
}
|
||
};
|
||
inner.view.label(cx, ids!(band_label)).set_text(cx, &band_text);
|
||
|
||
let fee_text = match mpesa_fee_for_kind_amount(kind, amount) {
|
||
Some(0) => "Free".to_string(),
|
||
Some(f) => format!("KSh {}", f),
|
||
None => "—".to_string(),
|
||
};
|
||
inner.view.label(cx, ids!(fee_label)).set_text(cx, &fee_text);
|
||
}
|
||
}
|
||
}
|
||
|
||
script_mod! {
|
||
use mod.prelude.widgets.*
|
||
use mod.widgets.*
|
||
|
||
mod.widgets.FeeBandPreview = #(FeeBandPreview::register_widget(vm)) {
|
||
..mod.widgets.RoundedView
|
||
width: Fill, height: Fit
|
||
flow: Down, spacing: 4
|
||
padding: 12
|
||
show_bg: true
|
||
draw_bg +: {
|
||
color: #xFFFFFF12
|
||
border_radius: 12.0
|
||
border_size: 1.0
|
||
border_color: #xFFFFFF20
|
||
}
|
||
band_label := Label {
|
||
width: Fill
|
||
text: "Band: — → Fee: — (enter amount)"
|
||
draw_text +: { color: #xB7B7B7, text_style: theme.font_regular { font_size: 11.0 } }
|
||
}
|
||
fee_label := Label {
|
||
width: Fit
|
||
text: "—"
|
||
draw_text +: { color: #x138808, text_style: theme.font_bold { font_size: 16.0 } }
|
||
}
|
||
}
|
||
}
|
||
|
||
|