flutter(rider): rebuild lib on Cubit/Bloc + Tripz backend (core + auth slice)
- keep store identity (com.mobileapp.store.ride, shorebird, firebase, fonts); old lib -> lib_old_getx - core: config/di/router(go_router)/theme/api_client(dio+tenant+bearer)/token_store(secure) - auth: repo + AuthCubit + OTP phone/otp screens; home: flutter_map (OSM placeholder, انطلق TODO) - android usesCleartextTraffic=true for http API; maps=flutter_map (no Google), state=Cubit Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import '../config.dart';
|
||||
import '../storage/token_store.dart';
|
||||
|
||||
/// عميل HTTP لكل نداءات الـ API — يحقن x-tenant-id و Bearer تلقائياً.
|
||||
class ApiClient {
|
||||
final Dio dio;
|
||||
|
||||
ApiClient(TokenStore store)
|
||||
: dio = Dio(BaseOptions(
|
||||
baseUrl: AppConfig.baseUrl,
|
||||
connectTimeout: const Duration(seconds: 15),
|
||||
receiveTimeout: const Duration(seconds: 20),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
)) {
|
||||
dio.interceptors.add(InterceptorsWrapper(
|
||||
onRequest: (options, handler) async {
|
||||
options.headers['x-tenant-id'] = AppConfig.tenantSlug;
|
||||
final token = await store.access();
|
||||
if (token != null && token.isNotEmpty) {
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
handler.next(options);
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/// إعداد التطبيق. يُمرَّر عبر --dart-define للنكهات (flavor) لاحقاً.
|
||||
class AppConfig {
|
||||
static const String baseUrl = String.fromEnvironment(
|
||||
'BASE_URL',
|
||||
defaultValue: 'http://194.163.173.157:4010/api',
|
||||
);
|
||||
|
||||
/// slug المستأجر (يُبنى لكل نكهة). حالياً: siro.
|
||||
static const String tenantSlug = String.fromEnvironment(
|
||||
'TENANT',
|
||||
defaultValue: 'siro',
|
||||
);
|
||||
|
||||
static const String appName = 'Tripz';
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'api/api_client.dart';
|
||||
import 'storage/token_store.dart';
|
||||
import '../features/auth/data/auth_repository.dart';
|
||||
|
||||
final GetIt getIt = GetIt.instance;
|
||||
|
||||
/// حقن التبعيات (بديل bindings في GetX).
|
||||
Future<void> setupDi() async {
|
||||
getIt.registerLazySingleton<TokenStore>(() => TokenStore());
|
||||
getIt.registerLazySingleton<ApiClient>(() => ApiClient(getIt<TokenStore>()));
|
||||
getIt.registerLazySingleton<AuthRepository>(
|
||||
() => AuthRepository(getIt<ApiClient>(), getIt<TokenStore>()),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../features/auth/cubit/auth_cubit.dart';
|
||||
import '../features/auth/cubit/auth_state.dart';
|
||||
import '../features/auth/view/phone_screen.dart';
|
||||
import '../features/auth/view/otp_screen.dart';
|
||||
import '../features/home/view/home_screen.dart';
|
||||
|
||||
GoRouter buildRouter(AuthCubit auth) {
|
||||
return GoRouter(
|
||||
initialLocation: '/phone',
|
||||
refreshListenable: _StreamRefresh(auth.stream),
|
||||
redirect: (context, state) {
|
||||
final status = auth.state.status;
|
||||
final loc = state.matchedLocation;
|
||||
final onAuthPages = loc == '/phone' || loc == '/otp';
|
||||
|
||||
if (status == AuthStatus.authenticated) {
|
||||
return onAuthPages ? '/home' : null;
|
||||
}
|
||||
if (status == AuthStatus.codeSent && loc == '/phone') return '/otp';
|
||||
if (status != AuthStatus.authenticated && !onAuthPages) return '/phone';
|
||||
return null;
|
||||
},
|
||||
routes: [
|
||||
GoRoute(path: '/phone', builder: (_, __) => const PhoneScreen()),
|
||||
GoRoute(path: '/otp', builder: (_, __) => const OtpScreen()),
|
||||
GoRoute(path: '/home', builder: (_, __) => const HomeScreen()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
class _StreamRefresh extends ChangeNotifier {
|
||||
late final StreamSubscription<dynamic> _sub;
|
||||
_StreamRefresh(Stream<dynamic> stream) {
|
||||
notifyListeners();
|
||||
_sub = stream.asBroadcastStream().listen((_) => notifyListeners());
|
||||
}
|
||||
@override
|
||||
void dispose() {
|
||||
_sub.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
/// تخزين آمن للتوكنات (access + refresh).
|
||||
class TokenStore {
|
||||
final FlutterSecureStorage _s = const FlutterSecureStorage();
|
||||
|
||||
Future<void> save(String access, String refresh) async {
|
||||
await _s.write(key: 'access_token', value: access);
|
||||
await _s.write(key: 'refresh_token', value: refresh);
|
||||
}
|
||||
|
||||
Future<String?> access() => _s.read(key: 'access_token');
|
||||
Future<String?> refresh() => _s.read(key: 'refresh_token');
|
||||
Future<void> clear() => _s.deleteAll();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// ثيم Tripz — أساس بسيط، خط عربي (mohanad) وألوان.
|
||||
ThemeData buildTheme() {
|
||||
const seed = Color(0xFF0E7C66); // أخضر تركوازي
|
||||
return ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: seed),
|
||||
fontFamily: 'mohanad',
|
||||
inputDecorationTheme: const InputDecorationTheme(
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
),
|
||||
filledButtonTheme: FilledButtonThemeData(
|
||||
style: FilledButton.styleFrom(
|
||||
minimumSize: const Size.fromHeight(52),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user