| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import 'package:dartin/dartin.dart';
- import 'package:dio/dio.dart';
- import '../helper/constants.dart';
- import '../helper/shared_preferences.dart';
- import '../model/repository.dart';
- import '../viewmodel/home_provide.dart';
- const testScope = DartInScope('test');
- /// ViewModel 模块
- ///
- /// 定义ViewModel的构造方式
- final viewModelModule = Module([
- factory<HomeProvide>(({params}) => HomeProvide(params.get(0), get())),
- ])
- ..withScope(testScope, [
- ///other scope
- // factory<HomeProvide>(({params}) => HomeProvide(params.get(0), get<GithubRepo>())),
- ]);
- /// Repository 模块
- ///
- /// 定义Repository 的构造方式
- final repoModule = Module([
- factory<GithubRepo>(({params}) => GithubRepo(get(), get())),
- ]);
- /// Remote 模块
- ///
- /// 定义各网络接口服务的构造方式
- final remoteModule = Module([
- factory<GithubService>(({params}) => GithubService()),
- ]);
- /// Local 模块
- ///
- /// 定义数据库层及SharedPreference/KV等等本地存储的构造方式
- final localModule = Module([
- single<SpUtil>(({params}) => spUtil),
- ]);
- final appModule = [viewModelModule, repoModule, remoteModule, localModule];
- /// AuthInterceptor
- ///
- /// 添加header认证
- class AuthInterceptor extends Interceptor {
- @override
- onRequest(RequestOptions options) {
- final token = spUtil.getString(KEY_TOKEN);
- options.headers.update("Authorization", (_) => token, ifAbsent: () => token);
- return super.onRequest(options);
- }
- }
- final dio = Dio()
- ..options = BaseOptions(baseUrl: 'https://api.github.com/', connectTimeout: 30, receiveTimeout: 30)
- ..interceptors.add(AuthInterceptor())
- ..interceptors.add(LogInterceptor(responseBody: true, requestBody: true));
- SpUtil spUtil;
- /// init
- ///
- /// 初始化 [spUtil] 并启动[DartIn]
- init() async {
- spUtil = await SpUtil.getInstance();
- // DartIn start
- startDartIn(appModule);
- }
|