Fakultas Ilmu Komputer UI

Verified Commit 26b89a44 authored by Muhammad Ariq Basyar's avatar Muhammad Ariq Basyar
Browse files

[CHORES] refactors and fix bugs:

- fix bug select dissability remove profile picture
- removed new_user_bloc (duplicate with user_bloc) and NewUser model
- added new model DetailUserModel, RegisterUserModel, UpdateUsermodel
- now shared preferences store user json with key 'user'
parent f0b6c945
Pipeline #81005 passed with stages
in 16 minutes and 51 seconds
import 'dart:async';
import 'package:bisaGo/model/new_user.dart';
import 'package:bisaGo/network/data/network_model.dart';
import 'package:bisaGo/repository/user_repository.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart';
class NewUserBloc {
UserRepository _userRepository;
StreamController _userController;
NewUserBloc() {
_userController = StreamController<NetworkModel<String>>();
_userRepository = GetIt.instance.get<BaseUserRepository>();
}
Future<bool> registerNewUser(NewUser newUser) async {
try {
await _userRepository.createUser(newUser);
return true;
} catch (_) {
return false;
}
}
Future<bool> updateUser(NewUser newUser) async {
try {
await _userRepository.updateUser(newUser);
return true;
} catch (_) {
return false;
}
}
Future<Response> updateUserPFP(Map<String, dynamic> newUserData) async {
try {
await _userRepository.updateUserProfile(newUserData);
return Response('Success', 200);
} catch (_) {
return Response('Failed to update user', 400);
}
}
void dispose() {
_userController?.close();
}
}
......@@ -4,49 +4,55 @@ import 'package:bisaGo/network/data/network_model.dart';
import 'package:bisaGo/repository/user_repository.dart';
import 'package:bisaGo/model/user.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart';
class UserBloc {
UserRepository _userRepository;
StreamController _userController;
List<UserModel> userFromApi;
String email;
StreamSink<NetworkModel<User>> get userSink => _userController.sink;
Stream<NetworkModel<User>> get userStream => _userController.stream;
StreamSink<NetworkModel<DetailUserModel>> get userSink =>
_userController.sink;
UserBloc(String email) {
_userController = StreamController<NetworkModel<User>>();
Stream<NetworkModel<DetailUserModel>> get userStream =>
_userController.stream;
UserBloc({this.email}) {
_userController = StreamController<NetworkModel<DetailUserModel>>();
_userRepository = GetIt.instance.get<BaseUserRepository>();
fetchUserDetail(email);
}
Future<void> fetchUserDetail(String email) async {
Future<void> fetchUserDetail() async {
userSink.add(NetworkModel.loading('Getting user'));
final response = await getUserDetail(email);
if (response is DetailUserModel) {
userSink.add(NetworkModel.completed(response));
} else {
userSink.add(NetworkModel.error(response.toString()));
}
}
Future<dynamic> getUserDetail(String email) async {
try {
final userResponse = await _userRepository.fetchUserDetail(email);
userFromApi = List.from(userResponse.user);
userSink.add(NetworkModel.completed(userResponse));
return await _userRepository.fetchUserDetail(email);
} catch (e) {
if (!_userController.isClosed) {
userSink.add(NetworkModel.error(e.toString()));
}
return e;
}
}
Future<UserModel> fetchUser(String email) async {
Future<DetailUserModel> updateUser(UpdateUserModel updateUser) async {
try {
await fetchUserDetail(email);
return userFromApi.first;
} catch (e) {
return await _userRepository.updateUser(updateUser, email);
} catch (_) {
return null;
}
}
Future<Response> updateUserProfile(Map<String, dynamic> newUserData) async {
Future<bool> registerNewUser(RegisterUserModel registerUserModel) async {
try {
return await _userRepository.updateUserProfile(newUserData);
} catch (e) {
return Response('Failed to update user', 400);
await _userRepository.registerUser(registerUserModel);
return true;
} catch (_) {
return false;
}
}
......
import 'dart:convert';
import 'package:bisaGo/page/informasi/list_sekolah.dart';
import 'package:bisaGo/page/profile/profile.dart';
import 'package:bisaGo/page/tentang_disabilitas/tentang_disabilitas.dart';
......@@ -168,7 +170,8 @@ class BisaGoDrawer extends StatelessWidget {
if (sharedPreferences.getString('token') == null) {
return 'Selamat datang ke BisaGo!';
} else {
return sharedPreferences.getString('email');
final userJson = jsonDecode(sharedPreferences.getString('user'));
return userJson['email'];
}
}
......
import 'package:json_annotation/json_annotation.dart';
part 'new_user.g.dart';
@JsonSerializable()
class NewUser {
String name;
String password;
String email;
@JsonKey(name: 'tanggal_lahir')
String tanggalLahir;
@JsonKey(name: 'jenis_kelamin')
String jenisKelamin;
String disabilitas;
String pekerjaan;
String alamat;
@JsonKey(name: 'phone_number')
String phoneNumber;
String foto;
bool seen;
@JsonKey(name: 'organisasi_komunitas')
String organisasiKomunitas;
NewUser({
this.name,
this.password,
this.email,
this.tanggalLahir,
this.jenisKelamin,
this.disabilitas,
this.pekerjaan,
this.alamat,
this.phoneNumber,
this.foto,
this.seen,
this.organisasiKomunitas,
});
factory NewUser.fromJson(Map<String, dynamic> json) =>
_$NewUserFromJson(json);
Map<String, dynamic> toJson() => _$NewUserToJson(this);
}
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'new_user.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
NewUser _$NewUserFromJson(Map<String, dynamic> json) {
return NewUser(
name: json['name'] as String,
password: json['password'] as String,
email: json['email'] as String,
tanggalLahir: json['tanggal_lahir'] as String,
jenisKelamin: json['jenis_kelamin'] as String,
disabilitas: json['disabilitas'] as String,
pekerjaan: json['pekerjaan'] as String,
alamat: json['alamat'] as String,
phoneNumber: json['phone_number'] as String,
foto: json['foto'] as String,
seen: json['seen'] as bool,
organisasiKomunitas: json['organisasi_komunitas'] as String,
);
}
Map<String, dynamic> _$NewUserToJson(NewUser instance) => <String, dynamic>{
'name': instance.name,
'password': instance.password,
'email': instance.email,
'tanggal_lahir': instance.tanggalLahir,
'jenis_kelamin': instance.jenisKelamin,
'disabilitas': instance.disabilitas,
'pekerjaan': instance.pekerjaan,
'alamat': instance.alamat,
'phone_number': instance.phoneNumber,
'foto': instance.foto,
'seen': instance.seen,
'organisasi_komunitas': instance.organisasiKomunitas,
};
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable()
class User {
final List<UserModel> user;
User(this.user);
}
part 'user.g.dart';
@JsonSerializable()
class UserModel {
bool is_login;
String username;
abstract class BaseUserModel {
String name;
String email;
@JsonKey(name: 'tanggal_lahir')
String tanggalLahir;
@JsonKey(name: 'phone_number')
......@@ -22,32 +13,130 @@ class UserModel {
String disabilitas;
String pekerjaan;
String alamat;
String token;
String foto;
bool seen;
@JsonKey(name: 'hidden_fields')
List<String> hiddenFields;
dynamic foto;
@JsonKey(name: 'organisasi_komunitas')
String organisasiKomunitas;
UserModel({
this.is_login,
this.username,
BaseUserModel({
this.name,
this.email,
this.tanggalLahir,
this.phoneNumber,
this.jenisKelamin,
this.disabilitas,
this.pekerjaan,
this.alamat,
this.token,
this.foto,
this.seen,
this.organisasiKomunitas,
});
}
@JsonSerializable()
class RegisterUserModel extends BaseUserModel {
String email;
String password;
RegisterUserModel({
this.email,
this.password,
name,
tanggalLahir,
phoneNumber,
jenisKelamin,
disabilitas,
pekerjaan,
alamat,
foto,
organisasiKomunitas,
}) : super(
name: name,
tanggalLahir: tanggalLahir,
phoneNumber: phoneNumber,
jenisKelamin: jenisKelamin,
disabilitas: disabilitas,
pekerjaan: pekerjaan,
alamat: alamat,
foto: foto,
organisasiKomunitas: organisasiKomunitas);
factory RegisterUserModel.fromJson(Map<String, dynamic> json) =>
_$RegisterUserModelFromJson(json);
Map<String, dynamic> toJson() => _$RegisterUserModelToJson(this);
}
@JsonSerializable()
class UpdateUserModel extends BaseUserModel {
bool seen;
UpdateUserModel({
this.seen,
name,
tanggalLahir,
phoneNumber,
jenisKelamin,
disabilitas,
pekerjaan,
alamat,
foto,
organisasiKomunitas,
}) : super(
name: name,
tanggalLahir: tanggalLahir,
phoneNumber: phoneNumber,
jenisKelamin: jenisKelamin,
disabilitas: disabilitas,
pekerjaan: pekerjaan,
alamat: alamat,
foto: foto,
organisasiKomunitas: organisasiKomunitas);
factory UpdateUserModel.fromJson(Map<String, dynamic> json) =>
_$UpdateUserModelFromJson(json);
Map<String, dynamic> toJson() => _$UpdateUserModelToJson(this);
}
@JsonSerializable()
class DetailUserModel extends BaseUserModel {
String username;
String email;
bool seen;
@JsonKey(name: 'hidden_fields')
List<String> hiddenFields;
DetailUserModel({
this.username,
this.email,
this.seen,
name,
tanggalLahir,
phoneNumber,
jenisKelamin,
disabilitas,
pekerjaan,
alamat,
foto,
organisasiKomunitas,
}) : super(
name: name,
tanggalLahir: tanggalLahir,
phoneNumber: phoneNumber,
jenisKelamin: jenisKelamin,
disabilitas: disabilitas,
pekerjaan: pekerjaan,
alamat: alamat,
foto: foto,
organisasiKomunitas: organisasiKomunitas);
UpdateUserModel toUpdateUserModel() {
var thisData = toJson();
thisData.remove('username');
thisData.remove('email');
return UpdateUserModel.fromJson(thisData);
}
factory DetailUserModel.fromJson(Map<String, dynamic> json) =>
_$DetailUserModelFromJson(json);
factory UserModel.fromJson(Map<String, dynamic> json) =>
_$UserModelFromJson(json);
Map<String, dynamic> toJson() => _$UserModelToJson(this);
Map<String, dynamic> toJson() => _$DetailUserModelToJson(this);
}
......@@ -6,53 +6,97 @@ part of 'user.dart';
// JsonSerializableGenerator
// **************************************************************************
User _$UserFromJson(Map<String, dynamic> json) {
return User(
(json['user'] as List)
?.map((e) =>
e == null ? null : UserModel.fromJson(e as Map<String, dynamic>))
?.toList(),
RegisterUserModel _$RegisterUserModelFromJson(Map<String, dynamic> json) {
return RegisterUserModel(
email: json['email'] as String,
password: json['password'] as String,
name: json['name'],
tanggalLahir: json['tanggal_lahir'],
phoneNumber: json['phone_number'],
jenisKelamin: json['jenis_kelamin'],
disabilitas: json['disabilitas'],
pekerjaan: json['pekerjaan'],
alamat: json['alamat'],
foto: json['foto'],
organisasiKomunitas: json['organisasi_komunitas'],
);
}
Map<String, dynamic> _$UserToJson(User instance) => <String, dynamic>{
'user': instance.user,
Map<String, dynamic> _$RegisterUserModelToJson(RegisterUserModel instance) =>
<String, dynamic>{
'name': instance.name,
'tanggal_lahir': instance.tanggalLahir,
'phone_number': instance.phoneNumber,
'jenis_kelamin': instance.jenisKelamin,
'disabilitas': instance.disabilitas,
'pekerjaan': instance.pekerjaan,
'alamat': instance.alamat,
'foto': instance.foto,
'organisasi_komunitas': instance.organisasiKomunitas,
'email': instance.email,
'password': instance.password,
};
UserModel _$UserModelFromJson(Map<String, dynamic> json) {
return UserModel(
is_login: json['is_login'] as bool,
UpdateUserModel _$UpdateUserModelFromJson(Map<String, dynamic> json) {
return UpdateUserModel(
seen: json['seen'] as bool,
name: json['name'],
tanggalLahir: json['tanggal_lahir'],
phoneNumber: json['phone_number'],
jenisKelamin: json['jenis_kelamin'],
disabilitas: json['disabilitas'],
pekerjaan: json['pekerjaan'],
alamat: json['alamat'],
foto: json['foto'],
organisasiKomunitas: json['organisasi_komunitas'],
);
}
Map<String, dynamic> _$UpdateUserModelToJson(UpdateUserModel instance) =>
<String, dynamic>{
'name': instance.name,
'tanggal_lahir': instance.tanggalLahir,
'phone_number': instance.phoneNumber,
'jenis_kelamin': instance.jenisKelamin,
'disabilitas': instance.disabilitas,
'pekerjaan': instance.pekerjaan,
'alamat': instance.alamat,
'foto': instance.foto,
'organisasi_komunitas': instance.organisasiKomunitas,
'seen': instance.seen,
};
DetailUserModel _$DetailUserModelFromJson(Map<String, dynamic> json) {
return DetailUserModel(
username: json['username'] as String,
name: json['name'] as String,
email: json['email'] as String,
tanggalLahir: json['tanggal_lahir'] as String,
phoneNumber: json['phone_number'] as String,
jenisKelamin: json['jenis_kelamin'] as String,
disabilitas: json['disabilitas'] as String,
pekerjaan: json['pekerjaan'] as String,
alamat: json['alamat'] as String,
token: json['token'] as String,
foto: json['foto'] as String,
seen: json['seen'] as bool,
organisasiKomunitas: json['organisasi_komunitas'] as String,
name: json['name'],
tanggalLahir: json['tanggal_lahir'],
phoneNumber: json['phone_number'],
jenisKelamin: json['jenis_kelamin'],
disabilitas: json['disabilitas'],
pekerjaan: json['pekerjaan'],
alamat: json['alamat'],
foto: json['foto'],
organisasiKomunitas: json['organisasi_komunitas'],
)..hiddenFields =
(json['hidden_fields'] as List)?.map((e) => e as String)?.toList();
}
Map<String, dynamic> _$UserModelToJson(UserModel instance) => <String, dynamic>{
'is_login': instance.is_login,
'username': instance.username,
Map<String, dynamic> _$DetailUserModelToJson(DetailUserModel instance) =>
<String, dynamic>{
'name': instance.name,
'email': instance.email,
'tanggal_lahir': instance.tanggalLahir,
'phone_number': instance.phoneNumber,
'jenis_kelamin': instance.jenisKelamin,
'disabilitas': instance.disabilitas,
'pekerjaan': instance.pekerjaan,
'alamat': instance.alamat,
'token': instance.token,
'foto': instance.foto,
'organisasi_komunitas': instance.organisasiKomunitas,
'username': instance.username,
'email': instance.email,
'seen': instance.seen,
'hidden_fields': instance.hiddenFields,
'organisasi_komunitas': instance.organisasiKomunitas,
};
......@@ -7,6 +7,10 @@ import 'package:bisaGo/flavor/flavor.dart';
import 'package:shared_preferences/shared_preferences.dart';
class NetworkInterface {
NetworkInterface() {
dio.options.headers['content-type'] = 'application/json';
}
Future<dynamic> post({
String url,
Map<String, dynamic> bodyParams,
......@@ -15,12 +19,7 @@ class NetworkInterface {
}) async {
var responseJson;
try {
if (isLogin) {
final sharedPreferences = await SharedPreferences.getInstance();
dio.options.headers['Authorization'] =
'Token ${sharedPreferences.getString('token')}';
dio.options.headers['content-type'] = 'application/json';
}
await setToken(isLogin);
final response = await dio.post(
'${ApiFlavor.getBaseUrl()}$url',
data: formData ? FormData.fromMap(bodyParams) : json.encode(bodyParams),
......@@ -42,12 +41,7 @@ class NetworkInterface {
}) async {
var responseJson;
try {
if (isLogin) {
final sharedPreferences = await SharedPreferences.getInstance();
dio.options.headers['Authorization'] =
'Token ${sharedPreferences.getString('token')}';
}
dio.options.headers['content-type'] = 'application/json';
await setToken(isLogin);
final response = await dio.put(
'${ApiFlavor.getBaseUrl()}$url',
data: formData ? FormData.fromMap(bodyParams) : json.encode(bodyParams),
......@@ -69,7 +63,7 @@ class NetworkInterface {
}) async {
var responseJson;
try {
dio.options.headers['content-type'] = 'application/json';
await setToken(isLogin);
final response = await dio.get(
'${ApiFlavor.getBaseUrl()}$url',
);
......@@ -82,6 +76,16 @@ class NetworkInterface {
return responseJson;
}
Future<void> setToken(bool isLogin) async {
if (isLogin) {
final sharedPreferences = await SharedPreferences.getInstance();
dio.options.headers['Authorization'] =
'Token ${sharedPreferences.getString('token')}';
} else {
dio.options.headers.remove('Authorization');
}
}
dynamic _response(Response response) {
switch (response.statusCode) {
case 200:
......
......@@ -46,7 +46,6 @@ class _DetailPostKegiatanPageState extends State<DetailPostKegiatanPage> {
@override
Widget build(BuildContext context) {
print('asd ${widget.kegiatan.toJson()}');
return Scaffold(
appBar: BisaGoAppBar(
title: widget.lokasi.name,
......
import 'dart:async';
import 'dart:convert';
import 'package:bisaGo/bloc/cloud_messaging_bloc.dart';
import 'package:bisaGo/bloc/new_user_bloc.dart';
import 'package:bisaGo/bloc/user_bloc.dart';
import 'package:bisaGo/model/new_user.dart';
import 'package:bisaGo/model/user.dart';
import 'package:bisaGo/page/dashboard/dashboard.dart';
import 'package:bisaGo/page/login/pilih_disabilitas.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';