Skip to content

Commit

Permalink
Merge pull request #90 from fanzru/staging
Browse files Browse the repository at this point in the history
Bump Main for v0.10
  • Loading branch information
kaenova authored Jun 22, 2022
2 parents d0369fa + 372d979 commit 11296dc
Show file tree
Hide file tree
Showing 18 changed files with 626 additions and 83 deletions.
37 changes: 37 additions & 0 deletions mobile/lib/api/komentar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:mobile/model/komentar.dart';
import 'package:mobile/model/profile_secure.dart';
import 'package:mobile/model/user.dart';

Future<Komentar> postKomentar(int reviewId, String komentar) async {
var profile = await SecureProfile.getStorage();
var formData = FormData.fromMap({"komentar": komentar});
try {
var response = await Dio()
.post("https://travelliu.yaudahlah.my.id/api/komentar/$reviewId",
options: Options(
headers: {
'Authorization': 'Bearer ${profile.getApiKey()}',
"Accept": "application/json",
},
),
data: formData);
var data = response.data;
var user = User.empty(name: profile.name!);
return Komentar(id: data["id"] as int, komentar: komentar, user: user);
} on DioError catch (e) {
if (e.response != null) {
var response = e.response!;
if (response.statusCode == 401) {
await profile.setLoggedOut();
return Future.error("Session expired");
}
if (response.statusCode == 400) {
return Future.error(response.data);
}
}
return Future.error("Gagal untuk mempost review");
}
}
16 changes: 15 additions & 1 deletion mobile/lib/api/review.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@ Future<List<Review>> getAllReview() async {
List decoded = jsonDecode(response.body);
List<Review> reviews = [];
for (var review in decoded) {
reviews.add(Review.fromJson(review, true));
reviews.add(Review.fromJson(review));
}
return reviews;
} else {
return Future.error('Failed to load all reviews');
}
}

Future<Review> getReviewById(int id) async {
try {
var response = await http
.get(Uri.parse("https://travelliu.yaudahlah.my.id/api/review/$id"));
if (response.statusCode == 200) {
return Review.fromJson(jsonDecode(response.body));
}
Future.error("Tidak dapat mengambil review");
} catch (e) {
return Future.error(e);
}
return Future.error("Tidak dapat mengambil review");
}

Future<void> createReview(
{required String nama,
required String alamat,
Expand Down
3 changes: 2 additions & 1 deletion mobile/lib/api/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ Future<void> loginUser(String email, String password) async {
var decoded = jsonDecode(response.body);
var token = decoded["token"];
var id = decoded["user"]["id"];
var name = decoded["user"]["name"];

var profile = await SecureProfile.getStorage();
await profile.setLoggedIn(id, token);
await profile.setLoggedIn(id, token, name);
} else if (response.statusCode == 400) {
return Future.error("Email atau password salah");
} else {
Expand Down
2 changes: 1 addition & 1 deletion mobile/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MyApp extends StatelessWidget {
),
routes: {
HomeScreen.routeName: (context) => const HomeScreen(),
ReviewDetailScreen.routeName: (context) => const ReviewDetailScreen(),
ReviewDetailScreen.routeName: (context) => ReviewDetailScreen(),
FormReviewScreen.routeName: (context) => const FormReviewScreen(),
FormTikumScreen.routeName: (context) => const FormTikumScreen(),
RegisterScreen.routeName: (context) => RegisterScreen(),
Expand Down
14 changes: 14 additions & 0 deletions mobile/lib/model/komentar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:mobile/model/user.dart';

class Komentar {
final int id;
final String komentar;
User? user;

Komentar({required this.id, required this.komentar, this.user});

factory Komentar.fromJson(Map<String, dynamic> json) {
var user = User.empty(id: json["user"]["id"], name: json["user"]["name"]);
return Komentar(id: json["id"], komentar: json["komentar"], user: user);
}
}
26 changes: 17 additions & 9 deletions mobile/lib/model/profile_secure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,32 @@ class SecureProfile {

bool isLoggedIn;
int? userId;
String? apiKey;
String? apiKey, name;

SecureProfile(
{required this.storage,
required this.isLoggedIn,
this.userId,
this.apiKey});
this.apiKey,
this.name});

static Future<SecureProfile> getStorage() async {
var storage = await SharedPreferences.getInstance();
var userId = storage.getInt("user_id");
var apiKey = storage.getString("api_key");
var name = storage.getString("user_name");

bool loggedIn = false;

if ((userId == null || userId == -1) || (apiKey == null || apiKey == "")) {
await storage.setInt("user_id", -1);
await storage.setString("api_key", "");
} else {
if (userId != null && apiKey != null && name != null) {
loggedIn = true;
}

return SecureProfile(
storage: storage, isLoggedIn: loggedIn, apiKey: apiKey, userId: userId);
storage: storage,
isLoggedIn: loggedIn,
apiKey: apiKey,
userId: userId,
name: name);
}

bool getLoggedInStatus() {
Expand All @@ -63,15 +65,21 @@ class SecureProfile {
return userId;
}

Future<void> setLoggedIn(int userId, String apiKey) async {
String? getUserName() {
return name;
}

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

Future<void> setLoggedOut() async {
await storage.remove("api_key");
await storage.remove("user_id");
await storage.remove("user_name");
isLoggedIn = false;
}
}
30 changes: 18 additions & 12 deletions mobile/lib/model/review.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:mobile/model/komentar.dart';

import "./user.dart";

class Review {
Expand All @@ -6,8 +8,9 @@ class Review {
final String namaTempat, alamat, review, photo;
final User user;
final double? longitude, latitude;
List<Komentar> komentar = [];

const Review(
Review(
{required this.userId,
required this.id,
required this.photo,
Expand All @@ -18,17 +21,13 @@ class Review {
this.user = const User.empty(),
this.longitude,
this.latitude,
this.komentar = const [],
this.numKomentar = 0});

factory Review.fromJson(Map<String, dynamic> json, bool concatReview) {
factory Review.fromJson(Map<String, dynamic> json) {
var user = User.fromJson(json["user"]);
String review = json["review"];
if (concatReview) {
if (review.length > 200) {
review = review.substring(0, 195);
review = review + " ...";
}
}
List<Komentar> finalKomentar = [];
int komentarcount = 0;

if (json["latitude"] != null) {
if (json["latitude"] is int) {
Expand All @@ -46,18 +45,25 @@ class Review {
json["longitude"] = json["longitude"] as double;
}

if (json.containsKey("komentar")) {
List<dynamic> komentarTemp = json["komentar"];
for (var komentar in komentarTemp) {
finalKomentar.add(Komentar.fromJson(komentar));
}
}
return Review(
userId: json['user_id'],
id: json['id'],
alamat: json["alamat"],
namaTempat: json["nama_tempat"],
photo: json["photo"],
rating: json["rating"].toDouble(),
review: review,
review: json["review"],
latitude: json["latitude"],
longitude: json["longitude"],
numKomentar: json["komentar_count"],
user: user);
numKomentar: json["komentar_count"] ?? finalKomentar.length,
user: user,
komentar: finalKomentar);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'package:flutter/material.dart';
import 'package:mobile/screen/_global/components/shimmer/shimmer_container.dart';

class ShimmerDetailReview extends StatelessWidget {
const ShimmerDetailReview({Key? key}) : super(key: key);
final double textHeight = 10;

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const ShimmerContainer(
height: 200,
),
const SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(50),
child: const ShimmerContainer(
width: 40,
height: 40,
),
),
const SizedBox(
width: 15,
),
const ShimmerContainer(
width: 200,
)
],
),
const SizedBox(
height: 20,
),
const ShimmerContainer(
width: 150,
),
const SizedBox(
height: 20,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ShimmerContainer(
width: MediaQuery.of(context).size.width * .7,
height: textHeight,
),
const SizedBox(
height: 5,
),
ShimmerContainer(
height: textHeight,
width: MediaQuery.of(context).size.width * .8,
),
const SizedBox(
height: 5,
),
ShimmerContainer(
height: textHeight,
width: MediaQuery.of(context).size.width * .6,
),
const SizedBox(
height: 5,
),
ShimmerContainer(
height: textHeight,
width: MediaQuery.of(context).size.width * .9,
),
const SizedBox(
height: 5,
),
ShimmerContainer(
height: textHeight,
width: MediaQuery.of(context).size.width * .9,
),
],
),
const SizedBox(height: 20),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ShimmerContainer(
height: textHeight,
width: MediaQuery.of(context).size.width * .8,
),
const SizedBox(
height: 5,
),
ShimmerContainer(
width: MediaQuery.of(context).size.width * .6,
height: textHeight,
),
const SizedBox(
height: 5,
),
ShimmerContainer(
height: textHeight,
width: MediaQuery.of(context).size.width * .9,
),
const SizedBox(
height: 5,
),
ShimmerContainer(
height: textHeight,
width: MediaQuery.of(context).size.width * .9,
),
],
)
],
))
],
);
}
}
25 changes: 14 additions & 11 deletions mobile/lib/screen/form_login/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ class _LoginScreenState extends State<LoginScreen> {
padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 30),
child: Stack(
children: [
TextButton(
style: ButtonStyle(
overlayColor: MaterialStateProperty.all(Colors.black12)),
onPressed: _handleKembaliButton,
child: const Text(
"Kembali",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 15),
)),
Center(
child: SingleChildScrollView(
reverse: true,
Expand Down Expand Up @@ -217,6 +206,20 @@ class _LoginScreenState extends State<LoginScreen> {
),
),
),
ElevatedButton(
style: ButtonStyle(
overlayColor: MaterialStateProperty.all(Colors.black12),
backgroundColor: MaterialStateProperty.all(Colors.white),
),
onPressed: _handleKembaliButton,
child: const Text(
"Kembali",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 15),
),
),
],
),
)));
Expand Down
Loading

0 comments on commit 11296dc

Please sign in to comment.