File tree Expand file tree Collapse file tree 7 files changed +39
-33
lines changed Expand file tree Collapse file tree 7 files changed +39
-33
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ class Prefs {
11
11
prefs = await SharedPreferences .getInstance ();
12
12
}
13
13
14
- static final localData = SharedPrefValue <int >('local_data' , prefs);
14
+ static final localData = SharedPrefValue <String >('local_data' , prefs);
15
15
static final email = SharedPrefValue <String >('_email' , prefs);
16
16
static final password = SharedPrefValue <String >('_password' , prefs);
17
17
static final remember = SharedPrefValue <bool >('_is_remember' , prefs);
Original file line number Diff line number Diff line change
1
+ import 'dart:convert' ;
2
+
1
3
import 'package:built_value/built_value.dart' ;
2
- import 'package:built_value/built_value.dart' ;
3
- import 'package:built_value/serializer.dart' ;
4
4
import 'package:built_value/serializer.dart' ;
5
5
import 'package:interview_task/data/remote/model/time_dto.dart' ;
6
6
7
7
import 'bpi.dart' ;
8
+ import 'default_serializers.dart' ;
8
9
9
10
part 'current_price.g.dart' ;
10
11
@@ -23,4 +24,14 @@ abstract class CurrentPrice
23
24
static Serializer <CurrentPrice > get serializer => _$currentPriceSerializer;
24
25
factory CurrentPrice ([void Function (CurrentPriceBuilder )? updates]) =
25
26
_$CurrentPrice ;
27
+
28
+ String toJson () {
29
+ return json
30
+ .encode (serializers.serializeWith (CurrentPrice .serializer, this ));
31
+ }
32
+
33
+ static CurrentPrice ? fromJson (String jsonString) {
34
+ return serializers.deserializeWith (
35
+ CurrentPrice .serializer, json.decode (jsonString));
36
+ }
26
37
}
Original file line number Diff line number Diff line change @@ -5,7 +5,6 @@ import 'package:http/http.dart' as http;
5
5
import 'package:http_interceptor/http/http.dart' ;
6
6
import 'package:interview_task/data/remote/endpoints.dart' ;
7
7
import 'package:interview_task/data/remote/logging_interceptor.dart' ;
8
- import 'package:interview_task/data/remote/model/response_dto.dart' ;
9
8
import 'package:interview_task/data/utils/response_parser.dart' ;
10
9
11
10
Client client = InterceptedClient .build (interceptors: [
@@ -40,11 +39,11 @@ dynamic _callGetApi(
40
39
return jsonDecode (res.body);
41
40
}
42
41
43
- Future <ResponseDto < T > > httpGet <T >(
42
+ Future <T > httpGet <T >(
44
43
final String path,
45
44
final Map <String , String > queryParams, [
46
45
final String ? baseUrl,
47
46
]) async {
48
47
final response = await _callGetApi (path, queryParams, baseUrl);
49
- return parseResponseDto <T >(response);
48
+ return parseResponse <T >(response);
50
49
}
Original file line number Diff line number Diff line change 1
1
import 'package:interview_task/data/remote/endpoints.dart' ;
2
2
import 'package:interview_task/data/remote/model/current_price.dart' ;
3
- import 'package:interview_task/data/remote/model/response_dto.dart' ;
4
3
import 'package:interview_task/data/remote/networking.dart' ;
5
4
6
5
class HomeRepo {
7
- static Future <ResponseDto < CurrentPrice > > getCurrency () async {
8
- ResponseDto < CurrentPrice > responseDto =
6
+ static Future <CurrentPrice > getCurrency () async {
7
+ CurrentPrice responseDto =
9
8
await httpGet <CurrentPrice >(Endpoints .getCurrency, {});
10
9
return responseDto;
11
10
}
Original file line number Diff line number Diff line change 1
1
import 'package:built_value/serializer.dart' ;
2
- import 'package:interview_task/data/remote/model/response_dto.dart' ;
3
2
4
3
import '../remote/model/default_serializers.dart' ;
5
4
6
5
///Parses a jsonEncoded string to an object of type ResponseDto<T>.
7
6
///T can be any built_value class with serializer defined with [Serializers]
8
- ResponseDto < T > parseResponseDto <T >(dynamic jsonString) {
9
- final specifiedType = FullType (ResponseDto , [ FullType ( T )] );
10
- final serializersWithBuilder = (serializers.toBuilder ()
11
- ..addBuilderFactory (specifiedType, ResponseDtoBuilder <T >.new ))
12
- .build ();
13
- final result = serializersWithBuilder .deserialize (
7
+ T parseResponse <T >(dynamic jsonString) {
8
+ final specifiedType = FullType (T );
9
+ // final serializersWithBuilder = (serializers.toBuilder()
10
+ // ..addBuilderFactory(specifiedType, ResponseDtoBuilder<T>.new))
11
+ // .build();
12
+ final result = serializers .deserialize (
14
13
jsonString,
15
14
specifiedType: specifiedType,
16
15
);
17
- return result as ResponseDto < T > ;
16
+ return result as T ;
18
17
}
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
2
2
import 'package:interview_task/ui/screens/home/home_viewmodel.dart' ;
3
3
import 'package:interview_task/ui/utils/base_class/base_screen.dart' ;
4
4
import 'package:interview_task/ui/utils/base_class/view_model.dart' ;
5
- import 'package:provider/provider.dart' ;
6
5
7
6
class HomeScreen extends StatefulWidget {
8
7
static const String route = "homeScreen" ;
@@ -24,19 +23,8 @@ class _HomeScreenState extends State<HomeScreen> {
24
23
25
24
@override
26
25
Widget build (BuildContext context) {
27
- return ScreenBase <HomeViewModel >(
28
- child: Scaffold (
29
- body: SingleChildScrollView (
30
- child: Center (
31
- child: ElevatedButton (
32
- onPressed: () {
33
- context.read <HomeViewModel >().getPrices ();
34
- },
35
- child: Text ("Api Call" ),
36
- ),
37
- ),
38
- ),
39
- ),
26
+ return const ScreenBase <HomeViewModel >(
27
+ child: Scaffold (),
40
28
);
41
29
}
42
30
}
Original file line number Diff line number Diff line change
1
+ import 'package:flutter/material.dart' ;
2
+ import 'package:interview_task/data/local/shared_pref_helper.dart' ;
1
3
import 'package:interview_task/data/remote/model/current_price.dart' ;
2
- import 'package:interview_task/data/remote/model/response_dto.dart' ;
3
4
import 'package:interview_task/data/remote/repo/home_repo.dart' ;
4
5
import 'package:interview_task/ui/utils/base_class/view_model.dart' ;
5
6
6
7
class HomeViewModel extends ViewModel {
7
8
void getPrices () {
8
9
callApi (() async {
9
- ResponseDto <CurrentPrice ?> responseDto = await HomeRepo .getCurrency ();
10
+ CurrentPrice responseDto = await HomeRepo .getCurrency ();
11
+ Prefs .localData.set (responseDto.toJson ());
12
+ getFromLocal ();
10
13
});
11
14
}
15
+
16
+ void getFromLocal () {
17
+ String json = Prefs .localData.get ();
18
+ CurrentPrice ? responseDto = CurrentPrice .fromJson (json);
19
+ debugPrint ("Get Local" );
20
+ debugPrint (responseDto.toString ());
21
+ }
12
22
}
You can’t perform that action at this time.
0 commit comments