Skip to content

Commit

Permalink
feat: improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
santitigaga committed Feb 6, 2025
1 parent 55b61ab commit 50bc599
Show file tree
Hide file tree
Showing 18 changed files with 455 additions and 189 deletions.
25 changes: 18 additions & 7 deletions examples/app_async_flutter/lib/application/app_config.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:app_async_flutter/infraestructure/notifier/log_notifier.dart';
import 'package:app_async_flutter/infrastructure/notifier/log_notifier.dart';
import 'package:flutter/material.dart';

class AppConfig extends InheritedWidget {
const AppConfig({
AppConfig({
Key? key,
required this.businessUrl,
required this.socketUrl,
Expand All @@ -12,15 +12,26 @@ class AppConfig extends InheritedWidget {
required Widget child,
}) : super(key: key, child: child);

final String businessUrl;
final String socketUrl;
final int heartbeatInterval;
final int maxRetries;
final LogNotifier logNotifier;
String businessUrl;
String socketUrl;
int heartbeatInterval;
int maxRetries;
LogNotifier logNotifier;

static AppConfig of(BuildContext context) =>
context.findAncestorWidgetOfExactType<AppConfig>()!;

@override
bool updateShouldNotify(InheritedWidget oldWidget) => false;

void updateConfig(
{required int heartbeatInterval,
required int maxRetries,
required String socketUrl,
required String businessUrl}) {
this.heartbeatInterval = heartbeatInterval;
this.maxRetries = maxRetries;
this.socketUrl = socketUrl;
this.businessUrl = businessUrl;
}
}
53 changes: 46 additions & 7 deletions examples/app_async_flutter/lib/async_client_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'package:app_async_flutter/domain/model/channel_credentials.dart';
import 'package:app_async_flutter/domain/model/gateway/async_client_gateway.dart';
import 'package:channel_sender_client/channel_sender_client.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';

class ResponsesNotifier extends ChangeNotifier {
List<String> responses = [];
Expand Down Expand Up @@ -33,33 +35,70 @@ class AsyncClientService extends InheritedWidget {
final String eventListen;
late AsyncClient asyncClient;
final AsyncClientGateway asyncClientGateway;
final _log = Logger('AsyncClientService');

late SharedPreferences prefs;
ResponsesNotifier responsesNotifier = ResponsesNotifier();
final AppConfig appConfig;

void _handleEvent(dynamic result) {
responsesNotifier.addResponse(
"Message from async dataflow, payload: ${result.payload} correlationId: ${result.correlationId}");
void _handleEvent(dynamic msg) {
if (msg.event == 'businessEvent') {
responsesNotifier.addResponse(
'Message from async dataflow, title: ${msg.payload['title']} detail: ${msg.payload['detail']}');
}

if (msg.event == 'ch-ms-async-callback.svp.reply') {
responsesNotifier.addResponse(
'Message from async dataflow, title: ${msg.payload['data']['reply']['messageData']['title']} detail: ${msg.payload['data']['reply']['messageData']['detail']}');
}
}

static AsyncClientService? of(BuildContext context) {
return context.findAncestorWidgetOfExactType<AsyncClientService>();
}

void closeSession() async {
Future<void> closeSession() async {
prefs = await SharedPreferences.getInstance();
await deleteChannelCreated();
asyncClient.disconnect();
}

Future<void> deleteChannelCreated() async {
await prefs.remove('channelRef');
await prefs.remove('channelSecret');
await prefs.remove('userRef');
}

bool hasUserRef() {
return prefs.getString('userRef') != null;
}

Future<String> createUserRef() async {
if (hasUserRef()) {
return prefs.getString('userRef')!;
}
var uuid = const Uuid();
String ref = uuid.v4();
await prefs.setString('userRef', ref);
return ref;
}

Future<void> saveConfig() async {
prefs = await SharedPreferences.getInstance();

await prefs.setString('socketUrl', appConfig.socketUrl);
await prefs.setString('apiBusiness', appConfig.businessUrl);
await prefs.setString(
'heartbeatInterval', appConfig.heartbeatInterval.toString());
await prefs.setString('maxRetries', appConfig.maxRetries.toString());
}

Future<void> initAsyncClient() async {
prefs = await SharedPreferences.getInstance();
ChannelCredential? channelCredential = await _requestChannelCredentials();
var userRef = await createUserRef();
_log.info("userRef $userRef");
ChannelCredential? channelCredential =
await _requestChannelCredentials(userRef);
if (channelCredential != null) {
final conf = AsyncConfig(
socketUrl: appConfig.socketUrl,
Expand All @@ -80,12 +119,12 @@ class AsyncClientService extends InheritedWidget {
}
}

Future<ChannelCredential?> _requestChannelCredentials() async {
Future<ChannelCredential?> _requestChannelCredentials(String userRef) async {
ChannelCredential? channelCredential;
if (hasChannelCreated()) {
return getChannelCreated();
}
channelCredential = await asyncClientGateway.getCredentials();
channelCredential = await asyncClientGateway.getCredentials(userRef);
print(channelCredential!.channelRef);
persistCredentials(channelCredential);
return channelCredential;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:app_async_flutter/domain/model/channel_credentials.dart';

abstract class AsyncClientGateway {
Future<ChannelCredential?> getCredentials();
Future<void> callBusinessUseCase(String channelRef, int delay);
Future<ChannelCredential?> getCredentials(String userRef);
Future<void> callBusinessUseCase(
String channelRef, String userRef, int delay);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class ApiService implements AsyncClientGateway {
urlBusinessService = AppConfig.of(context).businessUrl;
}
@override
Future<ChannelCredential?> getCredentials() async {
Future<ChannelCredential?> getCredentials(String userRef) async {
ChannelCredential? channelCredential;
return http
.get(Uri.parse("$urlBusinessService/credentials"))
.get(Uri.parse("$urlBusinessService/credentials?user_ref=$userRef"))
.then((response) {
try {
print("response.body ${response.body}");
Expand All @@ -31,8 +31,8 @@ class ApiService implements AsyncClientGateway {

@override
Future<http.Response> callBusinessUseCase(
String channelRef, int delay) async {
String channelRef, String userRef, int delay) async {
return http.get(Uri.parse(
"$urlBusinessService/business?channel_ref=$channelRef&delay=$delay"));
"$urlBusinessService/business?channel_ref=$channelRef&user_ref=$userRef&delay=$delay"));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import 'package:flutter/material.dart';

class LogNotifier extends ChangeNotifier {
LogLevel level = LogLevel.all;

List<String> logs = [];
void setLog(log) {
logs.insert(0, log);
notifyListeners();
}

void setLevel(newLevel) {
level = newLevel;
notifyListeners();
}

void clean() {
logs.clear();
notifyListeners();
Expand All @@ -16,3 +23,5 @@ class LogNotifier extends ChangeNotifier {
return logs;
}
}

enum LogLevel { info, all }
5 changes: 4 additions & 1 deletion examples/app_async_flutter/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'package:app_async_flutter/setup.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async {
await dotenv.load(fileName: ".env");
WidgetsFlutterBinding.ensureInitialized();
runApp(Setup.getApp());
SharedPreferences prefs = await SharedPreferences.getInstance();

runApp(Setup.getApp(prefs));
}
4 changes: 2 additions & 2 deletions examples/app_async_flutter/lib/my_app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:app_async_flutter/application/app_config.dart';
import 'package:app_async_flutter/async_client_service.dart';
import 'package:app_async_flutter/infraestructure/driven_adapter/api_service.dart';
import 'package:app_async_flutter/infrastructure/driven_adapter/api_service.dart';
import 'package:app_async_flutter/ui/pages/home_page.dart';
import 'package:flutter/material.dart';

Expand All @@ -11,7 +11,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AsyncClientService(
eventListen: "businessEvent",
eventListen: "ch-ms-async-callback.svp.reply",
asyncClientGateway: ApiService(context),
appConfig: AppConfig.of(context),
child: MaterialApp(
Expand Down
49 changes: 37 additions & 12 deletions examples/app_async_flutter/lib/setup.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
import 'package:app_async_flutter/application/app_config.dart';
import 'package:app_async_flutter/infraestructure/notifier/log_notifier.dart';
import 'package:app_async_flutter/infrastructure/notifier/log_notifier.dart';
import 'package:app_async_flutter/my_app.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Setup {
static AppConfig getApp() {
static AppConfig getApp(SharedPreferences prefs) {
var env = dotenv.env;
Logger.root.level = Level.INFO;
LogNotifier logNotifier = configureLogger();
prefs.getString('apiBusiness');
return AppConfig(
businessUrl: getEnvironment(prefs, 'apiBusiness'),
heartbeatInterval: int.parse(getEnvironment(prefs, 'heartbeatInterval')),
maxRetries: int.parse(getEnvironment(prefs, 'maxRetries')),
socketUrl: getEnvironment(prefs, 'socketUrl'),
logNotifier: logNotifier,
child: const MyApp(),
);
}

static String getEnvironment(
SharedPreferences prefs,
String key,
) {
var businessUrl = prefs.getString(key);
if (businessUrl == null) {
businessUrl = dotenv.env[key]!;
prefs.setString(key, businessUrl);
}
print(businessUrl);

return businessUrl;
}

static LogNotifier configureLogger() {
LogNotifier logNotifier = LogNotifier();
logNotifier.addListener(() {
Logger.root.level =
logNotifier.level == LogLevel.all ? Level.FINEST : Level.INFO;
});

Logger.root.onRecord.listen((record) {
var log = '${record.level.name}: ${record.time}: ${record.message}';
logNotifier.setLog(log);
print(log);
// debugPrint(log);
});
return AppConfig(
businessUrl: env['apiBusiness'] ?? 'http://localhost:8080/api',
heartbeatInterval: int.parse(env['heartbeatInterval'] ?? '2500'),
maxRetries: int.parse(env['maxRetries'] ?? '15'),
socketUrl: dotenv.env['socketUrl'] ?? 'ws://localhost:8082/ext/socket',
logNotifier: logNotifier,
child: const MyApp(),
);
return logNotifier;
}
}
23 changes: 0 additions & 23 deletions examples/app_async_flutter/lib/ui/atoms/delay_field.dart

This file was deleted.

29 changes: 29 additions & 0 deletions examples/app_async_flutter/lib/ui/atoms/input_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';

class InputField extends StatelessWidget {
const InputField(
{Key? key,
required this.textEditingController,
this.keyboardType = TextInputType.number,
required this.labelText,
this.icon})
: super(key: key);

final TextEditingController textEditingController;
final TextInputType? keyboardType;
final String labelText;
final IconData? icon;

@override
Widget build(BuildContext context) {
return TextField(
controller: textEditingController,
keyboardType: keyboardType,
decoration: InputDecoration(
border: const UnderlineInputBorder(),
icon: Icon(icon),
labelText: labelText,
),
);
}
}
15 changes: 7 additions & 8 deletions examples/app_async_flutter/lib/ui/helpers/home_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@ import 'package:app_async_flutter/async_client_service.dart';
import 'package:flutter/material.dart';

class HomeHelper {
late final AsyncClientService asyncClientService;
HomeHelper(BuildContext context) {
asyncClientService = AsyncClientService.of(context)!;
}
final AsyncClientService asyncClientService;
HomeHelper(BuildContext context, this.asyncClientService);
void callAsyncBackend(textEditingController) {
int start = DateTime.now().millisecondsSinceEpoch;

asyncClientService.asyncClientGateway
.callBusinessUseCase(
asyncClientService.prefs.getString("channelRef") ?? "",
asyncClientService.prefs.getString("userRef") ?? "",
int.tryParse(textEditingController.text) ?? 100)
.then((value) => asyncClientService.responsesNotifier.addResponse(
"Get empty response after ${DateTime.now().millisecondsSinceEpoch - start} ms"));
}

void disconnect() {
asyncClientService.closeSession();
Future<void> disconnect() async {
await asyncClientService.closeSession();
}

void connect() {
asyncClientService.initAsyncClient();
Future<void> connect() async {
await asyncClientService.initAsyncClient();
}
}
Loading

0 comments on commit 50bc599

Please sign in to comment.