Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic rate conversion #3

Merged
merged 35 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
21fb372
Add basic rate conversion
virtualdesigner Sep 2, 2023
ca44978
Make search independent of the text case,
virtualdesigner Sep 2, 2023
47b24c0
Get rid of duplicates between fiat and crypto rates
virtualdesigner Sep 2, 2023
5763e84
Rectify calculations,
virtualdesigner Sep 2, 2023
42aead9
Make the close button bolder,
virtualdesigner Sep 3, 2023
afa47ff
Auto-detect current tab
virtualdesigner Sep 3, 2023
3a61d4b
Limit decimal precision to 8 digits
virtualdesigner Sep 3, 2023
ee5b176
Reset existing amount to zero if deselected,
virtualdesigner Sep 3, 2023
9f3a33e
Remove unnecessary file
virtualdesigner Sep 3, 2023
3930d03
Remove unused style
virtualdesigner Sep 3, 2023
df2f329
Add installation and build instructions
virtualdesigner Sep 4, 2023
955a450
Change identifier
virtualdesigner Sep 4, 2023
7050723
Add more details on the readme for installing the prerequisites
virtualdesigner Sep 4, 2023
e9c45cd
Add github workflow scripts for build and release
virtualdesigner Sep 4, 2023
dd37796
Limit build targets to "msi", "app" & "dmg"
virtualdesigner Sep 4, 2023
4797ed0
Remove uploading .deb & binary files
virtualdesigner Sep 4, 2023
67f23c4
Add missing library to install during the build
virtualdesigner Sep 4, 2023
d0c48b7
Add missing dependency to install
virtualdesigner Sep 4, 2023
c8c9f19
Move currency total calculation and exchange calculation to Rust
virtualdesigner Sep 4, 2023
4f8a297
Downgrade to a stable version of Tauri,
virtualdesigner Sep 9, 2023
90d09ac
Change workflow build dependency
virtualdesigner Sep 9, 2023
e2888b4
Add type for children node
virtualdesigner Sep 11, 2023
923eadf
Fetch rates from Rust,
virtualdesigner Sep 14, 2023
8e6967c
Remove package in build workflow
virtualdesigner Sep 14, 2023
cedc566
Add missing package in build workflow
virtualdesigner Sep 14, 2023
3dbd7f9
Change config for build workflow
virtualdesigner Sep 14, 2023
0c6f0bc
Fix image bundle name
virtualdesigner Sep 15, 2023
45dfc1e
Change path for artifact
virtualdesigner Sep 15, 2023
1381c13
Use the rates data provided by Rust code
virtualdesigner Sep 16, 2023
6b2fa5d
Replace default icons
virtualdesigner Sep 18, 2023
ecf3fb4
Add URL strings to the top of the file
virtualdesigner Sep 18, 2023
f97821f
Use directories crate to store the rates
virtualdesigner Sep 23, 2023
ef96fd8
Bump actions/checkout package & use newer toolchain to install Rust
virtualdesigner Sep 23, 2023
a1db788
Refactor data fetch and storage
virtualdesigner Sep 24, 2023
0510b6a
Add test cases
virtualdesigner Sep 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
use std::collections::HashMap as Map;
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[derive(Debug, serde::Deserialize)]
struct Currency {
conversionRate: f32,
existingAmount: f32,
}
virtualdesigner marked this conversation as resolved.
Show resolved Hide resolved

#[tauri::command]
fn calculate_currency_total(selected_currencies: Map<String, Currency>) -> Map<String, f32> {
let currencies: Vec<(String, Currency)> = selected_currencies.into_iter().collect();
let mut total: Map<String, f32> = Map::new();

for (key, currency) in &currencies {
total.insert(key.clone(), currency.existingAmount);
}

for (key1, currency) in &currencies {
for (key2, other_currency) in &currencies {
if key1 != key2 {
let total_value = (total[key1])
+ (other_currency.existingAmount
* (currency.conversionRate / other_currency.conversionRate));
total.insert(key1.clone(), total_value);
}
}
}
virtualdesigner marked this conversation as resolved.
Show resolved Hide resolved

return total;
}

#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
fn calculate_exchange_rates(selected_currencies: Map<String, Currency>) -> Map<String, f32> {
let currencies: Vec<(String, Currency)> = selected_currencies.into_iter().collect();
let mut exchange: Map<String, f32> = Map::new();

for (key1, currency) in &currencies {
for (key2, other_currency) in &currencies {
if key1 != key2 {
exchange.insert(
format!("{key1}/{key2}"),
other_currency.conversionRate / currency.conversionRate,
);
}
}
}

return exchange;
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_window::init())
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![greet])
.invoke_handler(tauri::generate_handler![
calculate_currency_total,
calculate_exchange_rates
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
2 changes: 1 addition & 1 deletion src/context/CurrencyContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface ICurrencyList {
dict: ICurrency;
}

interface ICurrency {
export interface ICurrency {
[symbol: string]: {
conversionRate: number;
existingAmount?: number;
Expand Down
47 changes: 17 additions & 30 deletions src/pages/Summary.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useState } from "react";
import { useCurrencyContext } from "../context/CurrencyContext";
import { ICurrency, useCurrencyContext } from "../context/CurrencyContext";
import styled from "styled-components";
import Tabs from "../components/Tabs";
import { invoke } from "@tauri-apps/api/tauri";

const Wrapper = styled.div(() => ({
marginLeft: "50px",
Expand Down Expand Up @@ -50,36 +51,22 @@ const Summary = () => {
const { currencies } = useCurrencyContext();

useEffect(() => {
const selectedCurrencies = Object.keys(currencies.dict).filter(
(currencyCode) => currencies.dict[currencyCode].isSelected
);

const total: { [name: string]: number } = {};
const exchange: { [name: string]: number } = {};
for (let currency of selectedCurrencies) {
total[currency] = currencies.dict[currency].existingAmount || 0;
}
for (let currency of selectedCurrencies) {
for (let otherCurrency of selectedCurrencies) {
if (currency !== otherCurrency) {
total[currency] =
(total[currency] || 0) +
(currencies.dict[otherCurrency].existingAmount || 0) *
(currencies.dict[currency].conversionRate /
currencies.dict[otherCurrency].conversionRate);

exchange[`${currency}/${otherCurrency}`] =
currencies.dict[otherCurrency].conversionRate /
currencies.dict[currency].conversionRate;
exchange[`${otherCurrency}/${currency}`] =
currencies.dict[currency].conversionRate /
currencies.dict[otherCurrency].conversionRate;
const selectedCurrencies: ICurrency = {};
Object.keys(currencies.dict).map(
(currencyCode) => {
if (currencies.dict[currencyCode].isSelected) {
selectedCurrencies[currencyCode] = currencies.dict[currencyCode];
selectedCurrencies[currencyCode].existingAmount = selectedCurrencies[currencyCode].existingAmount || 0;
}
}
}

setTotalList({ ...total });
setExchangeList({ ...exchange });
});

invoke<{[key: string]: number}>("calculate_currency_total", { selectedCurrencies }).then(total => {
setTotalList({...total});
}).catch(err => console.error("Problem with calculating total: ", err));

invoke<{[key: string]: number}>("calculate_exchange_rates", { selectedCurrencies }).then(exchange => {
setExchangeList({ ...exchange });
}).catch(err => console.error("Problem with calculating exchange rates: ", err));
}, []);

return (
Expand Down