Skip to content

Commit eea8f46

Browse files
author
saksham
committed
Login And Api Completed"
1 parent ee1643d commit eea8f46

File tree

7 files changed

+39
-33
lines changed

7 files changed

+39
-33
lines changed

lib/data/local/shared_pref_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Prefs {
1111
prefs = await SharedPreferences.getInstance();
1212
}
1313

14-
static final localData = SharedPrefValue<int>('local_data', prefs);
14+
static final localData = SharedPrefValue<String>('local_data', prefs);
1515
static final email = SharedPrefValue<String>('_email', prefs);
1616
static final password = SharedPrefValue<String>('_password', prefs);
1717
static final remember = SharedPrefValue<bool>('_is_remember', prefs);

lib/data/remote/model/current_price.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import 'dart:convert';
2+
13
import 'package:built_value/built_value.dart';
2-
import 'package:built_value/built_value.dart';
3-
import 'package:built_value/serializer.dart';
44
import 'package:built_value/serializer.dart';
55
import 'package:interview_task/data/remote/model/time_dto.dart';
66

77
import 'bpi.dart';
8+
import 'default_serializers.dart';
89

910
part 'current_price.g.dart';
1011

@@ -23,4 +24,14 @@ abstract class CurrentPrice
2324
static Serializer<CurrentPrice> get serializer => _$currentPriceSerializer;
2425
factory CurrentPrice([void Function(CurrentPriceBuilder)? updates]) =
2526
_$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+
}
2637
}

lib/data/remote/networking.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:http/http.dart' as http;
55
import 'package:http_interceptor/http/http.dart';
66
import 'package:interview_task/data/remote/endpoints.dart';
77
import 'package:interview_task/data/remote/logging_interceptor.dart';
8-
import 'package:interview_task/data/remote/model/response_dto.dart';
98
import 'package:interview_task/data/utils/response_parser.dart';
109

1110
Client client = InterceptedClient.build(interceptors: [
@@ -40,11 +39,11 @@ dynamic _callGetApi(
4039
return jsonDecode(res.body);
4140
}
4241

43-
Future<ResponseDto<T>> httpGet<T>(
42+
Future<T> httpGet<T>(
4443
final String path,
4544
final Map<String, String> queryParams, [
4645
final String? baseUrl,
4746
]) async {
4847
final response = await _callGetApi(path, queryParams, baseUrl);
49-
return parseResponseDto<T>(response);
48+
return parseResponse<T>(response);
5049
}

lib/data/remote/repo/home_repo.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import 'package:interview_task/data/remote/endpoints.dart';
22
import 'package:interview_task/data/remote/model/current_price.dart';
3-
import 'package:interview_task/data/remote/model/response_dto.dart';
43
import 'package:interview_task/data/remote/networking.dart';
54

65
class HomeRepo {
7-
static Future<ResponseDto<CurrentPrice>> getCurrency() async {
8-
ResponseDto<CurrentPrice> responseDto =
6+
static Future<CurrentPrice> getCurrency() async {
7+
CurrentPrice responseDto =
98
await httpGet<CurrentPrice>(Endpoints.getCurrency, {});
109
return responseDto;
1110
}

lib/data/utils/response_parser.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import 'package:built_value/serializer.dart';
2-
import 'package:interview_task/data/remote/model/response_dto.dart';
32

43
import '../remote/model/default_serializers.dart';
54

65
///Parses a jsonEncoded string to an object of type ResponseDto<T>.
76
///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(
1413
jsonString,
1514
specifiedType: specifiedType,
1615
);
17-
return result as ResponseDto<T>;
16+
return result as T;
1817
}

lib/ui/screens/home/home_screen.dart

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
22
import 'package:interview_task/ui/screens/home/home_viewmodel.dart';
33
import 'package:interview_task/ui/utils/base_class/base_screen.dart';
44
import 'package:interview_task/ui/utils/base_class/view_model.dart';
5-
import 'package:provider/provider.dart';
65

76
class HomeScreen extends StatefulWidget {
87
static const String route = "homeScreen";
@@ -24,19 +23,8 @@ class _HomeScreenState extends State<HomeScreen> {
2423

2524
@override
2625
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(),
4028
);
4129
}
4230
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:interview_task/data/local/shared_pref_helper.dart';
13
import 'package:interview_task/data/remote/model/current_price.dart';
2-
import 'package:interview_task/data/remote/model/response_dto.dart';
34
import 'package:interview_task/data/remote/repo/home_repo.dart';
45
import 'package:interview_task/ui/utils/base_class/view_model.dart';
56

67
class HomeViewModel extends ViewModel {
78
void getPrices() {
89
callApi(() async {
9-
ResponseDto<CurrentPrice?> responseDto = await HomeRepo.getCurrency();
10+
CurrentPrice responseDto = await HomeRepo.getCurrency();
11+
Prefs.localData.set(responseDto.toJson());
12+
getFromLocal();
1013
});
1114
}
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+
}
1222
}

0 commit comments

Comments
 (0)