Line data Source code
1 : import "package:errorlookup/core/data/repository/error_codes_repository.dart"; 2 : import "package:errorlookup/core/models/error_detail.dart"; 3 : import "package:errorlookup/core/models/result.dart"; 4 : import "package:errorlookup/feature/home/models/home_state.dart"; 5 : import "package:flutter_riverpod/flutter_riverpod.dart"; 6 : import "package:get_it/get_it.dart"; 7 : 8 : /// ホーム画面のビジネスロジックを扱うViewModel 9 : class HomeViewModel extends StateNotifier<HomeState> { 10 1 : HomeViewModel( 11 : {required ErrorCodesRepository errorCodesRepository, 12 : bool initialFetch = true}) 13 : : _errorCodesRepository = errorCodesRepository, 14 1 : super(const HomeState()) { 15 : if (initialFetch) { 16 1 : fetchErrorCodes(); 17 : } 18 : } 19 : 20 : final ErrorCodesRepository _errorCodesRepository; 21 : 22 : /// エラーコードの一覧を取得します 23 1 : Future<Result<Object, Exception>> fetchErrorCodes() async { 24 2 : if (!state.isFetchingErrorCodes) { 25 4 : state = state.copyWith(isFetchingErrorCodes: true); 26 : List<Result<List<ErrorDetail>, Exception>> errorCodes = 27 2 : await Future.wait([ 28 2 : _errorCodesRepository.getErrorCodes(ErrorType.windows), 29 2 : _errorCodesRepository.getErrorCodes(ErrorType.linux), 30 2 : _errorCodesRepository.getErrorCodes(ErrorType.curl) 31 : ]); 32 2 : for (final element in errorCodes) { 33 : // 一部が取得失敗していた場合はその旨を返す 34 1 : if (element is Failure) { 35 0 : state = state.copyWith(isFetchingErrorCodes: false); 36 : return element; 37 : } 38 : } 39 4 : state = state.copyWith( 40 : windowsErrorDetails: 41 2 : (errorCodes[0] as Success<List<ErrorDetail>, Exception>).value, 42 : linuxErrorDetails: 43 2 : (errorCodes[1] as Success<List<ErrorDetail>, Exception>).value, 44 : curlErrorDetails: 45 2 : (errorCodes[2] as Success<List<ErrorDetail>, Exception>).value, 46 : isFetchingErrorCodes: false); 47 : return const Success("success"); 48 : } 49 : return const Success("fetching now"); 50 : } 51 : 52 : /// ErrorCodeの入力値を更新する 53 1 : void updateErrorCodeInput(final String newValue) { 54 2 : final newErrorInput = int.tryParse(newValue) ?? -1; 55 4 : state = state.copyWith(errorCodeInput: newErrorInput); 56 : } 57 : } 58 : 59 0 : final homeViewModelProvider = StateNotifierProvider<HomeViewModel, HomeState>( 60 0 : (ref) => 61 0 : HomeViewModel(errorCodesRepository: GetIt.I<ErrorCodesRepository>()));