Skip to content

Commit

Permalink
mypage投稿記事無限スクロール、リクエスト回数増やす
Browse files Browse the repository at this point in the history
  • Loading branch information
azuma502 committed Apr 25, 2024
1 parent 796b5ec commit 6503bf9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
6 changes: 4 additions & 2 deletions qiita_app/lib/pages/follower_following_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ class _FollowerFollowingListPageState extends State<FollowerFollowingListPage> {

List<User> fetchedUsers;
if (widget.listType == 'following') {
fetchedUsers = await QiitaRepository.fetchFollowingUsers(widget.userId);
fetchedUsers = await QiitaRepository.fetchFollowingUsers(
widget.userId, currentPage);
} else {
fetchedUsers = await QiitaRepository.fetchFollowersUsers(widget.userId);
fetchedUsers = await QiitaRepository.fetchFollowersUsers(
widget.userId, currentPage);
}
debugPrint('${fetchedUsers.length} users loaded successfully');

Expand Down
21 changes: 18 additions & 3 deletions qiita_app/lib/pages/my_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:qiita_app/models/article.dart';
import 'package:qiita_app/models/user.dart';
import 'package:qiita_app/pages/my_page_notlogin.dart';
import 'package:qiita_app/repository/qiita_repository.dart';
import 'package:qiita_app/services/articles_paginator.dart';
import 'package:qiita_app/widgets/app_title.dart';
import 'package:qiita_app/widgets/article_container.dart';
import 'package:qiita_app/widgets/network_error.dart';
Expand All @@ -21,11 +22,24 @@ class _MyPageState extends State<MyPage> {
User? loggedInUser;
bool isLoading = true;
bool hasNetworkError = false;
late final ArticlesPaginator articlesPaginator;

@override
void initState() {
super.initState();
fetchLoggedInUserInfo();
fetchLoggedInUserInfo().then((_) {
articlesPaginator = ArticlesPaginator(
fetchArticlesCallback: (page) =>
QiitaRepository.fetchUserArticles(loggedInUser!.id, page: page),
onDataUpdated: () => setState(() {}));
articlesPaginator.fetchArticles();
});
}

@override
void dispose() {
articlesPaginator.dispose();
super.dispose();
}

Future<void> fetchLoggedInUserInfo() async {
Expand Down Expand Up @@ -94,10 +108,11 @@ class _MyPageState extends State<MyPage> {
const SectionDivider(text: '投稿記事'),
Expanded(
child: ListView.builder(
itemCount: articles.length,
controller: articlesPaginator.scrollController,
itemCount: articlesPaginator.articles.length,
itemBuilder: (context, index) {
return ArticleContainer(
article: articles[index],
article: articlesPaginator.articles[index],
showAvatar: false,
);
},
Expand Down
46 changes: 31 additions & 15 deletions qiita_app/lib/repository/qiita_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,15 @@ class QiitaRepository {
final accessToken = await _getAccessToken(); // アクセストークンを取得するメソッド
debugPrint('Fetched access token: $accessToken'); // ログ出力でトークン確認

final url = Uri.parse('${Urls.qiitaBaseUrl}/authenticated_user');
final url = Uri.parse('${Urls.qiitaBaseUrl}/users/yametaro');
debugPrint('Requesting authenticated user info from: $url'); // リクエストURLのログ

final response =
await http.get(url, headers: {'Authorization': 'Bearer $accessToken'});
final response = await http.get(url);

// final response = await http.get(url,
// headers: accessToken.isNotEmpty
// ? {'Authorization': 'Bearer $accessToken'}
// : null);
debugPrint('Received response: ${response.body}'); // レスポンス内容のログ

if (response.statusCode == 200) {
Expand Down Expand Up @@ -204,10 +208,14 @@ class QiitaRepository {

static Future<List<Article>> fetchUserArticles(String userId,
{int page = 1}) async {
final accessToken = await _getAccessToken();
final url =
Uri.parse('${Urls.qiitaBaseUrl}/users/$userId/items?page=$page');
try {
final response = await http.get(url);
final response = await http.get(url,
headers: accessToken.isNotEmpty
? {'Authorization': 'Bearer $accessToken'}
: null);

if (response.statusCode == 200) {
final List<dynamic> jsonResponse = jsonDecode(response.body);
Expand All @@ -220,13 +228,15 @@ class QiitaRepository {
}
}

static Future<List<User>> fetchFollowingUsers(String userId) async {
static Future<List<User>> fetchFollowingUsers(String userId, int page) async {
final accessToken = await _getAccessToken(); // アクセストークンを取得
final url = Uri.parse('${Urls.qiitaBaseUrl}/users/$userId/followees');
final url =
Uri.parse('${Urls.qiitaBaseUrl}/users/$userId/followees?page=$page');
try {
final response = await http.get(url, headers: {
'Authorization': 'Bearer $accessToken', // 認証ヘッダーにアクセストークンを設定
});
final response = await http.get(url,
headers: accessToken.isNotEmpty
? {'Authorization': 'Bearer $accessToken'}
: null);

if (response.statusCode == 200) {
final List<dynamic> jsonResponse = jsonDecode(response.body);
Expand All @@ -239,13 +249,15 @@ class QiitaRepository {
}
}

static Future<List<User>> fetchFollowersUsers(String userId) async {
static Future<List<User>> fetchFollowersUsers(String userId, int page) async {
final accessToken = await _getAccessToken();
final url = Uri.parse('${Urls.qiitaBaseUrl}/users/$userId/followers');
final url =
Uri.parse('${Urls.qiitaBaseUrl}/users/$userId/followers?page=$page');
try {
final response = await http.get(url, headers: {
'Authorization': 'Bearer $accessToken',
});
final response = await http.get(url,
headers: accessToken.isNotEmpty
? {'Authorization': 'Bearer $accessToken'}
: null);

if (response.statusCode == 200) {
final List<dynamic> jsonResponse = jsonDecode(response.body);
Expand All @@ -264,9 +276,13 @@ class QiitaRepository {
}

static Future<User> fetchUserInfo(String userId) async {
final accessToken = await _getAccessToken();
final url = Uri.parse('${Urls.qiitaBaseUrl}/users/$userId');
try {
final response = await http.get(url);
final response = await http.get(url,
headers: accessToken.isNotEmpty
? {'Authorization': 'Bearer $accessToken'}
: null);

if (response.statusCode == 200) {
final Map<String, dynamic> jsonResponse = jsonDecode(response.body);
Expand Down

0 comments on commit 6503bf9

Please sign in to comment.