-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement cursor and lazy loading for articles
- Loading branch information
1 parent
5816e78
commit 5b7938a
Showing
5 changed files
with
104 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ class CrossRefApi { | |
static const String email = '[email protected]'; | ||
static String? _journalCursor = '*'; | ||
static String? _journalWorksCursor = '*'; | ||
static String? _worksQueryCursor = '*'; | ||
static String? _currentQuery; | ||
|
||
// Query journals by name | ||
|
@@ -111,6 +112,10 @@ class CrossRefApi { | |
_journalWorksCursor = '*'; | ||
} | ||
|
||
static void resetWorksQueryCursor() { | ||
_worksQueryCursor = '*'; | ||
} | ||
|
||
static String? getCurrentQuery() { | ||
return _currentQuery; | ||
} | ||
|
@@ -132,24 +137,29 @@ class CrossRefApi { | |
} | ||
} | ||
|
||
static Future<List<journalsWorks.Item>> getWorksByQuery( | ||
static Future<ListAndMore<journalsWorks.Item>> getWorksByQuery( | ||
Map<String, dynamic> queryParams) async { | ||
String url = '$baseUrl$worksEndpoint'; | ||
// Construct the query parameters string by iterating over the queryParams map | ||
String queryString = queryParams.entries | ||
.map((entry) => | ||
'${Uri.encodeQueryComponent(entry.key)}=${Uri.encodeQueryComponent(entry.value.toString())}') | ||
.join('&'); | ||
|
||
final response = | ||
await http.get(Uri.parse('$url?$queryString&rows=50&$email')); | ||
String apiUrl = '$url?$queryString&rows=50&$email'; | ||
if (_worksQueryCursor != null) { | ||
apiUrl += '&cursor=$_worksQueryCursor'; | ||
} | ||
final response = await http.get(Uri.parse(apiUrl)); | ||
//print('$url?$queryString'); | ||
|
||
if (response.statusCode == 200) { | ||
final responseData = | ||
journalsWorks.JournalWork.fromJson(json.decode(response.body)); | ||
List<journalsWorks.Item> feedItems = responseData.message.items; | ||
return feedItems; | ||
_worksQueryCursor = responseData.message.nextCursor; | ||
bool hasMoreResults = | ||
_worksQueryCursor != null && _worksQueryCursor != ""; | ||
return ListAndMore(feedItems, hasMoreResults); | ||
} else { | ||
throw Exception('Failed to fetch results'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters