Fakultas Ilmu Komputer UI

Commit 93793b12 authored by Surya Nirvana's avatar Surya Nirvana
Browse files

[REFACTOR] Implement bloc for add new lokasi to database

parent 02c3aa8f
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"geolocator","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-5.3.2+2/","dependencies":["location_permissions"]},{"name":"google_maps_flutter","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_maps_flutter-0.5.33/","dependencies":[]},{"name":"google_sign_in","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in-4.5.6/","dependencies":[]},{"name":"image_picker","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7+14/","dependencies":[]},{"name":"location","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/location-2.5.4/","dependencies":[]},{"name":"location_permissions","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/location_permissions-3.0.0+1/","dependencies":[]},{"name":"path_provider","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.24/","dependencies":[]},{"name":"share","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/share-0.6.5+4/","dependencies":[]},{"name":"shared_preferences","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.6+3/","dependencies":[]},{"name":"sqflite","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+2/","dependencies":[]}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_plugin_android_lifecycle-1.0.11/","dependencies":[]},{"name":"geolocator","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-5.3.2+2/","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_api_availability-2.0.4/","dependencies":[]},{"name":"google_maps_flutter","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_maps_flutter-0.5.33/","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"google_sign_in","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in-4.5.6/","dependencies":[]},{"name":"image_picker","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7+14/","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"location","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/location-2.5.4/","dependencies":[]},{"name":"location_permissions","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/location_permissions-3.0.0+1/","dependencies":[]},{"name":"path_provider","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.24/","dependencies":[]},{"name":"share","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/share-0.6.5+4/","dependencies":[]},{"name":"shared_preferences","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.6+3/","dependencies":[]},{"name":"sqflite","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+2/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.4+6/","dependencies":[]},{"name":"shared_preferences_macos","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_macos-0.0.1+11/","dependencies":[]},{"name":"sqflite","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+2/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-0.0.1+2/","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-0.0.4+3/","dependencies":[]}],"web":[{"name":"google_sign_in_web","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.9.2/","dependencies":[]},{"name":"shared_preferences_web","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_web-0.1.2+7/","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"geolocator","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","dependencies":[]},{"name":"google_maps_flutter","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"google_sign_in","dependencies":["google_sign_in_web"]},{"name":"google_sign_in_web","dependencies":[]},{"name":"image_picker","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"location","dependencies":[]},{"name":"location_permissions","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"share","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_macos","shared_preferences_web"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"sqflite","dependencies":[]}],"date_created":"2020-12-01 11:58:06.957648","version":"1.20.3"}
\ No newline at end of file
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"geolocator","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-5.3.2+2/","dependencies":["location_permissions"]},{"name":"google_maps_flutter","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_maps_flutter-0.5.33/","dependencies":[]},{"name":"google_sign_in","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in-4.5.6/","dependencies":[]},{"name":"image_picker","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7+14/","dependencies":[]},{"name":"location","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/location-2.5.4/","dependencies":[]},{"name":"location_permissions","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/location_permissions-3.0.0+1/","dependencies":[]},{"name":"path_provider","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.24/","dependencies":[]},{"name":"share","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/share-0.6.5+4/","dependencies":[]},{"name":"shared_preferences","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.6+3/","dependencies":[]},{"name":"sqflite","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+2/","dependencies":[]}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_plugin_android_lifecycle-1.0.11/","dependencies":[]},{"name":"geolocator","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-5.3.2+2/","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_api_availability-2.0.4/","dependencies":[]},{"name":"google_maps_flutter","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_maps_flutter-0.5.33/","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"google_sign_in","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in-4.5.6/","dependencies":[]},{"name":"image_picker","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7+14/","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"location","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/location-2.5.4/","dependencies":[]},{"name":"location_permissions","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/location_permissions-3.0.0+1/","dependencies":[]},{"name":"path_provider","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.24/","dependencies":[]},{"name":"share","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/share-0.6.5+4/","dependencies":[]},{"name":"shared_preferences","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.6+3/","dependencies":[]},{"name":"sqflite","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+2/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.4+6/","dependencies":[]},{"name":"shared_preferences_macos","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_macos-0.0.1+11/","dependencies":[]},{"name":"sqflite","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/sqflite-1.3.1+2/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-0.0.1+2/","dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_windows-0.0.4+3/","dependencies":[]}],"web":[{"name":"google_sign_in_web","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/google_sign_in_web-0.9.2/","dependencies":[]},{"name":"shared_preferences_web","path":"/Users/suryanirvana/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_web-0.1.2+7/","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"geolocator","dependencies":["google_api_availability","location_permissions"]},{"name":"google_api_availability","dependencies":[]},{"name":"google_maps_flutter","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"google_sign_in","dependencies":["google_sign_in_web"]},{"name":"google_sign_in_web","dependencies":[]},{"name":"image_picker","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"location","dependencies":[]},{"name":"location_permissions","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux","path_provider_windows"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"path_provider_windows","dependencies":[]},{"name":"share","dependencies":[]},{"name":"shared_preferences","dependencies":["shared_preferences_macos","shared_preferences_web"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"sqflite","dependencies":[]}],"date_created":"2020-12-01 17:23:10.891742","version":"1.20.3"}
\ No newline at end of file
......@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:bisaGo/model/lokasi.dart';
import 'package:bisaGo/network/data/network_model.dart';
import 'package:bisaGo/repository/lokasi_repository.dart';
import 'package:http/http.dart';
class LokasiResponseBloc {
StreamController _recentSearchController;
......@@ -49,6 +50,14 @@ class LokasiResponseBloc {
}
}
Future<dynamic> addNewLokasi(Map<String, dynamic> lokasi) async {
try {
return await _lokasiRepository.addNewLokasi(lokasi);
} catch (e) {
return Response('Failed to add new location', 400);
}
}
void saveRecentSearch(Lokasi search) async {
await _lokasiRepository.saveRecentSearch(search);
}
......
import 'dart:io';
import 'package:bisaGo/bloc/lokasi_response_bloc.dart';
import 'package:bisaGo/page/login/login.dart';
import 'package:bisaGo/page/pencarian/pencarian.dart';
import 'package:dio/dio.dart';
......@@ -13,7 +14,6 @@ import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:image_picker/image_picker.dart';
import 'package:bisaGo/component/bisago_appbar.dart';
import 'package:bisaGo/config/styles.dart';
import 'package:bisaGo/network/network_interface.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AddLokasi extends StatefulWidget {
......@@ -109,6 +109,7 @@ class _AddLokasiState extends State<AddLokasi> {
}
},
child: Container(
key: Key('Pencarian'),
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
......@@ -314,6 +315,7 @@ class _AddLokasiState extends State<AddLokasi> {
}
Future<void> addLocationToApi() async {
final lokasiResponseBloc = LokasiResponseBloc();
final sharedPreferences = await SharedPreferences.getInstance();
if (sharedPreferences.getString('token') == null) {
await Navigator.push(context, MaterialPageRoute(builder: (_) => Login()));
......@@ -323,20 +325,16 @@ class _AddLokasiState extends State<AddLokasi> {
? alamatController.text.substring(0, 100)
: alamatController.text;
data['no_telp'] = phoneNumbController.text;
final fileName = _image.path.split('/').last;
data['image'] = await MultipartFile.fromFile(
_image.path,
filename: fileName,
);
final post = await NetworkInterface().post(
url: '/informasi-fasilitas/lokasi/add/',
bodyParams: data,
filename: _image.path.split('/').last,
);
final post = await lokasiResponseBloc.addNewLokasi(data);
setState(() {
isLoading = false;
});
try {
if (post.containsKey('id')) {
if (post.toString().contains('id')) {
var alertDialog = AlertDialog(
title: Text('Tambah Lokasi berhasil'),
content: Icon(FontAwesomeIcons.checkCircle),
......
......@@ -7,9 +7,7 @@ class LokasiRepository {
final _cookie = CookiesInterface();
Future<LokasiListResponse> fetchLokasi() async {
final response = await _network.get(
url:
'/informasi-fasilitas/lokasi/list/',
isLogin: false);
url: '/informasi-fasilitas/lokasi/list/', isLogin: false);
return LokasiListResponse(
response.map<Lokasi>((lokasi) => Lokasi.fromJson(lokasi)).toList());
......@@ -25,4 +23,10 @@ class LokasiRepository {
final searchToMap = recentSearch.toJson();
await _cookie.createSearchHistoryCookie(recentSearch: searchToMap);
}
Future<dynamic> addNewLokasi(Map<String, dynamic> lokasi) async {
final response = await _network.post(
url: '/informasi-fasilitas/lokasi/add/', bodyParams: lokasi);
return response;
}
}
import 'package:bisaGo/component/bisago_appbar.dart';
import 'package:bisaGo/page/addLokasi/add_lokasi.dart';
import 'package:bisaGo/page/pencarian/pencarian.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() {
Widget buildTestableWidget(Widget widget) {
// https://docs.flutter.io/flutter/widgets/MediaQuery-class.html
return MediaQuery(data: MediaQueryData(), child: MaterialApp(home: widget));
}
testWidgets('find add lokasi page', (WidgetTester tester) async {
await tester.pumpWidget(buildTestableWidget(AddLokasi()));
});
testWidgets('validate empty nama lokasi test', (WidgetTester tester) async {
var result = FieldValidator.validateNamaLokasi('');
expect(result, '*Wajib diisi');
});
testWidgets('validate empty alamat test', (WidgetTester tester) async {
var result = FieldValidator.validateAlamat('');
expect(result, '*Wajib diisi');
});
testWidgets('validate empty phone number test', (WidgetTester tester) async {
var result = FieldValidator.validatePhoneNumb('');
expect(result, '*Wajib diisi');
});
testWidgets('validate empty latitude test', (WidgetTester tester) async {
var result = FieldValidator.validateLatitude('');
expect(result, '*Wajib diisi');
});
testWidgets('validate empty langitude test', (WidgetTester tester) async {
var result = FieldValidator.validateLangitude('');
expect(result, '*Wajib diisi');
});
testWidgets('tap search field in add lokasi page',
(WidgetTester tester) async {
await tester.pumpWidget(buildTestableWidget(AddLokasi()));
final bisaGoAppBar = find.byType(BisaGoAppBar);
await tester.tap(bisaGoAppBar);
await tester.pumpWidget(buildTestableWidget(Pencarian()));
});
testWidgets('Add Lokasi State Page', (WidgetTester tester) async {
final addLokasiPage = AddLokasi();
final addLokasiState = addLokasiPage.createState();
addLokasiState.currentLatLng = LatLng(5.0, 5.0);
addLokasiState.addLokasiModal();
addLokasiState.resetData();
addLokasiState.checkAvailabilityofUserLocation();
});
testWidgets('Back To Pencarian From Add Lokasi', (WidgetTester tester) async {
final backButtonToPencarian = find.byIcon(Icons.arrow_back_ios);
await tester.pumpWidget(buildTestableWidget(AddLokasi()));
await tester.tap(backButtonToPencarian);
});
// testWidgets('Location Prediction Test', (WidgetTester tester) async {
// final inkwell = find.byKey(Key('InkWell'));
// await tester.pumpWidget(buildTestableWidget(AddLokasi()));
// await tester.tap(inkwell);
// expect(inkwell, findsOneWidget);
// });
}
......@@ -62,10 +62,10 @@ void main() {
await tester.tap(backButtonToPencarian);
});
// testWidgets('Location Prediction Test', (WidgetTester tester) async {
// final inkwell = find.byKey(Key('InkWell'));
// await tester.pumpWidget(buildTestableWidget(AddLokasi()));
// await tester.tap(inkwell);
// expect(inkwell, findsOneWidget);
// });
testWidgets('Location Prediction Test', (WidgetTester tester) async {
final inkwell = find.byKey(Key('InkWell'));
await tester.pumpWidget(buildTestableWidget(AddLokasi()));
await tester.tap(inkwell);
expect(inkwell, findsOneWidget);
});
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment