Skip to content

Commit

Permalink
- added game detail page
Browse files Browse the repository at this point in the history
 - added game comments
  • Loading branch information
maliksenpai committed Dec 16, 2021
1 parent b40c7cc commit 57ef792
Show file tree
Hide file tree
Showing 11 changed files with 431 additions and 16 deletions.
40 changes: 40 additions & 0 deletions lib/data/firebase/firebase_game.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import 'dart:convert';

import 'package:findgamemates/data/firebase/firebase_log.dart';
import 'package:findgamemates/model/database_response.dart';
import 'package:findgamemates/model/game_comment.dart';
import 'package:findgamemates/model/game_post.dart';
import 'package:findgamemates/model/game_types.dart';
import 'package:findgamemates/utils/log_utils.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class FirebaseGame extends GetxService{

late DatabaseReference databaseReference;
late DatabaseReference commentsReference;
late FirebaseAuth firebaseAuth;
FirebaseLog firebaseLog = Get.put(FirebaseLog());

@override
void onInit() {
databaseReference = FirebaseDatabase.instance.reference().child("games");
commentsReference = FirebaseDatabase.instance.reference().child("comments");
firebaseAuth = FirebaseAuth.instance;
}

Expand Down Expand Up @@ -44,4 +50,38 @@ class FirebaseGame extends GetxService{
return gameList;
}

Future<List<GameComment>> getComments(String postId) async{
List<GameComment> commentList = [];
var postRef = commentsReference.child(postId);
await postRef.orderByChild("active").equalTo(true).once().then((value){
try{
var mapEntry = value.value.entries.toList();
mapEntry.forEach((data){
Map<String, dynamic> json = Map<String, dynamic>.from(data.value);
GameComment gameComment = GameComment.fromJson(json);
commentList.add(gameComment);
});
}catch(e){
debugPrint(e.toString());
return [];
}
});
return commentList;
}

Future<GameComment> addComment(String postId, String comment) async{
DatabaseReference dbRef = commentsReference.child(postId).push();
//todo: sendername ekle
GameComment gameComment = GameComment(
id: dbRef.key,
postId: postId,
senderId: firebaseAuth.currentUser!.uid,
senderName: "asd",
sendTime: DateTime.now().toUtc().toString(),
comment: comment,
active: true
);
await dbRef.set(gameComment.toJson());
return gameComment;
}
}
19 changes: 19 additions & 0 deletions lib/get/game_get.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:findgamemates/data/database/user_database.dart';
import 'package:findgamemates/data/firebase/firebase_game.dart';
import 'package:findgamemates/data/firebase/firebase_log.dart';
import 'package:findgamemates/model/database_response.dart';
import 'package:findgamemates/model/game_comment.dart';
import 'package:findgamemates/model/game_post.dart';
import 'package:findgamemates/model/game_types.dart';
import 'package:findgamemates/utils/log_utils.dart';
Expand All @@ -14,6 +15,8 @@ class GameGet extends GetxController {
Rx<List<GamePost>> postList = Rx([]);
List<GamePost> notFilteredList = [];

Rx<List<GameComment>?> currComments = Rx(null);

@override
void onInit() async {
if (postList.value.isEmpty) {
Expand Down Expand Up @@ -48,4 +51,20 @@ class GameGet extends GetxController {
}
return databaseResponse.result;
}

Future getListComment(String postId) async {
currComments.value = await firebaseGame.getComments(postId);
currComments.refresh();
}

Future<GameComment?> addComment(String postId, String comment) async{
try{
GameComment gameComment = await firebaseGame.addComment(postId, comment);
currComments.value!.add(gameComment);
currComments.refresh();
return gameComment;
}catch(e){
return null;
}
}
}
43 changes: 43 additions & 0 deletions lib/model/game_comment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class GameComment{

String id;
String postId;
String senderId;
String senderName;
String sendTime;
String comment;
bool active;

GameComment({
required this.id,
required this.postId,
required this.senderId,
required this.senderName,
required this.sendTime,
required this.comment,
required this.active
});

Map<String, dynamic> toJson() => {
"id" : id,
"postId" :postId,
"senderId" :senderId,
"senderName": senderName,
"sendTime" : sendTime,
"comment" :comment,
"active": active
};

static fromJson(Map<String, dynamic> json){
return GameComment(
id: json["id"],
postId: json["postId"],
senderId: json["senderId"],
senderName: json["senderName"],
sendTime: json["sendTime"],
comment: json["comment"],
active: json["active"]
);
}

}
131 changes: 129 additions & 2 deletions lib/screen/game_detail_screen.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,142 @@
import 'package:findgamemates/model/game_post.dart';
import 'package:findgamemates/model/game_types.dart';
import 'package:findgamemates/view/game_detail_comments.dart';
import 'package:findgamemates/view/game_detail_input.dart';
import 'package:flutter/material.dart';

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

GamePost gamePost;

GameDetailScreen({Key? key, required this.gamePost}) : super(key: key);

@override
_GameDetailScreenState createState() => _GameDetailScreenState();
}

class _GameDetailScreenState extends State<GameDetailScreen> {

late String gameType;

@override
void initState() {
gameType = widget.gamePost.gameType == GameType.frp
? "FRP"
: widget.gamePost.gameType == GameType.boardGame
? "Kutu oyunu"
: widget.gamePost.gameType == GameType.tcg
? "TCG"
: "Hata";
super.initState();
}

@override
void didChangeDependencies() {
gameType = widget.gamePost.gameType == GameType.frp
? "FRP"
: widget.gamePost.gameType == GameType.boardGame
? "Kutu oyunu"
: widget.gamePost.gameType == GameType.tcg
? "TCG"
: "Hata";
super.didChangeDependencies();
}

@override
Widget build(BuildContext context) {
return Container();
return Scaffold(
key: widget.key,
appBar: AppBar(title: Text(widget.gamePost.title),),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: NotificationListener<OverscrollIndicatorNotification>(
onNotification: (overScroll){
overScroll.disallowGlow();
return false;
},
child: ListView(
children: [
Text(
widget.gamePost.title,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 22,
),
),
const Divider(),
Card(
elevation: 12,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
widget.gamePost.desc,
),
),
const Divider(),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.extension,
color: Theme.of(context).primaryColor,
size: 20,
),
const SizedBox(
width: 4,
),
Text(
gameType,
style: const TextTheme().bodyText2,
),
],
),
),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.location_on,
size: 20,
),
const SizedBox(
width: 4,
),
Text(
widget.gamePost.province,
style: const TextTheme().bodyText2,
),
],
),
),
],
),
)
],
),
),
Card(
elevation: 12,
child: GameDetailComments(gamePostId: widget.gamePost.id, key: UniqueKey(),),
),
Card(
elevation: 12,
child: GameDetailInput(postId: widget.gamePost.id,),
)
],
),
),
),
),
);
}
}
9 changes: 0 additions & 9 deletions lib/screen/games_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,10 @@ class GamesScreen extends StatefulWidget {
}

class _GamesScreenState extends State<GamesScreen> {
late List<GamePost> list = [];
GameGet gameGet = Get.put(GameGet());

@override
void initState() {
if (kDebugMode) {
//todo: mock data
list.add(GamePost(id: "asd", createrId: "asd", createTime: "12312313", title: "asd", desc: "sdfs", gameType: GameType.frp, province: "Ankara", active: true));
list.add(GamePost(id: "asd", createrId: "asd", createTime: "12312313", title: "asd", desc: "sdfs", gameType: GameType.frp, province: "Ankara", active: true));
list.add(GamePost(id: "asd", createrId: "asd", createTime: "12312313", title: "asd", desc: "sdfs", gameType: GameType.frp, province: "Ankara", active: true));
list.add(GamePost(id: "asd", createrId: "asd", createTime: "12312313", title: "asd", desc: "sdfs", gameType: GameType.frp, province: "Ankara", active: true));
list.add(GamePost(id: "asd", createrId: "asd", createTime: "12312313", title: "asd", desc: "sdfs", gameType: GameType.frp, province: "Ankara", active: true));
}
super.initState();
}

Expand Down
1 change: 1 addition & 0 deletions lib/screen/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
primary: Colors.redAccent
),
onPressed: () async {
//todo:refactor dialog like game detail input
Get.dialog(
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
Expand Down
63 changes: 63 additions & 0 deletions lib/view/comment_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:findgamemates/model/game_comment.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class CommentWidget extends StatefulWidget {

GameComment gameComment;

CommentWidget({Key? key, required this.gameComment}) : super(key: key);

@override
_CommentWidgetState createState() => _CommentWidgetState();
}

class _CommentWidgetState extends State<CommentWidget> {

late String date;

@override
void initState() {

super.initState();
}

@override
Widget build(BuildContext context) {
return Card(
elevation: 8,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16,horizontal: 8),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.symmetric(horizontal: 12),
alignment: Alignment.centerLeft,
child: Text(
widget.gameComment.senderName,
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 4),
alignment: Alignment.centerLeft,
child: Text(
widget.gameComment.comment
),
),
Container(
padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 4),
alignment: Alignment.bottomRight,
child: Text(
//todo: check local date time
DateFormat("dd/MM/yyyy HH:mm").format(DateTime.parse(widget.gameComment.sendTime).toLocal()),
style: const TextStyle(color: Colors.grey),
),
)
],
),
),
);
}
}
Loading

0 comments on commit 57ef792

Please sign in to comment.