favoriteData.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import 'package:sqflite/sqflite.dart';
  2. import 'dart:async';
  3. import 'dart:io';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:video/model/video.dart';
  6. class FavoriteData {
  7. static FavoriteData _databaseHelper;
  8. static Database _database;
  9. final String tableVideo = 'TableVideo';
  10. final String columnId = 'id';
  11. final String url = 'url';
  12. final String title = 'title';
  13. final String time = 'time';
  14. final String type = 'type';
  15. final String imageUrl = 'imageUrl';
  16. FavoriteData._creatInstance();
  17. factory FavoriteData() {
  18. if (_databaseHelper == null) {
  19. _databaseHelper = FavoriteData._creatInstance();
  20. }
  21. return _databaseHelper;
  22. }
  23. Future<Database> initializeDatabase() async {
  24. Directory directory = await getApplicationDocumentsDirectory();
  25. String path = directory.path + 'favorite.db';
  26. var employeeDatabase =
  27. await openDatabase(path, version: 1, onCreate: _creatDB);
  28. return employeeDatabase;
  29. }
  30. void _creatDB(Database db, int newVersion) async {
  31. await db.execute(
  32. 'CREATE TABLE $tableVideo($columnId INTEGER PRIMARY KEY AUTOINCREMENT, $url TEXT, $title TEXT, $time TEXT, $type TEXT, $imageUrl)',
  33. );
  34. }
  35. Future<Database> get database async {
  36. if (_database == null) {
  37. _database = await initializeDatabase();
  38. }
  39. return _database;
  40. }
  41. Future<List<Map<String, dynamic>>> getVideoMapList(String dataType) async {
  42. Database db = await this.database;
  43. var result = await db.rawQuery(
  44. "SELECT * FROM $tableVideo where $type = '$dataType' order by $columnId ASC");
  45. return result;
  46. }
  47. // Insert
  48. Future<int> insertVideo(Video video) async {
  49. Database db = await this.database;
  50. final videoList = await getVideoList(video.type);
  51. if (videoList.where((element) => element.url == video.url).length > 0) {
  52. return 0;
  53. } else {
  54. var result = await db.insert(tableVideo, video.toJson());
  55. return result;
  56. }
  57. }
  58. // Update
  59. // Future<int> updateVideo(Video video, String dataType) async {
  60. // Database db = await this.database;
  61. // var result = await db.update(tableVideo, video.toJson(),
  62. // where: '$columnId = ?', whereArgs: [video.id]);
  63. // return result;
  64. // }
  65. // Delete
  66. Future<int> deleteVideo(String videoUrl, String dataType) async {
  67. var db = await this.database;
  68. int result = await db.rawDelete(
  69. "DELETE FROM $tableVideo WHERE $url = '$videoUrl' and $type = '$dataType'");
  70. return result;
  71. }
  72. Future<int> deleteVideoList(String dataType) async {
  73. var db = await this.database;
  74. int result =
  75. await db.rawDelete("DELETE FROM $tableVideo WHERE $type = '$dataType'");
  76. return result;
  77. }
  78. //Total Count
  79. // Future<int> getCount() async {
  80. // var db = await this.database;
  81. // List<Map<String, dynamic>> x =
  82. // await db.rawQuery('SELECT COUNT (*) from $tableVideo');
  83. // int result = Sqflite.firstIntValue(x);
  84. // return result;
  85. // }
  86. // IsExit
  87. Future<bool> isExit(String videoUrl, String dataType) async {
  88. var db = await this.database;
  89. List<Map<String, dynamic>> x = await db.rawQuery(
  90. "SELECT COUNT (*) from $tableVideo WHERE $url = '$videoUrl' and $type = '$dataType'");
  91. int result = Sqflite.firstIntValue(x);
  92. if (result == 0) {
  93. return false;
  94. } else {
  95. return true;
  96. }
  97. }
  98. Future<List<Video>> getVideoList(String type) async {
  99. var videoMapList =
  100. await getVideoMapList(type); // Get 'Map List' from database
  101. int count =
  102. videoMapList.length; // Count the number of map entries in db table
  103. List<Video> videoList = List<Video>();
  104. // For loop to create a 'Note List' from a 'Map List'
  105. for (int i = 0; i < count; i++) {
  106. videoList.add(Video.fromJson(videoMapList[i]));
  107. }
  108. return videoList;
  109. }
  110. }