Skip to content

Commit

Permalink
fix: history
Browse files Browse the repository at this point in the history
  • Loading branch information
ezeanyimhenry committed Aug 1, 2024
1 parent dfec869 commit 4871742
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 11 deletions.
7 changes: 7 additions & 0 deletions lib/presentation/screens/history/models/history_model.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import 'package:nfc_app/presentation/screens/translate/model/language_data.dart';

enum HistoryType { read, written }

class HistoryModel {
final LanguageType label;
final String language;
final DateTime date;
final String actualText;
final HistoryType type;

HistoryModel({
required this.label,
required this.language,
required this.date,
required this.actualText,
Expand All @@ -16,6 +20,7 @@ class HistoryModel {
// Convert HistoryModel to JSON
Map<String, dynamic> toJson() {
return {
'label': label.toString().split('.').last,
'language': language,
'date': date.toIso8601String(),
'actualText': actualText,
Expand All @@ -26,6 +31,8 @@ class HistoryModel {
// Create HistoryModel from JSON
factory HistoryModel.fromJson(Map<String, dynamic> json) {
return HistoryModel(
label: LanguageType.values
.firstWhere((e) => e.toString().split('.').last == json['label']),
language: json['language'],
date: DateTime.parse(json['date']),
actualText: json['actualText'],
Expand Down
13 changes: 7 additions & 6 deletions lib/presentation/screens/settings/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:nfc_app/constants/app_spacing.dart';
import 'package:nfc_app/constants/app_textstyles.dart';
import 'package:nfc_app/presentation/screens/settings/privacy_policy.dart';
import 'package:nfc_app/presentation/screens/settings/settings_language_screen.dart';
import 'package:nfc_app/presentation/screens/translate/translate_screen.dart';
import 'package:nfc_app/presentation/widgets/app_buttons.dart';

class SettingsScreen extends StatelessWidget {
Expand Down Expand Up @@ -115,12 +116,12 @@ class SettingsScreen extends StatelessWidget {
trailing:
SvgPicture.asset("assets/icons/svg/caret_right.svg"),
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) =>
// const TranslateScreen(message: "I am a boy")),
// );
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const TranslateScreen(message: "I am a boy")),
);
},
),
ListTile(
Expand Down
3 changes: 2 additions & 1 deletion lib/presentation/screens/translate/translate_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class _TranslateScreenState extends State<TranslateScreen> {
targetLanguage: languageNotifier.languageToBeTranslatedTo,
),
);
//save to history
// save to history
List<HistoryModel> loadedHistoryList =
await AppSharedPreference().getHistoryList();
loadedHistoryList.add(HistoryModel(
label: LanguageType.source,
language: languageNotifier.languageToBeTranslatedTo,
date: DateTime.now(),
actualText: widget.message,
Expand Down
70 changes: 67 additions & 3 deletions lib/presentation/screens/translate/widgets/language_card.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:nfc_app/constants/app_colors.dart';
import 'package:nfc_app/constants/app_spacing.dart';
import 'package:nfc_app/constants/app_svgs.dart';
import 'package:nfc_app/constants/app_textstyles.dart';
import 'package:nfc_app/presentation/screens/history/logic/shared_preference.dart';
import 'package:nfc_app/presentation/screens/history/models/history_model.dart';
import 'package:nfc_app/presentation/screens/translate/model/language_data.dart';
import 'package:share/share.dart';

class LanguageCard extends StatelessWidget {
const LanguageCard({
Expand All @@ -16,6 +20,60 @@ class LanguageCard extends StatelessWidget {
final LanguageData data;
final bool isTranslating;
final bool hasError;

// Method to copy text to clipboard
void _copyToClipboard(BuildContext context) {
Clipboard.setData(ClipboardData(text: data.content)).then((_) {
// Show a snackbar to confirm the copy action
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Copied to clipboard'),
duration: Duration(seconds: 2),
),
);
});
}

// Method to share text
void _shareText(BuildContext context) {
Share.share(data.content, subject: 'Shared content from Translate Buddy');
}

Future<void> _saveToHistory(BuildContext context) async {
try {
// Fetch existing history list
List<HistoryModel> loadedHistoryList =
await AppSharedPreference().getHistoryList();

// Validate data before adding to the history list
// final String label = data.type.label ?? 'Unknown';
final String language = data.name;
final String content = data.content;

// Add new history entry
loadedHistoryList.add(HistoryModel(
label: LanguageType.source,
language: language,
date: DateTime.now(),
actualText: content,
type: HistoryType.read,
));

// Save updated history list
await AppSharedPreference().saveHistoryList(loadedHistoryList);

// Optionally show a message to indicate success
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Saved to history!')),
);
} catch (e) {
// Optionally show an error message
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to save to history')),
);
}
}

@override
Widget build(BuildContext context) {
return Container(
Expand Down Expand Up @@ -59,17 +117,23 @@ class LanguageCard extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.end,
children: [
InkWell(
onTap: () {},
onTap: () {
_copyToClipboard(context);
},
child: SvgPicture.asset(AppSvgs.copy),
),
const XGap(value: 8),
InkWell(
onTap: () {},
onTap: () {
_saveToHistory(context);
},
child: SvgPicture.asset(AppSvgs.save),
),
const XGap(value: 8),
InkWell(
onTap: () {},
onTap: () {
_shareText(context);
},
child: SvgPicture.asset(AppSvgs.upload),
)
],
Expand Down
2 changes: 2 additions & 0 deletions lib/presentation/screens/write-nfc/text_record_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:nfc_app/constants/app_spacing.dart';
import 'package:nfc_app/constants/app_textstyles.dart';
import 'package:nfc_app/notifier/nfc_notifier.dart';
import 'package:nfc_app/presentation/screens/history/models/history_model.dart';
import 'package:nfc_app/presentation/screens/translate/model/language_data.dart';
import 'package:nfc_app/presentation/screens/translate/translate_screen.dart';
import 'package:nfc_app/presentation/widgets/app_bottom_sheet.dart';
import 'package:nfc_app/presentation/widgets/app_buttons.dart';
Expand Down Expand Up @@ -159,6 +160,7 @@ class _TextRecordScreenState extends State<TextRecordScreen> {
List<HistoryModel> loadedHistoryList =
await AppSharedPreference().getHistoryList();
loadedHistoryList.add(HistoryModel(
label: LanguageType.target,
language: "English",
date: DateTime.now(),
actualText: controller.text,
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/widgets/history_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HistoryCard extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Source Language - ${historyModel!.language}",
"${historyModel!.label.name} Language - ${historyModel!.language}",
style: AppTextStyle.bodyTextSemiBold,
),
const YGap(
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.12.0"
mime:
dependency: transitive
description:
name: mime
sha256: "2e123074287cc9fd6c09de8336dae606d1ddb88d9ac47358826db698c176a1f2"
url: "https://pub.dev"
source: hosted
version: "1.0.5"
nested:
dependency: transitive
description:
Expand Down Expand Up @@ -512,6 +520,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.1.2"
share:
dependency: "direct main"
description:
name: share
sha256: "97e6403f564ed1051a01534c2fc919cb6e40ea55e60a18ec23cee6e0ce19f4be"
url: "https://pub.dev"
source: hosted
version: "2.0.4"
shared_preferences:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:
video_player: ^2.8.6
youtube_player_flutter: ^9.0.1
chewie: ^1.8.2
share: ^2.0.4

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 4871742

Please sign in to comment.