Skip to content

Commit 466090d

Browse files
committed
Add database helper and favorite movies local data source
1 parent 8090aa7 commit 466090d

11 files changed

+247
-2
lines changed

ios/Flutter/Debug.xcconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
12
#include "Generated.xcconfig"

ios/Flutter/Release.xcconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
12
#include "Generated.xcconfig"

ios/Podfile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Uncomment this line to define a global platform for your project
2+
# platform :ios, '12.0'
3+
4+
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
5+
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
6+
7+
project 'Runner', {
8+
'Debug' => :debug,
9+
'Profile' => :release,
10+
'Release' => :release,
11+
}
12+
13+
def flutter_root
14+
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
15+
unless File.exist?(generated_xcode_build_settings_path)
16+
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
17+
end
18+
19+
File.foreach(generated_xcode_build_settings_path) do |line|
20+
matches = line.match(/FLUTTER_ROOT\=(.*)/)
21+
return matches[1].strip if matches
22+
end
23+
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
24+
end
25+
26+
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
27+
28+
flutter_ios_podfile_setup
29+
30+
target 'Runner' do
31+
use_frameworks!
32+
33+
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
34+
target 'RunnerTests' do
35+
inherit! :search_paths
36+
end
37+
end
38+
39+
post_install do |installer|
40+
installer.pods_project.targets.each do |target|
41+
flutter_additional_ios_build_settings(target)
42+
end
43+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:path/path.dart';
2+
import 'package:sqflite/sqflite.dart';
3+
4+
class DatabaseHelper {
5+
factory DatabaseHelper() => _instance;
6+
7+
DatabaseHelper._internal();
8+
9+
static final DatabaseHelper _instance = DatabaseHelper._internal();
10+
static Database? _database;
11+
12+
Future<Database> get database async {
13+
if (_database != null) return _database!;
14+
_database = await _initDatabase();
15+
return _database!;
16+
}
17+
18+
Future<Database> _initDatabase() async {
19+
final path = join(await getDatabasesPath(), 'favorite_movies.db');
20+
return openDatabase(
21+
path,
22+
version: 1,
23+
onCreate: _onCreate,
24+
);
25+
}
26+
27+
Future<void> _onCreate(Database db, int version) async {
28+
await db.execute('''
29+
CREATE TABLE favorite_movies(
30+
id INTEGER PRIMARY KEY,
31+
movieId INTEGER NOT NULL,
32+
title TEXT NOT NULL,
33+
posterPath TEXT,
34+
overview TEXT,
35+
voteAverage REAL,
36+
releaseDate TEXT,
37+
createdAt TEXT NOT NULL
38+
)
39+
''');
40+
}
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'package:sqflite/sqflite.dart';
2+
import 'package:tmdb_flutter/app/data/local/database_helper.dart';
3+
4+
class FavoriteMoviesLocalDataSource {
5+
FavoriteMoviesLocalDataSource(this._databaseHelper);
6+
7+
final DatabaseHelper _databaseHelper;
8+
9+
Future<void> addFavoriteMovie(Map<String, dynamic> movie) async {
10+
final db = await _databaseHelper.database;
11+
await db.insert(
12+
'favorite_movies',
13+
{
14+
...movie,
15+
'createdAt': DateTime.now().toIso8601String(),
16+
},
17+
conflictAlgorithm: ConflictAlgorithm.replace,
18+
);
19+
}
20+
21+
Future<void> removeFavoriteMovie(int movieId) async {
22+
final db = await _databaseHelper.database;
23+
await db.delete(
24+
'favorite_movies',
25+
where: 'movieId = ?',
26+
whereArgs: [movieId],
27+
);
28+
}
29+
30+
Future<List<Map<String, dynamic>>> getFavoriteMovies() async {
31+
final db = await _databaseHelper.database;
32+
return db.query(
33+
'favorite_movies',
34+
orderBy: 'createdAt DESC',
35+
);
36+
}
37+
38+
Future<bool> isMovieFavorite(int movieId) async {
39+
final db = await _databaseHelper.database;
40+
final result = await db.query(
41+
'favorite_movies',
42+
where: 'movieId = ?',
43+
whereArgs: [movieId],
44+
);
45+
return result.isNotEmpty;
46+
}
47+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
12
#include "ephemeral/Flutter-Generated.xcconfig"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
12
#include "ephemeral/Flutter-Generated.xcconfig"

macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import FlutterMacOS
66
import Foundation
77

8+
import sqflite_darwin
89

910
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
11+
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
1012
}

macos/Podfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
platform :osx, '10.14'
2+
3+
# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
4+
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
5+
6+
project 'Runner', {
7+
'Debug' => :debug,
8+
'Profile' => :release,
9+
'Release' => :release,
10+
}
11+
12+
def flutter_root
13+
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'ephemeral', 'Flutter-Generated.xcconfig'), __FILE__)
14+
unless File.exist?(generated_xcode_build_settings_path)
15+
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure \"flutter pub get\" is executed first"
16+
end
17+
18+
File.foreach(generated_xcode_build_settings_path) do |line|
19+
matches = line.match(/FLUTTER_ROOT\=(.*)/)
20+
return matches[1].strip if matches
21+
end
22+
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Flutter-Generated.xcconfig, then run \"flutter pub get\""
23+
end
24+
25+
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)
26+
27+
flutter_macos_podfile_setup
28+
29+
target 'Runner' do
30+
use_frameworks!
31+
32+
flutter_install_all_macos_pods File.dirname(File.realpath(__FILE__))
33+
target 'RunnerTests' do
34+
inherit! :search_paths
35+
end
36+
end
37+
38+
post_install do |installer|
39+
installer.pods_project.targets.each do |target|
40+
flutter_additional_macos_build_settings(target)
41+
end
42+
end

pubspec.lock

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,29 @@ packages:
465465
source: hosted
466466
version: "2.2.0"
467467
path:
468-
dependency: transitive
468+
dependency: "direct main"
469469
description:
470470
name: path
471471
sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
472472
url: "https://pub.dev"
473473
source: hosted
474474
version: "1.9.1"
475+
platform:
476+
dependency: transitive
477+
description:
478+
name: platform
479+
sha256: "5d6b1b0036a5f331ebc77c850ebc8506cbc1e9416c27e59b439f917a902a4984"
480+
url: "https://pub.dev"
481+
source: hosted
482+
version: "3.1.6"
483+
plugin_platform_interface:
484+
dependency: transitive
485+
description:
486+
name: plugin_platform_interface
487+
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
488+
url: "https://pub.dev"
489+
source: hosted
490+
version: "2.1.8"
475491
pool:
476492
dependency: transitive
477493
description:
@@ -581,6 +597,46 @@ packages:
581597
url: "https://pub.dev"
582598
source: hosted
583599
version: "1.10.1"
600+
sqflite:
601+
dependency: "direct main"
602+
description:
603+
name: sqflite
604+
sha256: e2297b1da52f127bc7a3da11439985d9b536f75070f3325e62ada69a5c585d03
605+
url: "https://pub.dev"
606+
source: hosted
607+
version: "2.4.2"
608+
sqflite_android:
609+
dependency: transitive
610+
description:
611+
name: sqflite_android
612+
sha256: "2b3070c5fa881839f8b402ee4a39c1b4d561704d4ebbbcfb808a119bc2a1701b"
613+
url: "https://pub.dev"
614+
source: hosted
615+
version: "2.4.1"
616+
sqflite_common:
617+
dependency: transitive
618+
description:
619+
name: sqflite_common
620+
sha256: "84731e8bfd8303a3389903e01fb2141b6e59b5973cacbb0929021df08dddbe8b"
621+
url: "https://pub.dev"
622+
source: hosted
623+
version: "2.5.5"
624+
sqflite_darwin:
625+
dependency: transitive
626+
description:
627+
name: sqflite_darwin
628+
sha256: "279832e5cde3fe99e8571879498c9211f3ca6391b0d818df4e17d9fff5c6ccb3"
629+
url: "https://pub.dev"
630+
source: hosted
631+
version: "2.4.2"
632+
sqflite_platform_interface:
633+
dependency: transitive
634+
description:
635+
name: sqflite_platform_interface
636+
sha256: "8dd4515c7bdcae0a785b0062859336de775e8c65db81ae33dd5445f35be61920"
637+
url: "https://pub.dev"
638+
source: hosted
639+
version: "2.4.0"
584640
stack_trace:
585641
dependency: transitive
586642
description:
@@ -613,6 +669,14 @@ packages:
613669
url: "https://pub.dev"
614670
source: hosted
615671
version: "1.4.1"
672+
synchronized:
673+
dependency: transitive
674+
description:
675+
name: synchronized
676+
sha256: "0669c70faae6270521ee4f05bffd2919892d42d1276e6c495be80174b6bc0ef6"
677+
url: "https://pub.dev"
678+
source: hosted
679+
version: "3.3.1"
616680
term_glyph:
617681
dependency: transitive
618682
description:
@@ -735,4 +799,4 @@ packages:
735799
version: "3.1.3"
736800
sdks:
737801
dart: ">=3.7.0 <4.0.0"
738-
flutter: ">=3.18.0-18.0.pre.54"
802+
flutter: ">=3.24.0"

0 commit comments

Comments
 (0)