Skip to content

Commit

Permalink
Add send to zotero button in article details screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Scriptbash committed Feb 12, 2024
1 parent f2726bf commit 7bf2fba
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 106 deletions.
125 changes: 19 additions & 106 deletions lib/publication_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,25 @@ class _PublicationCardState extends State<PublicationCard> {
//selectedMenu = result;
if (result == SampleItem.itemOne) {
// Send article to Zotero
_sendToZotero();
// Prepare the author names
List<Map<String, dynamic>> authorsData = [];
for (PublicationAuthor author
in widget.authors) {
authorsData.add({
'creatorType': 'author',
'firstName': author.given,
'lastName': author.family,
});
}
ZoteroService.sendToZotero(
context,
authorsData,
widget.title,
widget.abstract,
widget.journalTitle,
widget.publishedDate,
widget.doi,
widget.issn);
} else {
if (result == SampleItem.itemTwo) {
// Copy DOI
Expand Down Expand Up @@ -279,109 +297,4 @@ class _PublicationCardState extends State<PublicationCard> {

return rows.isNotEmpty ? rows.first : null;
}

void _sendToZotero() async {
String? apiKey = await ZoteroService.loadApiKey();
String? userId = await ZoteroService.loadUserId();
String? wisparCollectionKey;

if (apiKey != null && apiKey.isNotEmpty && userId != null) {
List<ZoteroCollection> collections =
await ZoteroService.getTopCollections(apiKey, userId);

bool collectionExists = false;
for (ZoteroCollection collection in collections) {
if (collection.name == "Wispar") {
collectionExists = true;
wisparCollectionKey = collection.key; // Extract the key
break;
}
}

if (collectionExists) {
print(
'Wispar collection already exists with key: $wisparCollectionKey');
} else {
print('Wispar collection does not exist yet');

// Create the "Wispar" collection
await ZoteroService.createZoteroCollection(apiKey, userId, 'Wispar');

// Retrieve the updated list of collections
collections = await ZoteroService.getTopCollections(apiKey, userId);

// Extract the key of the "Wispar" collection from the updated list
for (ZoteroCollection collection in collections) {
if (collection.name == "Wispar") {
wisparCollectionKey = collection.key;
break;
}
}
}

// Prepare the author names
List<Map<String, dynamic>> authorsData = [];
for (PublicationAuthor author in widget.authors) {
authorsData.add({
'creatorType': 'author',
'firstName': author.given,
'lastName': author.family,
});
}
Map<String, dynamic> articleData = {
'data': {
'itemType': 'journalArticle',
'title': widget.title,
'abstractNote': widget.abstract,
'publicationTitle': widget.journalTitle,
'volume': '', //'Volume Number',
'issue': '', //'Issue Number',
'pages': '', //'Page Numbers',
'date': widget.publishedDate!.toIso8601String(),
'series': '', //'Series',
'seriesTitle': '', //'Series Title',
'seriesText': '', //'Series Text',
'journalAbbreviation': '', //'Journal Abbreviation',
'language': '', //'Language',
'DOI': widget.doi,
'ISSN': widget.issn,
'shortTitle': '', //'Short Title',
'url': '',
'accessDate': _formattedDate(DateTime.now()),
'archive': '', //'Archive',
'archiveLocation': '', //'Archive Location',
'libraryCatalog': '', //'Library Catalog',
'callNumber': '', //'Call Number',
'rights': '', //'Rights',
'extra': '', //'Extra Information',
'creators': authorsData,
'collections': [wisparCollectionKey],
'tags': [
{'tag': 'wispar'},
],
'relations': {},
}
/*'creatorTypes': [
{'creatorType': 'author', 'primary': true},
{'creatorType': 'contributor'},
{'creatorType': 'editor'},
{'creatorType': 'translator'},
{'creatorType': 'reviewedAuthor'}
]*/
};
await ZoteroService.createZoteroItem(apiKey, userId, articleData);

ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('The article was sent to Zotero'),
duration: const Duration(seconds: 1),
));
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'API key is null or empty. Please configure the API key in the settings.',
),
duration: const Duration(seconds: 5),
));
}
}
}
28 changes: 28 additions & 0 deletions lib/screens/article_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import '../models/crossref_journals_works_models.dart';
import '../services/database_helper.dart';
import '../publication_card.dart';
import './journals_details_screen.dart';
import '../services/zotero_api.dart';

class ArticleScreen extends StatefulWidget {
final String doi;
Expand Down Expand Up @@ -160,6 +161,33 @@ class _ArticleScreenState extends State<ArticleScreen> {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
children: [
IconButton(
icon: Icon(Icons.book_outlined),
onPressed: () {
List<Map<String, dynamic>> authorsData = [];
for (PublicationAuthor author in articleDetails.authors) {
authorsData.add({
'creatorType': 'author',
'firstName': author.given,
'lastName': author.family,
});
}
ZoteroService.sendToZotero(
context,
authorsData,
widget.title,
articleDetails.abstract,
articleDetails.journalTitle,
articleDetails.publishedDate,
widget.doi,
widget.issn);
},
),
Text('Send to Zotero'),
],
),
Column(
children: [
IconButton(
Expand Down
111 changes: 111 additions & 0 deletions lib/services/zotero_api.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
Expand Down Expand Up @@ -192,4 +193,114 @@ class ZoteroService {
//print('Response body: ${response.body}');
}
}

static void sendToZotero(
context,
List<Map<String, dynamic>> authorsData,
String title,
String? abstract,
String journalTitle,
DateTime? publishedDate,
String doi,
String issn) async {
String? apiKey = await ZoteroService.loadApiKey();
String? userId = await ZoteroService.loadUserId();
String? wisparCollectionKey;

if (apiKey != null && apiKey.isNotEmpty && userId != null) {
List<ZoteroCollection> collections =
await ZoteroService.getTopCollections(apiKey, userId);

bool collectionExists = false;
for (ZoteroCollection collection in collections) {
if (collection.name == "Wispar") {
collectionExists = true;
wisparCollectionKey = collection.key; // Extract the key
break;
}
}

if (collectionExists) {
print(
'Wispar collection already exists with key: $wisparCollectionKey');
} else {
print('Wispar collection does not exist yet');

// Create the "Wispar" collection
await ZoteroService.createZoteroCollection(apiKey, userId, 'Wispar');

// Retrieve the updated list of collections
collections = await ZoteroService.getTopCollections(apiKey, userId);

// Extract the key of the "Wispar" collection from the updated list
for (ZoteroCollection collection in collections) {
if (collection.name == "Wispar") {
wisparCollectionKey = collection.key;
break;
}
}
}

// Prepare the author names

Map<String, dynamic> articleData = {
'data': {
'itemType': 'journalArticle',
'title': title,
'abstractNote': abstract,
'publicationTitle': journalTitle,
'volume': '', //'Volume Number',
'issue': '', //'Issue Number',
'pages': '', //'Page Numbers',
'date': publishedDate!.toIso8601String(),
'series': '', //'Series',
'seriesTitle': '', //'Series Title',
'seriesText': '', //'Series Text',
'journalAbbreviation': '', //'Journal Abbreviation',
'language': '', //'Language',
'DOI': doi,
'ISSN': issn,
'shortTitle': '', //'Short Title',
'url': '',
'accessDate': _formattedDate(DateTime.now()),
'archive': '', //'Archive',
'archiveLocation': '', //'Archive Location',
'libraryCatalog': '', //'Library Catalog',
'callNumber': '', //'Call Number',
'rights': '', //'Rights',
'extra': '', //'Extra Information',
'creators': authorsData,
'collections': [wisparCollectionKey],
'tags': [
{'tag': 'wispar'},
],
'relations': {},
}
/*'creatorTypes': [
{'creatorType': 'author', 'primary': true},
{'creatorType': 'contributor'},
{'creatorType': 'editor'},
{'creatorType': 'translator'},
{'creatorType': 'reviewedAuthor'}
]*/
};
await ZoteroService.createZoteroItem(apiKey, userId, articleData);

ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('The article was sent to Zotero'),
duration: const Duration(seconds: 1),
));
} else {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'API key is null or empty. Please configure the API key in the settings.',
),
duration: const Duration(seconds: 5),
));
}
}
}

String _formattedDate(DateTime date) {
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
}

0 comments on commit 7bf2fba

Please sign in to comment.