lune 0.0.3
lune: ^0.0.3 copied to clipboard
Flutter wrapper for native Lune SDK: Add native financial data visualization to your Android & iOS apps.
example/lib/main.dart
import 'dart:async';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:lune/lune.dart';
import 'package:lune/view_types.dart';
import 'package:lune_example/auth/actions.dart';
import 'package:lune_example/auth/config.dart';
import 'package:lune_example/auth/results.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
try {
await dotenv.load(fileName: ".env");
} catch (e) {
log("Failed to load .env file: $e");
}
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
LuneAuthResult authResult = LuneAuthResult();
late LuneAuthConfiguration config;
Lune? _lunePlugin;
String? _errorMessage;
@override
void initState() {
super.initState();
initLuneSDK();
}
Future<void> initLuneSDK() async {
try {
// Validate required environment variables
final requiredVars = [
'LUNE_BASE_URL',
'LUNE_USERNAME',
'LUNE_PASSWORD',
'LUNE_CUSTOMER_ID',
];
final missingVars = requiredVars.where((varName) {
final value = dotenv.env[varName];
return value == null || value.isEmpty;
}).toList();
if (missingVars.isNotEmpty) {
throw Exception(
'Missing required environment variables: ${missingVars.join(", ")}',
);
}
config = LuneAuthConfiguration(
baseURL: dotenv.env['LUNE_BASE_URL']!,
username: dotenv.env['LUNE_USERNAME']!,
password: dotenv.env['LUNE_PASSWORD']!,
customerID: dotenv.env['LUNE_CUSTOMER_ID']!,
);
authResult = await getToken(
config: config,
);
if (authResult.errorMessage != null) {
throw Exception(authResult.errorMessage);
}
if (authResult.accessToken == null) {
throw Exception('Failed to obtain access token');
}
log("Successfully initialized Lune SDK");
} catch (e) {
log("Failed to initialize Lune SDK: $e");
setState(() {
_errorMessage = e.toString();
});
}
if (!mounted) {
log("not mounted");
return;
}
if (authResult.accessToken != null) {
_lunePlugin = Lune(
baseUrl: config.baseURL,
accessToken: authResult.accessToken!,
);
await _lunePlugin?.initialize();
await _lunePlugin?.setLoggingCallback((event) {
log("Event: $event");
});
await _lunePlugin?.setRefreshTokenCallback(() async {
log("x-x: Fetching refresh token!");
authResult = await luneTokenizer(
config: config, refreshToken: authResult.refreshToken!);
log("x-x: Fetched refresh token! Access Token: ${authResult.accessToken}, Refresh Token: ${authResult.refreshToken}, Error Message: ${authResult.errorMessage}, Status: ${authResult.status}");
return authResult.accessToken;
});
setState(() {});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Lune SDK Examples'),
),
body: _buildBody(),
),
);
}
Widget _buildBody() {
if (_errorMessage != null) {
return Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.error_outline, color: Colors.red, size: 48),
const SizedBox(height: 16),
Text(
'Failed to initialize Lune SDK',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
Text(
_errorMessage!,
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
setState(() {
_errorMessage = null;
});
initLuneSDK();
},
child: const Text('Retry'),
),
],
),
),
);
}
if (_lunePlugin == null) {
return const Center(child: CircularProgressIndicator());
}
// return _buildLuneComposables();
return _buildSoloView(type: LuneViewType.expenseView);
}
Widget _buildSoloView({
LuneViewType type = LuneViewType.budgetView,
}) {
return _lunePlugin!.getView(
type,
solo: true,
);
}
Widget _buildLuneComposables() {
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildChartSection('Chart Views', [
LuneViewType.categorySpendChart,
LuneViewType.cashflowChart,
LuneViewType.brandTrendChart,
LuneViewType.categoryTrendChart,
]),
],
),
);
}
Widget _buildChartSection(String title, List<LuneViewType> viewTypes) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
title,
style: Theme.of(context).textTheme.titleLarge,
),
),
...viewTypes.map(
(viewType) => Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
viewType.identifier,
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
_lunePlugin!.getView(viewType, solo: false),
],
),
),
),
],
);
}
}