Fakultas Ilmu Komputer UI

Commit 80e608c4 authored by Muhammad Ariq Basyar's avatar Muhammad Ariq Basyar
Browse files

Merge branch 'dev-ariq-PBI-10' into 'PBI-10-edit_profile'

Finalisasi PBI 10

See merge request !57
parents f0b6c945 a88251ea
Pipeline #81253 passed with stages
in 14 minutes and 10 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('Gagal untuk mendapatkan profile'));
}
}
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;
}
}
......
......@@ -5,17 +5,24 @@ class BisaGoAppBar extends StatelessWidget implements PreferredSizeWidget {
final String title;
final List<Widget> actions;
final Widget leading;
final backgroundColor;
const BisaGoAppBar(
{this.title = 'bisaGo', this.actions = const [], this.leading, Key key})
{this.title = 'bisaGo',
this.actions = const [],
this.backgroundColor = greenPrimary,
this.leading,
Key key})
: super(key: key);
@override
final Size preferredSize = const Size.fromHeight(55);
@override
Widget build(BuildContext context) {
return AppBar(
elevation: 15,
centerTitle: true,
backgroundColor: greenPrimary,
backgroundColor: backgroundColor,
automaticallyImplyLeading: leading == null,
leading: leading,
title: Row(
......
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'];
}
}
......
......@@ -16,6 +16,8 @@ class KomentarPostingModel {
final String creator;
@JsonKey(name: 'creator_email')
final String creatorEmail;
@JsonKey(name: 'creator_picture')
final String creatorPicture;
@JsonKey(fromJson: CustomSerializer.stringToDateTime)
final DateTime created;
......@@ -24,6 +26,7 @@ class KomentarPostingModel {
this.deskripsi,
this.creator,
this.creatorEmail,
this.creatorPicture,
this.created,
});
......
......@@ -28,6 +28,7 @@ KomentarPostingModel _$KomentarPostingModelFromJson(Map<String, dynamic> json) {
deskripsi: json['deskripsi'] as String,
creator: json['creator'] as String,
creatorEmail: json['creator_email'] as String,
creatorPicture: json['creator_picture'] as String,
created: CustomSerializer.stringToDateTime(json['created'] as String),
);
}
......@@ -39,5 +40,6 @@ Map<String, dynamic> _$KomentarPostingModelToJson(
'deskripsi': instance.deskripsi,
'creator': instance.creator,
'creator_email': instance.creatorEmail,
'creator_picture': instance.creatorPicture,
'created': instance.created?.toIso8601String(),
};
......@@ -15,6 +15,8 @@ class KomentarPostingKegiatanModel {
final String creator;
@JsonKey(name: 'creator_email')
final String creatorEmail;
@JsonKey(name: 'creator_picture')
final String creatorPicture;
final String deskripsi;
@JsonKey(name: 'created', fromJson: _stringToDateTime)
final DateTime created;
......@@ -25,6 +27,7 @@ class KomentarPostingKegiatanModel {
this.deskripsi,
this.created,
this.creatorEmail,
this.creatorPicture,
});
factory KomentarPostingKegiatanModel.fromJson(Map<String, dynamic> json) =>
......
......@@ -31,6 +31,7 @@ KomentarPostingKegiatanModel _$KomentarPostingKegiatanModelFromJson(
deskripsi: json['deskripsi'] as String,
created: _stringToDateTime(json['created'] as String),
creatorEmail: json['creator_email'] as String,
creatorPicture: json['creator_picture'] as String,
);
}
......@@ -40,6 +41,7 @@ Map<String, dynamic> _$KomentarPostingKegiatanModelToJson(
'id': instance.id,
'creator': instance.creator,
'creator_email': instance.creatorEmail,
'creator_picture': instance.creatorPicture,
'deskripsi': instance.deskripsi,
'created': instance.created?.toIso8601String(),
};
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 'dart:ui';
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 +15,144 @@ 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;
@JsonKey(name: 'hidden_fields_verbose')
List<String> hiddenFieldsVerbose;
@JsonKey(name: 'can_see_hidden_fields')
bool canSeeHiddenFields;
@JsonKey(name: 'hidden_fields_color')
String hiddenFieldsColor;
DetailUserModel({
this.username,
this.email,
this.seen,
this.hiddenFields,
this.hiddenFieldsVerbose,
this.canSeeHiddenFields,
this.hiddenFieldsColor,
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();
return UpdateUserModel.fromJson(thisData);
}
Color getHiddenFieldsColor() {
final strColor = hiddenFieldsColor.replaceAll('#', '0xff');
final hexColor = int.parse(strColor);
return Color(hexColor);
}
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,106 @@ 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> _$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,
};
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> _$UserToJson(User instance) => <String, dynamic>{
'user': instance.user,
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,
};
UserModel _$UserModelFromJson(Map<String, dynamic> json) {
return UserModel(
is_login: json['is_login'] as bool,
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,
)..hiddenFields =
(json['hidden_fields'] as List)?.map((e) => e as String)?.toList();
hiddenFields:
(json['hidden_fields'] as List)?.map((e) => e as String)?.toList(),
hiddenFieldsVerbose: (json['hidden_fields_verbose'] as List)
?.map((e) => e as String)
?.toList(),
canSeeHiddenFields: json['can_see_hidden_fields'] as bool,
hiddenFieldsColor: json['hidden_fields_color'] as String,
name: json['name'],
tanggalLahir: json['tanggal_lahir'],
phoneNumber: json['phone_number'],