Skip to content

Commit

Permalink
Merge pull request #120 from 4Ailen/feat/#118_notiAPI
Browse files Browse the repository at this point in the history
Feat/#118 noti api
  • Loading branch information
urlotus authored Sep 1, 2023
2 parents 0a0cafe + dd65171 commit 8f0aca6
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 54 deletions.
33 changes: 33 additions & 0 deletions lib/apis/apis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,39 @@ class APIs {

/*전체 게시판 게시글 상세 조회*/

/*공지사항 전체조회*/
static Future<List<dynamic>> BoardNotice() async {
final _url = 'http://3.34.2.246:8080/api/v2/notices';

try {
// 토큰 읽어오기
var jwtToken = await storage.read(key: 'token');
jwtToken = json.decode(jwtToken!)['data']['accessToken'];

final response = await http.get(
Uri.parse(_url),
headers: {
'Authorization': 'Bearer $jwtToken',
'Content-Type': 'application/json',
},
);

if (response.statusCode == 200) {
final responseData = json.decode(utf8.decode(response.bodyBytes));
final data = responseData['data'];
if (data != null && data is List) {
return data;
}
}

print('API request failed: ${response.statusCode}');
return [];
} catch (error) {
print('Error fetching notices: $error');
return [];
}
}




Expand Down
26 changes: 16 additions & 10 deletions lib/mockdatas/board_mockdata.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'package:aliens/models/board_model.dart';

import '../models/noticeArticle.dart';



List<Board> totalBoardList = [
infoBoard1,
freePostingBoard1,
Expand Down Expand Up @@ -62,7 +66,7 @@ List<Board> NotificationList=[
Notification2
];

List<Board> NotiList = [
List<NoticeArticle> NotiList = [
Notice1,
Notice2,
Notice3,
Expand Down Expand Up @@ -383,24 +387,26 @@ Board Notification2= Board(
)
);

Board Notice1= Board(
articleId: 1,
title: '[Friend Ship 이용시 규칙사항]',
content: "안녕하세요!! \n Friendship 관리자 ㅇㅇㅇ입니다. ~~~~",
createdAt: "2023-01-31 23:53:00",

NoticeArticle Notice1= NoticeArticle (
noticeId: 1,
title: '[Friend Ship 이용시 규칙사항]',
content: "안녕하세요!! \n Friendship 관리자 ㅇㅇㅇ입니다. ~~~~",
createdAt: "2023-01-31 23:53:00",


);

Board Notice2= Board(
articleId: 1,
NoticeArticle Notice2= NoticeArticle (
noticeId: 1,
title: '베타버전 안내&베타테스터 모집 ',
content: "베타테스터 하실분~~~ 추첨을 통해 맥북드림",
createdAt: "2023-01-31 23:53:00",

);

Board Notice3= Board(
articleId: 1,
NoticeArticle Notice3= NoticeArticle (
noticeId: 1,
title: '3.1ver 업데이트 안내',
content: "업데이트 되었으니 업데이트 하시오",
createdAt: "2023-01-31 23:53:00",
Expand Down
53 changes: 53 additions & 0 deletions lib/models/noticeArticle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class NoticeArticle {
int? noticeId;
String? title;
String? content;
String? createdAt;
MemberDto? memberDto;

NoticeArticle({
this.noticeId,
this.title,
this.content,
this.createdAt,
this.memberDto,
});

factory NoticeArticle.fromJson(Map<String, dynamic> json) {
return NoticeArticle(
noticeId: json['noticeId'],
title: json['title'],
content: json['content'],
createdAt: json['createdAt'],
memberDto: json['memberDto'] != null
? MemberDto.fromJson(json['memberDto'])
: null,
);
}
}

class MemberDto {
int? memberId;
String? email;
String? name;
String? profileImageUrl;
String? nationality;

MemberDto({
this.memberId,
this.email,
this.name,
this.profileImageUrl,
this.nationality,
});

factory MemberDto.fromJson(Map<String, dynamic> json) {
return MemberDto(
memberId: json['memberId'],
email: json['email'],
name: json['name'],
profileImageUrl: json['profileImageUrl'],
nationality: json['nationality'],
);
}
}
6 changes: 1 addition & 5 deletions lib/views/components/board_dialog_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ class BoardDialog extends StatelessWidget{
final BuildContext context;
final Board board;

const BoardDialog({
Key? key,
required this.context,
required this.board
}) : super(key:key);
const BoardDialog({Key? key, required this.context, required this.board}) : super(key:key);

@override
Widget build(BuildContext context) {
Expand Down
4 changes: 3 additions & 1 deletion lib/views/components/board_drawer_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ class _BoardDrawerWidgetState extends State<BoardDrawerWidget> {
child: ListView(
children: [
InkWell(
onTap: (){
onTap: () async {
if(widget.isTotalBoard){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NoticeBoardPage(screenArguments: widget.screenArguments)),

);

}
else{
Navigator.pop(context);
Expand Down
6 changes: 2 additions & 4 deletions lib/views/components/info_article_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import 'package:flutter_svg/svg.dart';
import '../../models/board_model.dart';
import '../pages/board/article_page.dart';
import 'board_dialog_widget.dart';
/*
class InfoArticleWidget extends StatefulWidget {
/*class InfoArticleWidget extends StatefulWidget {
InfoArticleWidget({super.key, required this.board, required this.nationCode, required this.isTotal});
Expand Down Expand Up @@ -137,6 +136,5 @@ class _InfoArticleWidgetState extends State<InfoArticleWidget>{
),
);
}
}
}*/

*/
32 changes: 27 additions & 5 deletions lib/views/components/notice_board_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import 'dart:async';

import 'package:aliens/models/message_model.dart';
import 'package:aliens/models/noticeArticle.dart';
import 'package:aliens/views/pages/board/article_writing_page.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';

import '../../apis/apis.dart';
import '../../models/board_model.dart';
import '../../models/screenArgument.dart';
import '../pages/board/article_page.dart';
Expand All @@ -18,18 +20,38 @@ import 'board_dialog_widget.dart';

class NoticeWidget extends StatefulWidget {

NoticeWidget({super.key, required this.board, required this.screenArguments});
NoticeWidget({super.key, required this.noticeArticle, required this.screenArguments});
final ScreenArguments screenArguments;
final Board board;
final NoticeArticle noticeArticle;

@override
State<StatefulWidget> createState() => _NoticeWidgetState();
}
class _NoticeWidgetState extends State<NoticeWidget> {
String createdAt = '';
List<NoticeArticle> noticearticles = [];

@override
void initState() {
super.initState();
fetchNoticeData();
}


Future<void> fetchNoticeData() async {
try {
final response = await APIs.BoardNotice();
final dataList = response as List<dynamic>; // 변환된 리스트 데이터

setState(() {
print('1');
noticearticles = dataList.map((article) =>
NoticeArticle.fromJson(article)).toList();
print('2');
});
} catch (error) {
print('Error fetching article data: $error');
}
}

@override
Expand All @@ -39,7 +61,7 @@ class _NoticeWidgetState extends State<NoticeWidget> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NoticeDetailPage(board: widget.board, screenArguments: widget.screenArguments)),
builder: (context) => NoticeDetailPage(noticeArticle: widget.noticeArticle, screenArguments: widget.screenArguments)),
);
},
child: ListTile(
Expand All @@ -49,7 +71,7 @@ class _NoticeWidgetState extends State<NoticeWidget> {
Padding(
padding:EdgeInsets.only(),
child: Text(
'${widget.board.title}',
'${widget.noticeArticle.title}',
style: TextStyle(
fontSize: 18.spMin,
color: Colors.black,
Expand All @@ -58,7 +80,7 @@ class _NoticeWidgetState extends State<NoticeWidget> {
),
Flexible(
child: Text(
DataUtils.getTime(widget.board.createdAt),
DataUtils.getTime(widget.noticeArticle.createdAt),
style: TextStyle(
fontSize: 14.spMin,
color: Color(0xff888888),
Expand Down
29 changes: 26 additions & 3 deletions lib/views/pages/board/notice_board_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';

import 'package:aliens/apis/apis.dart';
import 'package:aliens/models/noticeArticle.dart';
import '../../../apis/apis.dart';
import '../../../mockdatas/market_comment_mockdata.dart';
import '../../../models/countries.dart';
import '../../components/article_widget.dart';
Expand All @@ -35,6 +37,26 @@ class NoticeBoardPage extends StatefulWidget {

class _NoticeBoardPageState extends State<NoticeBoardPage> {
bool isDrawerStart = false;
List<NoticeArticle> noticearticles = []; //공지사항 저장

void initState() {
super.initState();
fetchNoticeData();
}

Future<void> fetchNoticeData() async {
try {
final response = await APIs.BoardNotice();
final dataList = response as List<dynamic>;

setState(() {
// API 데이터를 공지사항 목록으로 변환하여 업데이트
noticearticles = dataList.map((article) => NoticeArticle.fromJson(article)).toList();
});
} catch (error) {
print('Error fetching notice data: $error');
}
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -131,15 +153,16 @@ class _NoticeBoardPageState extends State<NoticeBoardPage> {
child: Container(
decoration: BoxDecoration(color: Colors.white),
child: ListView.builder(
itemCount: NotiList.length,
itemCount: noticearticles.length,
itemBuilder: (context, index) {
final noticeArticle = noticearticles[index];
return Column(
children: [
Divider(
thickness: 1.h,
color: Color(0xffCECECE),
),
NoticeWidget(board: NotiList[index], screenArguments: widget.screenArguments),
NoticeWidget(noticeArticle: noticeArticle, screenArguments: widget.screenArguments),

],
);
Expand Down
11 changes: 6 additions & 5 deletions lib/views/pages/board/notice_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:aliens/mockdatas/board_mockdata.dart';
import 'package:aliens/models/board_model.dart';
import 'package:aliens/models/chatRoom_model.dart';
import 'package:aliens/models/noticeArticle.dart';
import 'package:aliens/models/screenArgument.dart';
import 'package:aliens/repository/sql_message_database.dart';
import 'package:aliens/views/pages/chatting/chatting_page.dart';
Expand All @@ -27,9 +28,9 @@ import 'notification_page.dart';


class NoticeDetailPage extends StatefulWidget {
NoticeDetailPage({Key? key, required this.screenArguments, required this.board}) : super(key: key);
NoticeDetailPage({Key? key, required this.screenArguments, required this.noticeArticle}) : super(key: key);
final ScreenArguments screenArguments;
final Board board;
final NoticeArticle noticeArticle;


@override
Expand Down Expand Up @@ -127,7 +128,7 @@ class _NoticeDetailPageState extends State<NoticeDetailPage> {
Padding(
padding:EdgeInsets.only(),
child: Text(
'${widget.board.title}',
'${widget.noticeArticle.title}',
style: TextStyle(
fontSize: 18.spMin,
fontWeight: FontWeight.bold,
Expand All @@ -138,7 +139,7 @@ class _NoticeDetailPageState extends State<NoticeDetailPage> {

Flexible(
child: Text(
DataUtils.getTime(widget.board.createdAt),
DataUtils.getTime(widget.noticeArticle.createdAt),
style: TextStyle(
fontSize: 14.spMin,
color: Color(0xff888888),
Expand All @@ -155,7 +156,7 @@ class _NoticeDetailPageState extends State<NoticeDetailPage> {
Padding(
padding: EdgeInsets.symmetric(horizontal: 21.w, vertical: 10.spMin),
child: Text(
'${widget.board.content}', // 'content' from the Board object
'${widget.noticeArticle.content}', // 'content' from the Board object
style: TextStyle(
fontSize: 18.spMin,
color: Colors.black,
Expand Down
Loading

0 comments on commit 8f0aca6

Please sign in to comment.