123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import 'package:sqflite/sqflite.dart';
- import 'dart:async';
- import 'dart:io';
- import 'package:path_provider/path_provider.dart';
- import 'package:video/model/video.dart';
- class FavoriteData {
- static FavoriteData _databaseHelper;
- static Database _database;
- final String tableVideo = 'TableVideo';
- final String columnId = 'id';
- final String url = 'url';
- final String title = 'title';
- final String time = 'time';
- final String type = 'type';
- final String imageUrl = 'imageUrl';
- FavoriteData._creatInstance();
- factory FavoriteData() {
- if (_databaseHelper == null) {
- _databaseHelper = FavoriteData._creatInstance();
- }
- return _databaseHelper;
- }
- Future<Database> initializeDatabase() async {
- Directory directory = await getApplicationDocumentsDirectory();
- String path = directory.path + 'favorite.db';
- var employeeDatabase =
- await openDatabase(path, version: 1, onCreate: _creatDB);
- return employeeDatabase;
- }
- void _creatDB(Database db, int newVersion) async {
- await db.execute(
- 'CREATE TABLE $tableVideo($columnId INTEGER PRIMARY KEY AUTOINCREMENT, $url TEXT, $title TEXT, $time TEXT, $type TEXT, $imageUrl)',
- );
- }
- Future<Database> get database async {
- if (_database == null) {
- _database = await initializeDatabase();
- }
- return _database;
- }
- Future<List<Map<String, dynamic>>> getVideoMapList(String dataType) async {
- Database db = await this.database;
- var result = await db.rawQuery(
- "SELECT * FROM $tableVideo where $type = '$dataType' order by $columnId ASC");
- return result;
- }
- // Insert
- Future<int> insertVideo(Video video) async {
- Database db = await this.database;
- final videoList = await getVideoList(video.type);
- if (videoList.where((element) => element.url == video.url).length > 0) {
- return 0;
- } else {
- var result = await db.insert(tableVideo, video.toJson());
- return result;
- }
- }
- // Update
- // Future<int> updateVideo(Video video, String dataType) async {
- // Database db = await this.database;
- // var result = await db.update(tableVideo, video.toJson(),
- // where: '$columnId = ?', whereArgs: [video.id]);
- // return result;
- // }
- // Delete
- Future<int> deleteVideo(String videoUrl, String dataType) async {
- var db = await this.database;
- int result = await db.rawDelete(
- "DELETE FROM $tableVideo WHERE $url = '$videoUrl' and $type = '$dataType'");
- return result;
- }
- Future<int> deleteVideoList(String dataType) async {
- var db = await this.database;
- int result =
- await db.rawDelete("DELETE FROM $tableVideo WHERE $type = '$dataType'");
- return result;
- }
- //Total Count
- // Future<int> getCount() async {
- // var db = await this.database;
- // List<Map<String, dynamic>> x =
- // await db.rawQuery('SELECT COUNT (*) from $tableVideo');
- // int result = Sqflite.firstIntValue(x);
- // return result;
- // }
- // IsExit
- Future<bool> isExit(String videoUrl, String dataType) async {
- var db = await this.database;
- List<Map<String, dynamic>> x = await db.rawQuery(
- "SELECT COUNT (*) from $tableVideo WHERE $url = '$videoUrl' and $type = '$dataType'");
- int result = Sqflite.firstIntValue(x);
- if (result == 0) {
- return false;
- } else {
- return true;
- }
- }
- Future<List<Video>> getVideoList(String type) async {
- var videoMapList =
- await getVideoMapList(type); // Get 'Map List' from database
- int count =
- videoMapList.length; // Count the number of map entries in db table
- List<Video> videoList = List<Video>();
- // For loop to create a 'Note List' from a 'Map List'
- for (int i = 0; i < count; i++) {
- videoList.add(Video.fromJson(videoMapList[i]));
- }
- return videoList;
- }
- }
|