Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Sominemo committed Mar 20, 2022
1 parent a0b2711 commit d1bd76d
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 249 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.4.5
- Minor refactoring

## 1.4.4
- Added support for new API fields

Expand Down
27 changes: 14 additions & 13 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Defines a default set of lint rules enforced for
# projects at Google. For details and rationale,
# see https://github.com/dart-lang/pedantic#enabled-lints.
include: package:pedantic/analysis_options.yaml

# For lint rules and documentation, see http://dart-lang.github.io/linter/lints.
# Uncomment to specify additional rules.
# linter:
# rules:
# - camel_case_types

include: package:lints/recommended.yaml
analyzer:
# exclude:
# - path/to/excluded/files/**
strong-mode:
implicit-casts: false
implicit-dynamic: false
language:
strict-raw-types: true
linter:
rules:
always_declare_return_types: true
prefer_single_quotes: true
unawaited_futures: true
package_api_docs: true
prefer_final_in_for_each: true
prefer_final_locals: true
23 changes: 12 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import 'package:monobank_api/currency/extensions/currency_names.dart';

void statement() async {
// Create client
var client = MonoAPI('token');
final client = MonoAPI('token');

// Request client
var res = await client.clientInfo();
final res = await client.clientInfo();

// Get first account
var account = res.accounts[0];
final account = res.accounts[0];

// Get statement list for last 3 months
var statement = account.statement(
final statement = account.statement(
DateTime.now().subtract(Duration(days: 31 * 3)), DateTime.now());

// For each statement item
await for (var item in statement.list(reverse: true)) {
await for (final item in statement.list(reverse: true)) {
// Output string representation
print('${item.mcc.emoji} $item (${item.operationAmount.currency.name})');
}
Expand All @@ -38,7 +38,7 @@ void money() {
Money(336, Currency.dummy));

// Money implements Comparable
var m = [
final m = [
Money(20, Currency.dummy),
Money(-5, Currency.dummy),
Money(3, Currency.dummy)
Expand All @@ -50,7 +50,7 @@ void money() {
assert(m[2] == Money(20, Currency.dummy));

// Convert currencies
var converter = CurrencyInfo(
final converter = CurrencyInfo(
Currency.code('EUR'),
Currency.code('USD'),
1.165,
Expand All @@ -64,16 +64,17 @@ void money() {

void currency() async {
// Creating client
var client = MonoAnonymousAPI();
final client = MonoAnonymousAPI();

// Getting currencies
var cur = await client.currency();
final cur = await client.currency();

// Looking for RUB exchanger
var currencyInfo = cur.firstWhere((e) => e.currencyA == Currency.code('RUB'));
final currencyInfo =
cur.firstWhere((e) => e.currencyA == Currency.code('RUB'));

// Exchanging 100 UAHs to RUB
var result = currencyInfo.exchange(Money(10000, Currency.code('UAH')));
final result = currencyInfo.exchange(Money(10000, Currency.code('UAH')));

// Printing
print(result);
Expand Down
1 change: 1 addition & 0 deletions lib/data/currency/currency_countries_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///
/// {@category Datasets}
/// {@subCategory Currency}
// ignore_for_file: constant_identifier_names, implicit_dynamic_map_literal
library currency_countries_dataset;

const Map<String, List<String>> Iso4217Countries = {
Expand Down
1 change: 1 addition & 0 deletions lib/data/currency/currency_names_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///
/// {@category Datasets}
/// {@subCategory Currency}
// ignore_for_file: constant_identifier_names, implicit_dynamic_map_literal
library currency_names_dataset;

const Map<String, String> Iso4217Names = {
Expand Down
2 changes: 2 additions & 0 deletions lib/data/currency/iso4217_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
///
/// {@category Datasets}
/// {@subCategory Currency}
// ignore_for_file: constant_identifier_names, implicit_dynamic_map_literal

library iso4217_dataset;

const List<Map<String, dynamic>> Iso4217 = [
Expand Down
1 change: 1 addition & 0 deletions lib/data/mcc/mcc_emoji_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///
/// {@category Datasets}
/// {@subCategory MCC}
// ignore_for_file: constant_identifier_names, implicit_dynamic_map_literal
library mcc_emoji_dataset;

const Map<String, List<int>> MCCEmojiDataset = {
Expand Down
1 change: 1 addition & 0 deletions lib/data/mcc/mcc_english_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///
/// {@category Datasets}
/// {@subCategory MCC}
// ignore_for_file: constant_identifier_names, implicit_dynamic_map_literal
library mcc_english_dataset;

const Map<String, String> MCCEnglishDataset = {
Expand Down
1 change: 1 addition & 0 deletions lib/data/mcc/mcc_russian_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///
/// {@category Datasets}
/// {@subCategory MCC}
// ignore_for_file: constant_identifier_names, implicit_dynamic_map_literal
library mcc_russian_dataset;

const Map<String, String> MCCRussianDataset = {
Expand Down
1 change: 1 addition & 0 deletions lib/data/mcc/mcc_ukrainian_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///
/// {@category Datasets}
/// {@subCategory MCC}
// ignore_for_file: constant_identifier_names, implicit_dynamic_map_literal
library mcc_ukrainian_dataset;

const Map<String, String> MCCUkrainianDataset = {
Expand Down
1 change: 1 addition & 0 deletions lib/data/mcc/mcc_visuals_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///
/// {@category Datasets}
/// {@subCategory MCC}
// ignore_for_file: constant_identifier_names, implicit_dynamic_map_literal
library mcc__dataset;

const Map<String, Map<String, String>> MCCVisualsDataset = {
Expand Down
7 changes: 5 additions & 2 deletions lib/src/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ enum APIHttpMethod {
/// Send as GET
///
/// NOTICE: It doesn't support body
// ignore: constant_identifier_names
GET,

/// Send as POST
// ignore: constant_identifier_names
POST
}

Expand Down Expand Up @@ -263,6 +265,7 @@ class API {
Map<String, Duration> requestTimeouts = const {},
Uri? noAuthDomain,
}) : noAuthDomain = noAuthDomain ?? domain {
// ignore: prefer_initializing_formals
this.requestTimeouts = requestTimeouts;
}

Expand Down Expand Up @@ -451,7 +454,7 @@ class API {

Duration? minDelay;

for (var request in _cart.toList()) {
for (final request in _cart.toList()) {
try {
globalWait = willFreeIn();
if (request.methodId != null) {
Expand Down Expand Up @@ -504,7 +507,7 @@ class API {

try {
request._isProcessingNeeded = false;
var inLine = (request.settings & APIFlags.skip == 0) &&
final inLine = (request.settings & APIFlags.skip == 0) &&
(request.settings & APIFlags.skipGlobal == 0);
_lastRequest = DateTime.now();

Expand Down
47 changes: 29 additions & 18 deletions lib/src/money.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:math';

import '../data/currency/iso4217_dataset.dart';

const Map<String, dynamic> _CurrencyStyling = {
const Map<String, String> _currencyStyling = {
'UAH': '\u20B4',
'RUB': '\u20bd',
'USD': '\u0024',
Expand Down Expand Up @@ -51,11 +51,15 @@ class Currency {
) {
final upperCode = code.toUpperCase();
final info = Iso4217.firstWhere((currency) => currency['code'] == upperCode,
orElse: () => {});
orElse: () => <String, dynamic>{});

if (!info.containsKey('code')) return UnknownCurrency(code);

return Currency(info['code'], info['number'], info['digits']);
return Currency(
info['code'] as String,
info['number'] as int,
info['digits'] as int,
);
}

/// Find currency by ISO-4217 currency number
Expand All @@ -66,11 +70,15 @@ class Currency {
int number,
) {
final info = Iso4217.firstWhere((currency) => currency['number'] == number,
orElse: () => {});
orElse: () => <String, dynamic>{});

if (!info.containsKey('code')) return UnknownCurrency(number.toString());

return Currency(info['code'], info['number'], info['digits']);
return Currency(
info['code'] as String,
info['number'] as int,
info['digits'] as int,
);
}

/// Dummy currency
Expand Down Expand Up @@ -122,14 +130,19 @@ class UnknownCurrency extends Currency {
/// hashCode of an unknown currency is always `0`
@override
int get hashCode => 0;

@override
bool operator ==(dynamic other) {
return identical(this, other);
}
}

/// Represents money
///
/// Supports `+`, `-`, `/`, `*`, `%` and all comparison operators, but works only
/// with instances of the same currency. See [CurrencyInfo] to exchange
/// currencies by rate
class Money implements Comparable {
class Money implements Comparable<Money> {
/// Constructs new money instance
///
/// The money constructor is constant
Expand Down Expand Up @@ -172,8 +185,8 @@ class Money implements Comparable {
@override
String toString() =>
'${toNumericString()} ' +
(fancyCurrencies && _CurrencyStyling.containsKey(currency.code)
? _CurrencyStyling[currency.code]
(fancyCurrencies && _currencyStyling.containsKey(currency.code)
? _currencyStyling[currency.code]!
: currency.code);

/// Returns `true` if the amount is `0`
Expand All @@ -184,7 +197,8 @@ class Money implements Comparable {

/// Returns new Money instance of the same currency with
/// absolute value of amount
factory Money.abs(target) => Money(target.amount.abs(), target.currency);
factory Money.abs(Money target) =>
Money(target.amount.abs(), target.currency);

/// Converts double to integer which represents amount of the instance
/// in the smallest unit of the currency
Expand Down Expand Up @@ -215,11 +229,8 @@ class Money implements Comparable {

@override
int compareTo(other) {
if (other is! Money) throw Exception('Money can be compared only to Money');
final second = other;

if (this > second) return 1;
if (this < second) return -1;
if (this > other) return 1;
if (this < other) return -1;
return 0;
}

Expand Down Expand Up @@ -327,9 +338,9 @@ class Money implements Comparable {
) {
if (items.isEmpty) throw Exception('Minimal 1 item expected');
var m = items[0];
items.forEach((e) {
for (final e in items) {
if (e < m) m = e;
});
}
return m;
}

Expand All @@ -344,9 +355,9 @@ class Money implements Comparable {
) {
if (items.isEmpty) throw Exception('Minimal 1 item expected');
var m = items[0];
items.forEach((e) {
for (final e in items) {
if (e > m) m = e;
});
}
return m;
}
}
Expand Down
Loading

0 comments on commit d1bd76d

Please sign in to comment.