Skip to content

Commit

Permalink
Merge pull request #83 from fanzru/kae_dev
Browse files Browse the repository at this point in the history
Tidak usah berpindah halaman sekarang :) + Refactoring jadi stateless widget + Fixing Shared Preferences Secure Profile
  • Loading branch information
kaenova authored Jun 16, 2022
2 parents 97198d7 + f0beadb commit bf8c353
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 165 deletions.
2 changes: 1 addition & 1 deletion mobile/lib/api/review.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Future<void> createReview(
if (e.response != null) {
var response = e.response!;
if (response.statusCode == 401) {
profile.setLoggedOut();
await profile.setLoggedOut();
return Future.error("Session expired");
}
if (response.statusCode == 400) {
Expand Down
6 changes: 3 additions & 3 deletions mobile/lib/api/tikum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Future<List<TikumProfile>> getMyTikum() async {
);

if (response.statusCode == 401) {
profile.setLoggedOut();
await profile.setLoggedOut();
return Future.error("Session expired");
}

Expand Down Expand Up @@ -64,7 +64,7 @@ Future<void> deleteMyTikum(int id) async {
);

if (response.statusCode == 401) {
profile.setLoggedOut();
await profile.setLoggedOut();
return Future.error("Session expired");
}

Expand Down Expand Up @@ -106,7 +106,7 @@ Future<void> createTikum({
if (e.response != null) {
var response = e.response!;
if (response.statusCode == 401) {
profile.setLoggedOut();
await profile.setLoggedOut();
return Future.error("Session expired");
}
if (response.statusCode == 400) {
Expand Down
12 changes: 6 additions & 6 deletions mobile/lib/api/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Future<void> loginUser(String email, String password) async {
var id = decoded["user"]["id"];

var profile = await SecureProfile.getStorage();
profile.setLoggedIn(id, token);
await profile.setLoggedIn(id, token);
} else if (response.statusCode == 400) {
return Future.error("Email atau password salah");
} else {
Expand Down Expand Up @@ -59,7 +59,7 @@ Future<List<ReviewProfile>> getMyReviewById() async {
);

if (response.statusCode == 401) {
profile.setLoggedOut();
await profile.setLoggedOut();
return Future.error("Session expired, please login again");
}

Expand Down Expand Up @@ -88,7 +88,7 @@ Future<Profile> getMyProfileById() async {
);

if (response.statusCode == 401) {
profile.setLoggedOut();
await profile.setLoggedOut();
return Future.error("Session expired, please login again");
}

Expand Down Expand Up @@ -118,7 +118,7 @@ Future<void> deleteMyReview(int id) async {
);

if (response.statusCode == 401) {
profile.setLoggedOut();
await profile.setLoggedOut();
return Future.error("Session expired, please login again");
}

Expand All @@ -144,15 +144,15 @@ Future<void> userLogout() async {
);

if (res.statusCode == 401) {
profile.setLoggedOut();
await profile.setLoggedOut();
return Future.error("Session expired, please login again");
}

if (res.statusCode != 200) {
return Future.error("Failed to logout");
}

profile.setLoggedOut();
await profile.setLoggedOut();
}

Future<Profile> getUserProfileById(int id) async {
Expand Down
4 changes: 2 additions & 2 deletions mobile/lib/model/profile_secure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ class SecureProfile {
return userId;
}

void setLoggedIn(int userId, String apiKey) async {
Future<void> setLoggedIn(int userId, String apiKey) async {
await storage.setInt("user_id", userId);
await storage.setString("api_key", apiKey);
isLoggedIn = true;
}

void setLoggedOut() async {
Future<void> setLoggedOut() async {
await storage.remove("api_key");
await storage.remove("user_id");
isLoggedIn = false;
Expand Down
2 changes: 1 addition & 1 deletion mobile/lib/screen/_global/components/profile_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ProfileCard extends StatelessWidget {
// ignore: prefer_const_literals_to_create_immutables
children: [
const Text("Rataan Rating"),
Text(data.avgRating.toString()),
Text(data.avgRating.toStringAsFixed(2)),
],
),
),
Expand Down
1 change: 0 additions & 1 deletion mobile/lib/screen/form_login/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:mobile/api/user.dart';
import 'package:mobile/screen/form_register/register_screen.dart';
import 'package:mobile/screen/home/home_screen.dart';
import 'package:mobile/utils/show_snackbar.dart';
import 'package:mobile/model/profile_secure.dart';

class LoginScreenArguments {
LoginScreenArguments();
Expand Down
2 changes: 0 additions & 2 deletions mobile/lib/screen/form_review/form_review_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
Expand Down
118 changes: 55 additions & 63 deletions mobile/lib/screen/home/components/profile/myprofile/card_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,59 @@ import 'package:mobile/screen/review_detail/review_detail_screen.dart';
import 'package:mobile/utils/show_snackbar.dart';

// final GlobalKey<NavigatorState> _navKey = GlobalKey<NavigatorState>();
class TimelineCard extends StatefulWidget {
final ReviewProfile data;

class MyReviewCard extends StatelessWidget {
final int randomForProfile = math.Random().nextInt(1000);
TimelineCard({Key? key, required this.data, int? randomForProfile})
final ReviewProfile data;
Function? refreshParent;
MyReviewCard({Key? key, required this.data, this.refreshParent})
: super(key: key);

@override
State<TimelineCard> createState() => _TimelineCardState();
}

class _TimelineCardState extends State<TimelineCard> {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
void _handleDelete() async {
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Hapus Review'),
content: const Text(
'Jika anda menghapus review ini akan hilang dari timeline umum dan profile anda!'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () async {
try {
await deleteMyReview(data.id);
ShowSnackBar(context, "Berhasil menghapus post");
if (refreshParent != null) {
refreshParent!();
}
} catch (err) {
ShowSnackBar(context, err.toString());
}
Navigator.pop(context, "OK");
},
child: const Text(
'OK',
style: TextStyle(
color: Colors.red,
),
),
),
],
),
);
}

return Container(
margin: const EdgeInsets.only(bottom: 5),
child: InkWell(
onTap: () {
Navigator.pushNamed(context, ReviewDetailScreen.routeName,
arguments: ReviewDetailScreenArguments(id: widget.data.id));
arguments: ReviewDetailScreenArguments(id: data.id));
},
child: Column(
children: [
Expand All @@ -45,64 +74,27 @@ class _TimelineCardState extends State<TimelineCard> {
borderRadius: BorderRadius.circular(50),
child: ImageNetworkWShimmer(
link:
"https://www.thiswaifudoesnotexist.net/example-${widget.randomForProfile}.jpg",
"https://www.thiswaifudoesnotexist.net/example-$randomForProfile.jpg",
width: 40,
height: 40,
),
),
const SizedBox(width: 10),
Text(widget.data.nameUser)
Text(data.nameUser)
]),
TextButton(
child: const Icon(
Icons.delete,
color: Colors.red,
),
onPressed: () => showDialog<String>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: const Text('Hapus Review'),
content: const Text(
'Jika anda menghapus review ini akan hilang dari timeline umum dan profile anda!'),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context, 'Cancel'),
child: const Text('Cancel'),
),
TextButton(
onPressed: () async {
try {
await deleteMyReview(widget.data.id);
ShowSnackBar(context,
"Berhasil menghapus post, harap pindah halaman untuk melihat perubahan");
} catch (err) {
ShowSnackBar(context, err.toString());
}
Navigator.pop(context, "OK");
},
child: const Text(
'OK',
style: TextStyle(
color: Colors.red,
),
),
),
],
child: const Icon(
Icons.delete,
color: Colors.red,
),
),
// onPressed: () async {
// setState(() {
// _futureStatus = deleteMyReview(widget.data.id);
// });
// },
),
onPressed: _handleDelete),
],
)),
const SizedBox(height: 10),
ImageNetworkWShimmer(
link: widget.data.photo[0] == "/"
? "https://travelliu.yaudahlah.my.id${widget.data.photo}"
: widget.data.photo),
link: data.photo[0] == "/"
? "https://travelliu.yaudahlah.my.id${data.photo}"
: data.photo),
Container(
margin: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
child: Column(
Expand All @@ -112,12 +104,12 @@ class _TimelineCardState extends State<TimelineCard> {
children: [
const Icon(Icons.star_border_outlined,
color: Colors.yellow),
Text(widget.data.rating.toString()),
Text(data.rating.toString()),
const SizedBox(
width: 10,
),
Text(
widget.data.namaTempat,
data.namaTempat,
style: const TextStyle(fontWeight: FontWeight.bold),
),
],
Expand All @@ -127,7 +119,7 @@ class _TimelineCardState extends State<TimelineCard> {
children: [
Expanded(
child: Text(
widget.data.review,
data.review,
textAlign: TextAlign.left,
))
],
Expand Down
46 changes: 32 additions & 14 deletions mobile/lib/screen/home/components/profile/myprofile/my_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,21 @@ class MyProfile extends StatelessWidget {
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: const [
SizedBox(
children: [
const SizedBox(
height: 10,
),
_ProfileSection(),
_ReviewSection(),
const _ReviewSection(),
],
),
);
}
}

class _ProfileSection extends StatefulWidget {
const _ProfileSection({Key? key}) : super(key: key);

@override
State<_ProfileSection> createState() => _ProfileSectionState();
}

class _ProfileSectionState extends State<_ProfileSection> {
Future<Profile> futureProfile = getMyProfileById();
class _ProfileSection extends StatelessWidget {
final Future<Profile> futureProfile = getMyProfileById();
_ProfileSection({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -101,7 +95,7 @@ class _ProfileSectionState extends State<_ProfileSection> {

return SizedBox(
height: MediaQuery.of(context).size.height * 0.4,
child: Center(child: ShimmerProfile()),
child: const Center(child: ShimmerProfile()),
);
},
),
Expand All @@ -120,6 +114,12 @@ class _ReviewSection extends StatefulWidget {
class __ReviewSectionState extends State<_ReviewSection> {
Future<List<ReviewProfile>> futureReview = getMyReviewById();

void refreshList() {
setState(() {
futureReview = getMyReviewById();
});
}

@override
Widget build(BuildContext context) {
return FutureBuilder<List<ReviewProfile>>(
Expand All @@ -129,11 +129,29 @@ class __ReviewSectionState extends State<_ReviewSection> {
return Center(child: Text(snapshot.error.toString()));
}
if (snapshot.hasData) {
if (snapshot.data!.isEmpty) {
return Center(
child: SizedBox(
width: 350,
child: Column(
children: const [
Text(
"Oops.. kamu masih belum membagikan apapun",
textAlign: TextAlign.center,
),
Text("Bagikan cerita perjalananmu sekarang!")
],
),
),
);
}

return Column(
children: [
for (var data in snapshot.data!)
TimelineCard(
MyReviewCard(
data: data,
refreshParent: refreshList,
)
],
);
Expand Down
Loading

0 comments on commit bf8c353

Please sign in to comment.