mirror of
https://github.com/hmalik144/easy_cc_flutter.git
synced 2025-12-10 03:05:34 +00:00
- Homescreen widget for android added
This commit is contained in:
@@ -2,6 +2,9 @@ extension CurrencyExtension on String {
|
||||
/// Convert currency string into currency code
|
||||
/// eg. "AUD - Australian Dollar" to "AUD"
|
||||
String getCurrencyCode(){
|
||||
if (length == 3) {
|
||||
return this;
|
||||
}
|
||||
return substring(0,3);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@ import 'package:flutter/material.dart';
|
||||
import 'package:stacked/stacked.dart';
|
||||
import 'package:toast/toast.dart';
|
||||
|
||||
import 'base_viewmodel.dart';
|
||||
import 'Utils/constants.dart';
|
||||
import 'Utils/view_state.dart';
|
||||
import 'Utils/view_utils.dart';
|
||||
import 'base_viewmodel.dart';
|
||||
|
||||
abstract class BaseStatelessWidget<T extends BaseViewmodel>
|
||||
extends StatelessWidget {
|
||||
|
||||
@@ -5,8 +5,8 @@ class AppDio {
|
||||
static Dio createDio() {
|
||||
Dio dio = Dio(
|
||||
BaseOptions(
|
||||
receiveTimeout: 60000,
|
||||
connectTimeout: 120000,
|
||||
receiveTimeout: 30000,
|
||||
connectTimeout: 30000,
|
||||
)
|
||||
);
|
||||
dio.interceptors.add(LogInterceptor());
|
||||
|
||||
@@ -4,7 +4,6 @@ import 'package:easy_cc_flutter/Utils/currency_utils.dart';
|
||||
import 'package:easy_cc_flutter/data/model/currency.dart';
|
||||
import 'package:easy_cc_flutter/data/prefs/currency_pair.dart';
|
||||
|
||||
import '../../locator.dart';
|
||||
import '../../main.dart';
|
||||
import '../network/backup_currency_api.dart';
|
||||
import '../network/currency_api.dart';
|
||||
@@ -13,9 +12,11 @@ import '../prefs/preference_provider.dart';
|
||||
import 'repository.dart';
|
||||
|
||||
class RepositoryImpl extends Repository with SafeApiCall {
|
||||
final PreferenceProvider _prefs = locator<PreferenceProvider>();
|
||||
final CurrencyApi _api = locator<CurrencyApi>();
|
||||
final BackupCurrencyApi _backupApi = locator<BackupCurrencyApi>();
|
||||
final PreferenceProvider _prefs;
|
||||
final CurrencyApi _api;
|
||||
final BackupCurrencyApi _backupApi;
|
||||
|
||||
RepositoryImpl(this._prefs, this._api, this._backupApi);
|
||||
|
||||
@override
|
||||
CurrencyPair getConversionPair() {
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
import 'package:easy_cc_flutter/main_view_model.dart';
|
||||
import 'package:easy_cc_flutter/Utils/selection_type.dart';
|
||||
import 'package:easy_cc_flutter/views/drop_down_box.dart';
|
||||
import 'package:easy_cc_flutter/locator.dart';
|
||||
import 'package:easy_cc_flutter/main_view_model.dart';
|
||||
import 'package:easy_cc_flutter/views/converter_edit_text.dart';
|
||||
import 'package:easy_cc_flutter/views/drop_down_box.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'base_widget.dart';
|
||||
import 'Utils/constants.dart';
|
||||
import 'base_widget.dart';
|
||||
|
||||
class HomePage extends BaseStatelessWidget<MainViewModel> {
|
||||
const HomePage({super.key});
|
||||
|
||||
@override
|
||||
MainViewModel createViewModel() {
|
||||
return MainViewModel();
|
||||
return locator<MainViewModel>();
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import 'package:easy_cc_flutter/main_view_model.dart';
|
||||
import 'package:easy_cc_flutter/data/network/backup_currency_api.dart';
|
||||
import 'package:easy_cc_flutter/data/network/currency_api.dart';
|
||||
import 'package:easy_cc_flutter/data/repository/repository_impl.dart';
|
||||
import 'package:easy_cc_flutter/main_view_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
import 'data/prefs/preference_provider.dart';
|
||||
import 'data/repository/repository.dart';
|
||||
|
||||
GetIt locator = GetIt.instance;
|
||||
|
||||
@@ -12,6 +12,6 @@ void setupLocator() {
|
||||
locator.registerLazySingleton(() => PreferenceProvider());
|
||||
locator.registerLazySingleton(() => CurrencyApi.create());
|
||||
locator.registerLazySingleton(() => BackupCurrencyApi.create());
|
||||
locator.registerLazySingleton(() => RepositoryImpl());
|
||||
locator.registerFactory(() => MainViewModel());
|
||||
locator.registerLazySingleton<Repository>(() => RepositoryImpl(locator<PreferenceProvider>(), locator<CurrencyApi>(),locator<BackupCurrencyApi>()));
|
||||
locator.registerFactory(() => MainViewModel(locator<Repository>()));
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
import 'package:easy_cc_flutter/Utils/currency_utils.dart';
|
||||
import 'package:easy_cc_flutter/data/network/backup_currency_api.dart';
|
||||
import 'package:easy_cc_flutter/data/network/currency_api.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:home_widget/home_widget.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
import 'data/model/currency.dart';
|
||||
import 'data/prefs/preference_provider.dart';
|
||||
import 'data/repository/repository.dart';
|
||||
import 'data/repository/repository_impl.dart';
|
||||
import 'home.dart';
|
||||
import 'locator.dart';
|
||||
|
||||
@@ -10,34 +16,56 @@ var logger = Logger(
|
||||
printer: PrettyPrinter(),
|
||||
);
|
||||
|
||||
void main() async {
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
setupLocator();
|
||||
await locator<PreferenceProvider>().init();
|
||||
await HomeWidget.registerBackgroundCallback(backgroundCallback);
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
Future<void> backgroundCallback(Uri? uri) async {
|
||||
if (uri?.host == 'updatecounter') {
|
||||
PreferenceProvider prefs = PreferenceProvider();
|
||||
await prefs.init();
|
||||
CurrencyApi api = CurrencyApi.create();
|
||||
BackupCurrencyApi backupApi = BackupCurrencyApi.create();
|
||||
RepositoryImpl repository = RepositoryImpl(prefs, api, backupApi);
|
||||
|
||||
if (uri?.host == 'updatewidget') {
|
||||
Map<String, String>? querys = uri?.queryParameters;
|
||||
String? widgetId = querys?["id"];
|
||||
|
||||
int _counter = 0;
|
||||
await HomeWidget.getWidgetData<int>('_counter', defaultValue: 0).then((int? value) {
|
||||
_counter = value ?? 0;
|
||||
_counter++;
|
||||
});
|
||||
await HomeWidget.saveWidgetData<int>('_counter', _counter);
|
||||
await HomeWidget.updateWidget(name: 'AppWidgetProvider', iOSName: 'AppWidgetProvider');
|
||||
} else if (uri?.host == 'createWidget') {
|
||||
await updateWidget(widgetId, repository);
|
||||
} else if (uri?.host == 'createwidget') {
|
||||
Map<String, String>? querys = uri?.queryParameters;
|
||||
String? id = querys?["id"];
|
||||
String? from = querys?["from"];
|
||||
String? to = querys?["to"];
|
||||
String? widgetId = querys?["id"];
|
||||
String? from = querys?["from"]?.getCurrencyCode();
|
||||
String? to = querys?["to"]?.getCurrencyCode();
|
||||
|
||||
await HomeWidget.saveWidgetData<String>("${widgetId}_from", from);
|
||||
await HomeWidget.saveWidgetData<String>("${widgetId}_to", to);
|
||||
|
||||
await updateWidget(widgetId, repository);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateWidget(String? widgetId, Repository repository) async {
|
||||
String? from = await HomeWidget.getWidgetData<String>("${widgetId}_from");
|
||||
String? to = await HomeWidget.getWidgetData<String>("${widgetId}_to");
|
||||
|
||||
if (from == null || to == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Currency currency = await repository.getConversationRateFromApi(from, to);
|
||||
|
||||
await HomeWidget.saveWidgetData<String>("${widgetId}_from", from);
|
||||
await HomeWidget.saveWidgetData<String>("${widgetId}_to", to);
|
||||
await HomeWidget.saveWidgetData<String>("${widgetId}_rate", currency.rate.toString());
|
||||
|
||||
await HomeWidget.updateWidget(name: 'AppWidgetProvider', iOSName: 'AppWidgetProvider');
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@@ -58,7 +86,7 @@ class MyApp extends StatelessWidget {
|
||||
// is not restarted.
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const HomePage(),
|
||||
home: HomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import 'package:easy_cc_flutter/base_viewmodel.dart';
|
||||
import 'package:easy_cc_flutter/Utils/selection_type.dart';
|
||||
import 'package:easy_cc_flutter/base_viewmodel.dart';
|
||||
|
||||
import 'Utils/constants.dart';
|
||||
import 'data/prefs/currency_pair.dart';
|
||||
import 'data/repository/repository.dart';
|
||||
import 'data/repository/repository_impl.dart';
|
||||
import 'locator.dart';
|
||||
|
||||
class MainViewModel extends BaseViewmodel {
|
||||
final Repository _repository = locator<RepositoryImpl>();
|
||||
final Repository _repository;
|
||||
|
||||
MainViewModel(this._repository);
|
||||
|
||||
double conversionRate = 1.0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user