Skip to content

Commit 9823e4a

Browse files
committed
📝 Added Lots of Comment, ♻️ Refractored Lots of Code
1 parent 432a397 commit 9823e4a

19 files changed

+549
-225
lines changed

lib/constants.dart

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
// 🐦 Flutter imports:
12
import 'package:flutter/material.dart';
23

4+
/// 🔠🗨️, already in 🗄️ db text
5+
String kAlreadyInDB = "already in the database";
6+
7+
///🔠, 📱 App Title
8+
String kAppTitle = "PDF Indexing";
9+
10+
/// ➕ Create Table Query
311
String kCreateTableQuery = '''
412
CREATE TABLE $kPdfTableName (
513
filename TEXT PRIMARY KEY,
@@ -10,12 +18,42 @@ String kCreateTableQuery = '''
1018
folder TEXT
1119
)
1220
''';
21+
22+
/// 🔠, 🗄️ Database is Empty Message
1323
String kDatabaseEmptyText = "Database is Empty, Click on + to import PDF files";
24+
25+
/// 🔠, 🗄️ Database file Name
1426
String kDBFileName = "data.db";
27+
28+
/// 🔠, 🌐 URL of 'no_file_found.png'
1529
String kFileNotFoundImage = "assets/images/no_file_found.png";
16-
String kGivePermissionText = "Give Permission To Access Files";
30+
31+
/// 🔠, 🙏 Give Permission Text
32+
String kGivePermissionText = "Click Here to Give Permission";
33+
34+
/// 🔠🗨️, 🙏 Give Permission Text for 🔘 FAB
35+
String kGivePermissionTextFAB = "Please Give Permission";
36+
37+
/// 🔠, ❔ Hint Text for Tags TextField in
38+
String kHintText = "Ex: LED Bulb,Panel,Round";
39+
40+
/// 🔠🗨️, Imported Successfully Text
41+
String kImportedSuccessfully = "Imported Successfully";
42+
43+
/// 🔠🗨️, Importing File Message shown in SnackBar
44+
String kImportingFilesMessage = "Importing Files, Please Wait";
45+
46+
/// 💄 TextStyle for [Item]
1747
TextStyle kItemWidgetTextStyle = TextStyle(fontSize: 12);
48+
49+
/// 🔠, [path] Asending for SQLite
1850
String kPathAsc = "path ASC";
51+
52+
/// 🔠, [path] Desending for SQLite
1953
String kPathDesc = "path DESC";
54+
55+
/// 🔠, 'pdf_files' 📁 Directory name
2056
String kPdfFilesPath = "pdf_files";
57+
58+
/// 🔠, 🗄️ Database Table name
2159
String kPdfTableName = "pdf_table";

lib/functions/db_helper.dart

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
// 📦 Package imports:
12
import 'package:path/path.dart';
3+
import 'package:sqflite/sqflite.dart';
4+
5+
// 🌎 Project imports:
26
import 'package:pdf_indexing/constants.dart';
37
import 'package:pdf_indexing/functions/utils.dart' as Utils;
4-
import 'package:pdf_indexing/pdfModel.dart';
5-
import 'package:sqflite/sqflite.dart';
8+
import 'package:pdf_indexing/model/pdfModel.dart';
69

10+
/// 🗄️🛠️ Database Helper Class
711
class DBHelper {
12+
/// 🗄️ [Database?] 🗿 🕵️ variable
813
static Database? _db;
914

15+
/// 🗞️ getter for [_db], which initiate Database if null
1016
Future<Database?> get db async {
1117
if (_db != null) {
1218
return _db;
@@ -16,20 +22,25 @@ class DBHelper {
1622
}
1723
}
1824

25+
/// 🗑️ Delete Row by [filename] from 🗄️ Database
1926
void deleteFromFilename(String filename) async {
2027
Database? dbClient = await db;
2128
int rowsDeleted = await dbClient!
2229
.delete(kPdfTableName, where: "filename = ?", whereArgs: [filename]);
2330
print("$rowsDeleted Rows Deleted");
2431
}
2532

33+
/// 🗑️ Delete Row by [path] from 🗄️ Database
2634
void deleteFromPath(String path) async {
2735
Database? dbClient = await db;
2836
int rowsDeleted = await dbClient!
2937
.delete(kPdfTableName, where: "path = ?", whereArgs: [path]);
3038
print("$rowsDeleted Rows Deleted");
3139
}
3240

41+
/// ⏩🔠 Return String, containing [folder] data
42+
///
43+
/// Where path = [path]
3344
Future<String> getFolder({required String path}) async {
3445
Database? dbClient = await db;
3546
List<Map> results = await dbClient!
@@ -41,6 +52,10 @@ class DBHelper {
4152
}
4253
}
4354

55+
/// ⏩#️⃣ Return String, containing [hash] String
56+
///
57+
/// Where hash = [hash]
58+
/// if hash doesn't exist return empty String ''
4459
Future<String> getSHA1HashDB({required String hash}) async {
4560
Database? dbClient = await db;
4661
List<Map> results = await dbClient!
@@ -53,6 +68,9 @@ class DBHelper {
5368
}
5469
}
5570

71+
/// ⏩🏷️ Return String, containing [tags] data
72+
///
73+
/// Where path = [path]
5674
Future<String> getTags({required String path}) async {
5775
Database? dbClient = await db;
5876
List<Map> results = await dbClient!
@@ -64,22 +82,29 @@ class DBHelper {
6482
}
6583
}
6684

67-
//Select query for all file paths
85+
/// ⏩🗄️ Return Database
86+
///
87+
/// It open Database in 📁 Directory and Create Table in 🗄️ Database on first run
6888
Future<Database> initDB() async {
6989
String storagePath = await Utils.getStoragePath();
7090
Database db = await openDatabase(join(storagePath, kDBFileName),
7191
version: 1, onCreate: _onCreate);
7292
return db;
7393
}
7494

75-
//Select query for file paths with where condition - seaching in the text column
95+
/// ⏩[🗺️] Return [Map,]
96+
///
97+
/// Return [path] from all Rows
7698
Future<List<Map>> queryForAllfilePaths() async {
7799
Database? dbClient = await db;
78100
return await dbClient!
79101
.query(kPdfTableName, columns: ['path'], orderBy: kPathAsc);
80102
}
81103

82-
// Select query for getting sha1 hash entry of the file
104+
/// ⏩[🗺️] Return [Map,]
105+
///
106+
/// Return [path] from Rows
107+
/// Where pdfText,filename,tags contains [text]
83108
Future<List<Map>> queryForFilePathsWithCondition(String text) async {
84109
Database? dbClient = await db;
85110
return await dbClient!.query(
@@ -90,22 +115,26 @@ class DBHelper {
90115
);
91116
}
92117

118+
/// 📥 Save [folder] data in the 🗄️ Database
93119
void saveFolder({required String path, required String folder}) async {
94120
Database? dbClient = await db;
95121
dbClient!
96122
.update(kPdfTableName, {'folder': '$folder'}, where: "path = '$path'");
97123
}
98124

125+
/// 📥 Save [pdfModel] data in the 🗄️ Database
99126
void savePdf(PDFModel pdfModel) async {
100127
Database? dbClient = await db;
101128
dbClient!.insert(kPdfTableName, pdfModel.toMap());
102129
}
103130

131+
/// 📥 Save [tags] data in the 🗄️ Database
104132
void saveTags({required String path, required String tags}) async {
105133
Database? dbClient = await db;
106134
dbClient!.update(kPdfTableName, {'tags': '$tags'}, where: "path = '$path'");
107135
}
108136

137+
/// ➕ Create 🗄️ Database Table if not Created
109138
void _onCreate(Database db, int version) async {
110139
await db.execute(kCreateTableQuery);
111140
}

lib/functions/initialize_db.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
// 🐦 Flutter imports:
12
import 'package:flutter/material.dart';
2-
import 'package:pdf_indexing/functions/db_helper.dart';
3-
import 'package:pdf_indexing/pdfItemModel.dart';
3+
4+
// 📦 Package imports:
45
import 'package:provider/provider.dart';
56

7+
// 🌎 Project imports:
8+
import 'package:pdf_indexing/functions/db_helper.dart';
9+
import 'package:pdf_indexing/model/pdfItemModel.dart';
10+
611
//not in use currrently
12+
/// To Initialise pdf Items in Provider
713
void initialPDFItem({required BuildContext context}) async {
814
List<Map> dbResultItems = [];
915
try {

lib/functions/pdfUtils.dart

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,76 @@
1+
// 🎯 Dart imports:
12
import 'dart:io';
23

4+
// 📦 Package imports:
35
import 'package:file_picker/file_picker.dart';
46
import 'package:path/path.dart';
7+
import 'package:pdf_text/pdf_text.dart';
8+
9+
// 🌎 Project imports:
510
import 'package:pdf_indexing/constants.dart';
611
import 'package:pdf_indexing/functions/utils.dart' as Utils;
7-
import 'package:pdf_indexing/pdfModel.dart';
8-
import 'package:pdf_text/pdf_text.dart';
12+
import 'package:pdf_indexing/model/pdfModel.dart';
913

14+
/// ⏩🧰 Return PDFModel
15+
///
16+
/// ⚙️ Generate PDFModel of [pdfFile]
1017
Future<PDFModel> getPdfModelOfFile(
1118
File pdfFile, List<String> filenamesInDir) async {
1219
String storagePath = await Utils.getStoragePath();
1320
List<String> filename = Utils.getFileNameAndExtentionFromPath(pdfFile.path);
1421
String filenameToUse = '';
1522

16-
//Generate filename if same filename already exists
23+
/// ⚙️ Generate New filename [filenameToUse]
24+
/// if, old filename [filename[0]] already exist in 📁 Directory
1725
int count = 0;
1826
String tempFileName = '${filename[0]}.${filename[1]}';
1927
while (true) {
2028
if (!filenamesInDir.contains(tempFileName)) {
2129
filenameToUse = tempFileName;
22-
print("File New Name 2: $filename");
23-
2430
break;
2531
} else {
26-
print("File Name Exists: $filename");
2732
count++;
2833
tempFileName = '${filename[0]}-$count.${filename[1]}';
29-
print("File New Name: $tempFileName");
3034
}
3135
}
32-
print("File New Name 3: $filename");
3336

37+
/// 📥 Save [pdfFile] in App Directory
3438
File pdfSavedFile =
3539
await pdfFile.copy(join(storagePath, kPdfFilesPath, filenameToUse));
3640

37-
// Extracting Text from pdf
41+
/// 📤 Extracting Text from [pdfSavedFile], at page 1
3842
PDFDoc doc = await PDFDoc.fromFile(pdfSavedFile);
3943
PDFPage page = doc.pageAt(1);
4044
String pageText = await page.text;
4145

42-
// Generating SHA1 Hash
46+
/// ⚙️ Generating [SHA1] #️⃣ of [pdfSavedFile]
4347
String hash = await Utils.getSHA1Hash(pdfSavedFile);
4448

49+
/// ⚙️🧰 Creating pdfModel
4550
PDFModel pdfModel = PDFModel(
4651
path: pdfSavedFile.path,
4752
pdfText: pageText,
4853
tags: '',
4954
hash: hash,
5055
folder: '');
56+
5157
return pdfModel;
5258
}
5359

60+
/// ⏩[📄]?
61+
///
62+
/// Return [File,]?
63+
/// 🗃️ Pick PDF file from [FilePicker]
64+
/// Can pick multiple File
5465
Future<List<File>?> pickPDFFiles() async {
5566
FilePickerResult? result = await FilePicker.platform.pickFiles(
5667
type: FileType.custom,
5768
allowedExtensions: ['pdf'],
5869
allowMultiple: true,
5970
);
71+
72+
/// 🗺️ Results
73+
/// path -> File(path)
6074
if (result != null) {
6175
return result.paths.map((path) => File(path!)).toList();
6276
} else {

0 commit comments

Comments
 (0)