636 lines
17 KiB
Dart
636 lines
17 KiB
Dart
// ========================================
|
|
// DATA SYNC PAGE - POST LOGIN SYNC
|
|
// lib/presentation/sync/pages/data_sync_page.dart
|
|
// ========================================
|
|
|
|
import 'dart:async';
|
|
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
|
|
import 'package:enaklo_pos/presentation/home/pages/dashboard_page.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../../core/components/buttons.dart';
|
|
import '../../../core/components/spaces.dart';
|
|
import '../../../core/constants/colors.dart';
|
|
import '../bloc/data_sync_bloc.dart';
|
|
|
|
class DataSyncPage extends StatefulWidget {
|
|
const DataSyncPage({super.key});
|
|
|
|
@override
|
|
State<DataSyncPage> createState() => _DataSyncPageState();
|
|
}
|
|
|
|
class _DataSyncPageState extends State<DataSyncPage>
|
|
with TickerProviderStateMixin {
|
|
late AnimationController _animationController;
|
|
late Animation<double> _progressAnimation;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_animationController = AnimationController(
|
|
duration: Duration(milliseconds: 500),
|
|
vsync: this,
|
|
);
|
|
_progressAnimation = Tween<double>(
|
|
begin: 0.0,
|
|
end: 1.0,
|
|
).animate(CurvedAnimation(
|
|
parent: _animationController,
|
|
curve: Curves.easeInOut,
|
|
));
|
|
|
|
// Auto start sync
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
context.read<DataSyncBloc>().add(const DataSyncEvent.startSync());
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_animationController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.grey.shade50,
|
|
body: SafeArea(
|
|
child: BlocConsumer<DataSyncBloc, DataSyncState>(
|
|
listener: (context, state) {
|
|
state.maybeWhen(
|
|
orElse: () {},
|
|
syncing: (step, progress, message) {
|
|
_animationController.animateTo(progress);
|
|
},
|
|
completed: (stats) {
|
|
_animationController.animateTo(1.0);
|
|
// Navigate to home after delay
|
|
Future.delayed(Duration(seconds: 2), () {
|
|
context.pushReplacement(DashboardPage());
|
|
});
|
|
},
|
|
error: (message) {
|
|
_animationController.stop();
|
|
},
|
|
);
|
|
},
|
|
builder: (context, state) {
|
|
return Padding(
|
|
padding: EdgeInsets.all(24),
|
|
child: Column(
|
|
children: [
|
|
SpaceHeight(60),
|
|
|
|
// Header
|
|
_buildHeader(),
|
|
|
|
SpaceHeight(60),
|
|
|
|
// Sync progress
|
|
Expanded(
|
|
child: state.when(
|
|
initial: () => _buildInitialState(),
|
|
syncing: (step, progress, message) =>
|
|
_buildSyncingState(step, progress, message),
|
|
completed: (stats) => _buildCompletedState(stats),
|
|
error: (message) => _buildErrorState(message),
|
|
),
|
|
),
|
|
|
|
SpaceHeight(40),
|
|
|
|
// Actions
|
|
_buildActions(state),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildHeader() {
|
|
return Column(
|
|
children: [
|
|
Container(
|
|
width: 80,
|
|
height: 80,
|
|
decoration: BoxDecoration(
|
|
color: AppColors.primary.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(20),
|
|
),
|
|
child: Icon(
|
|
Icons.sync,
|
|
size: 40,
|
|
color: AppColors.primary,
|
|
),
|
|
),
|
|
SpaceHeight(20),
|
|
Text(
|
|
'Sinkronisasi Data',
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.grey.shade800,
|
|
),
|
|
),
|
|
SpaceHeight(8),
|
|
Text(
|
|
'Mengunduh data terbaru ke perangkat',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: Colors.grey.shade600,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildInitialState() {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.download_rounded,
|
|
size: 64,
|
|
color: Colors.grey.shade400,
|
|
),
|
|
SpaceHeight(20),
|
|
Text(
|
|
'Siap untuk sinkronisasi',
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
SpaceHeight(8),
|
|
Text(
|
|
'Tekan tombol mulai untuk mengunduh data',
|
|
style: TextStyle(
|
|
color: Colors.grey.shade600,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildSyncingState(SyncStep step, double progress, String message) {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
// Progress circle
|
|
Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
SizedBox(
|
|
width: 120,
|
|
height: 120,
|
|
child: AnimatedBuilder(
|
|
animation: _progressAnimation,
|
|
builder: (context, child) {
|
|
return CircularProgressIndicator(
|
|
value: _progressAnimation.value,
|
|
strokeWidth: 8,
|
|
backgroundColor: Colors.grey.shade200,
|
|
valueColor:
|
|
AlwaysStoppedAnimation<Color>(AppColors.primary),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
Column(
|
|
children: [
|
|
Icon(
|
|
_getSyncIcon(step),
|
|
size: 32,
|
|
color: AppColors.primary,
|
|
),
|
|
SpaceHeight(4),
|
|
AnimatedBuilder(
|
|
animation: _progressAnimation,
|
|
builder: (context, child) {
|
|
return Text(
|
|
'${(_progressAnimation.value * 100).toInt()}%',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
color: AppColors.primary,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
|
|
SpaceHeight(30),
|
|
|
|
// Step indicator
|
|
_buildStepIndicator(step),
|
|
|
|
SpaceHeight(20),
|
|
|
|
// Current message
|
|
Container(
|
|
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
|
|
decoration: BoxDecoration(
|
|
color: Colors.blue.shade50,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Text(
|
|
message,
|
|
style: TextStyle(
|
|
color: Colors.blue.shade700,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
|
|
SpaceHeight(20),
|
|
|
|
// Sync details
|
|
_buildSyncDetails(step, progress),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildStepIndicator(SyncStep currentStep) {
|
|
final steps = [
|
|
('Produk', SyncStep.products, Icons.inventory_2),
|
|
('Kategori', SyncStep.categories, Icons.category),
|
|
('Variant', SyncStep.variants, Icons.tune),
|
|
('Selesai', SyncStep.completed, Icons.check_circle),
|
|
];
|
|
|
|
return Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: steps.map((stepData) {
|
|
final (label, step, icon) = stepData;
|
|
final isActive = step == currentStep;
|
|
final isCompleted = step.index < currentStep.index;
|
|
|
|
return Container(
|
|
margin: EdgeInsets.symmetric(horizontal: 4),
|
|
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: isActive
|
|
? AppColors.primary.withOpacity(0.1)
|
|
: isCompleted
|
|
? Colors.green.shade50
|
|
: Colors.grey.shade100,
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(
|
|
isCompleted ? Icons.check : icon,
|
|
size: 14,
|
|
color: isActive
|
|
? AppColors.primary
|
|
: isCompleted
|
|
? Colors.green.shade600
|
|
: Colors.grey.shade500,
|
|
),
|
|
SizedBox(width: 4),
|
|
Text(
|
|
label,
|
|
style: TextStyle(
|
|
fontSize: 11,
|
|
fontWeight: isActive ? FontWeight.w600 : FontWeight.normal,
|
|
color: isActive
|
|
? AppColors.primary
|
|
: isCompleted
|
|
? Colors.green.shade600
|
|
: Colors.grey.shade600,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}).toList(),
|
|
);
|
|
}
|
|
|
|
Widget _buildSyncDetails(SyncStep step, double progress) {
|
|
return Container(
|
|
padding: EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: Colors.grey.shade200),
|
|
),
|
|
child: Column(
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
'Status:',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w500,
|
|
color: Colors.grey.shade700,
|
|
),
|
|
),
|
|
Text(
|
|
_getStepLabel(step),
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.primary,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
SpaceHeight(8),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
'Progress:',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w500,
|
|
color: Colors.grey.shade700,
|
|
),
|
|
),
|
|
Text(
|
|
'${(progress * 100).toInt()}%',
|
|
style: TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.primary,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildCompletedState(SyncStats stats) {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
// Success icon
|
|
Container(
|
|
width: 100,
|
|
height: 100,
|
|
decoration: BoxDecoration(
|
|
color: Colors.green.shade50,
|
|
borderRadius: BorderRadius.circular(50),
|
|
),
|
|
child: Icon(
|
|
Icons.check_circle,
|
|
size: 60,
|
|
color: Colors.green.shade600,
|
|
),
|
|
),
|
|
|
|
SpaceHeight(30),
|
|
|
|
Text(
|
|
'Sinkronisasi Berhasil!',
|
|
style: TextStyle(
|
|
fontSize: 22,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.green.shade700,
|
|
),
|
|
),
|
|
|
|
SpaceHeight(16),
|
|
|
|
Text(
|
|
'Data berhasil diunduh ke perangkat',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: Colors.grey.shade600,
|
|
),
|
|
),
|
|
|
|
SpaceHeight(30),
|
|
|
|
// Stats cards
|
|
Container(
|
|
padding: EdgeInsets.all(20),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: Colors.grey.shade200),
|
|
),
|
|
child: Column(
|
|
children: [
|
|
Text(
|
|
'Data yang Diunduh',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w600,
|
|
color: Colors.grey.shade700,
|
|
),
|
|
),
|
|
SpaceHeight(16),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
_buildStatItem(
|
|
'Produk',
|
|
'${stats.totalProducts}',
|
|
Icons.inventory_2,
|
|
Colors.blue,
|
|
),
|
|
_buildStatItem(
|
|
'Kategori',
|
|
'${stats.totalCategories}',
|
|
Icons.category,
|
|
Colors.green,
|
|
),
|
|
_buildStatItem(
|
|
'Variant',
|
|
'${stats.totalVariants}',
|
|
Icons.tune,
|
|
Colors.orange,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
|
|
SpaceHeight(20),
|
|
|
|
Container(
|
|
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
decoration: BoxDecoration(
|
|
color: Colors.grey.shade100,
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
child: Text(
|
|
'Mengalihkan ke halaman utama...',
|
|
style: TextStyle(
|
|
color: Colors.grey.shade600,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildErrorState(String message) {
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
Icons.error_outline,
|
|
size: 64,
|
|
color: Colors.red.shade400,
|
|
),
|
|
SpaceHeight(20),
|
|
Text(
|
|
'Sinkronisasi Gagal',
|
|
style: TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.red.shade600,
|
|
),
|
|
),
|
|
SpaceHeight(12),
|
|
Container(
|
|
padding: EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.red.shade50,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Text(
|
|
message,
|
|
style: TextStyle(
|
|
color: Colors.red.shade700,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
SpaceHeight(20),
|
|
Text(
|
|
'Periksa koneksi internet dan coba lagi',
|
|
style: TextStyle(
|
|
color: Colors.grey.shade600,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildStatItem(
|
|
String label, String value, IconData icon, Color color) {
|
|
return Column(
|
|
children: [
|
|
Container(
|
|
padding: EdgeInsets.all(12),
|
|
decoration: BoxDecoration(
|
|
color: color.withOpacity(0.1),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Icon(
|
|
icon,
|
|
size: 24,
|
|
color: color,
|
|
),
|
|
),
|
|
SpaceHeight(8),
|
|
Text(
|
|
value,
|
|
style: TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
color: color,
|
|
),
|
|
),
|
|
Text(
|
|
label,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Colors.grey.shade600,
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildActions(DataSyncState state) {
|
|
return state.when(
|
|
initial: () => Button.filled(
|
|
onPressed: () {
|
|
context.read<DataSyncBloc>().add(const DataSyncEvent.startSync());
|
|
},
|
|
label: 'Mulai Sinkronisasi',
|
|
),
|
|
syncing: (step, progress, message) => Button.outlined(
|
|
onPressed: () {
|
|
context.read<DataSyncBloc>().add(const DataSyncEvent.cancelSync());
|
|
},
|
|
label: 'Batalkan',
|
|
),
|
|
completed: (stats) => Button.filled(
|
|
onPressed: () {
|
|
Navigator.of(context).pushReplacementNamed('/home');
|
|
},
|
|
label: 'Lanjutkan ke Aplikasi',
|
|
),
|
|
error: (message) => Row(
|
|
children: [
|
|
Expanded(
|
|
child: Button.outlined(
|
|
onPressed: () {
|
|
Navigator.of(context).pushReplacementNamed('/home');
|
|
},
|
|
label: 'Lewati',
|
|
),
|
|
),
|
|
SizedBox(width: 16),
|
|
Expanded(
|
|
child: Button.filled(
|
|
onPressed: () {
|
|
context
|
|
.read<DataSyncBloc>()
|
|
.add(const DataSyncEvent.startSync());
|
|
},
|
|
label: 'Coba Lagi',
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
IconData _getSyncIcon(SyncStep step) {
|
|
switch (step) {
|
|
case SyncStep.products:
|
|
return Icons.inventory_2;
|
|
case SyncStep.categories:
|
|
return Icons.category;
|
|
case SyncStep.variants:
|
|
return Icons.tune;
|
|
case SyncStep.completed:
|
|
return Icons.check_circle;
|
|
}
|
|
}
|
|
|
|
String _getStepLabel(SyncStep step) {
|
|
switch (step) {
|
|
case SyncStep.products:
|
|
return 'Mengunduh Produk';
|
|
case SyncStep.categories:
|
|
return 'Mengunduh Kategori';
|
|
case SyncStep.variants:
|
|
return 'Mengunduh Variant';
|
|
case SyncStep.completed:
|
|
return 'Selesai';
|
|
}
|
|
}
|
|
}
|