Skip to content

Commit

Permalink
[FEAT] Implementa funcionalidade ViewCount (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylancavalcante committed Jan 10, 2025
1 parent e5b6742 commit d022f55
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lib/core/data/local/StorageValue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:aranduapp/core/log/Log.dart';

class StorageValue {

static const String _authToken = "auth_token";
static const String _refreshToken = "refresh_token";
static const String _email = "email";
static const String _name = "name";
static const String _viewOnboardingCount = "view_onboarding_count";

static StorageValue? _singleton;
final FlutterSecureStorage storage;
Expand All @@ -15,8 +15,7 @@ class StorageValue {
: storage = const FlutterSecureStorage(
aOptions: AndroidOptions(encryptedSharedPreferences: true));

factory StorageValue.getInstance() =>
_singleton ??= StorageValue._internal();
factory StorageValue.getInstance() => _singleton ??= StorageValue._internal();

Future<void> _setValue(String key, String value) async {
try {
Expand All @@ -39,11 +38,15 @@ class StorageValue {
Future<void> setRefreshToken(String token) => _setValue(_refreshToken, token);
Future<void> setEmail(String email) => _setValue(_email, email);
Future<void> setName(String name) => _setValue(_name, name);
Future<void> setViewOnboardingCount(int count) =>
_setValue(_viewOnboardingCount, count.toString());

Future<String?> getAuthToken() => _getValue(_authToken);
Future<String?> getRefreshToken() => _getValue(_refreshToken);
Future<String?> getEmail() => _getValue(_email);
Future<String?> getName() => _getValue(_name);
Future<int?> getOnboardingCount() async =>
int.tryParse(await _getValue(_viewOnboardingCount) ?? '');

Future<void> clear() async {
try {
Expand Down
18 changes: 14 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import 'package:aranduapp/config/ThemeApp.dart';
import 'package:aranduapp/ui/login/view/LoginView.dart';
import 'package:aranduapp/ui/welcome/view/WelcomeView.dart';
import 'package:aranduapp/ui/onboarding/viewModel/onboarding_view_model.dart';
import 'package:flutter/material.dart';
import 'package:aranduapp/core/data/local/StorageValue.dart';

void main() {
runApp(const MyApp());
void main() async {
WidgetsFlutterBinding.ensureInitialized();

await OnboardingViewModel().incrementNumber();

int viewCount = await StorageValue.getInstance().getOnboardingCount() ?? 0;

runApp(MyApp(viewCount: viewCount));
}

class MyApp extends StatelessWidget {
const MyApp({super.key});
final int viewCount;

const MyApp({super.key, required this.viewCount});

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeApp.themeData(),
darkTheme: ThemeApp.darkThemeData(),
debugShowCheckedModeBanner: false,
home: WelcomeView(),
home: viewCount >= 3 ? const Login() : WelcomeView(),
);
}
}
13 changes: 13 additions & 0 deletions lib/ui/onboarding/viewModel/onboarding_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:aranduapp/ui/login/view/LoginView.dart';
import 'package:flutter/material.dart';
import 'package:aranduapp/core/data/local/StorageValue.dart';

class OnboardingViewModel extends ChangeNotifier {
final PageController pageController = PageController();
Expand All @@ -23,4 +24,16 @@ class OnboardingViewModel extends ChangeNotifier {
MaterialPageRoute(builder: (context) => const Login()),
);
}

Future<void> incrementNumber() async {
try {
final storage = StorageValue.getInstance();
int currentCount = await storage.getOnboardingCount() ?? 0;
currentCount++;
await storage.setViewOnboardingCount(currentCount);
debugPrint('View count incremented to: $currentCount');
} catch (e) {
debugPrint('Error incrementing view count: $e');
}
}
}

0 comments on commit d022f55

Please sign in to comment.