Skip to content

Commit

Permalink
Change update cycle from monthly to daily
Browse files Browse the repository at this point in the history
  • Loading branch information
harmlessman committed Sep 30, 2023
1 parent 40aab9a commit 490e2c7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 deletions lib/db/update.dart
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
/// Firestore 업데이트는 달에 한번씩, 5일에
/// ex) 7/5에 6월달 데이터 추가
/// Firestore 업데이트는 하루에 한 번씩, 7일 전 데이터를 기준으로
/// ex) 9/30에 9/23 데이터 추가
import 'package:child_movie/db/movie_database.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:child_movie/db/movie.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

/// 최근 업데이트 년/월을 가져옴
/// 최근 업데이트 년/월/일을 가져옴
Future<DateTime> getLastUpdatedDate() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
int? year = prefs.getInt('updatedYear');
int? month = prefs.getInt('updatedMonth');
int? day = prefs.getInt('updatedDay');

return DateTime(year!, month!);
return DateTime(year!, month!, day!);
}

/// 업데이트 후 년/월을 저장
Future<void> setLastUpdatedDate(int year, int month) async {
/// 업데이트 후 년/월/일을 저장
Future<void> setLastUpdatedDate(int year, int month, int day) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt('updatedYear', year);
await prefs.setInt('updatedMonth', month);
await prefs.setInt('updatedDay', day);

return;
}

/// Firebase의 가장 최근 업데이트 날짜를 계산
/// 매달 5일에 데이터가 추가되므로 5일기준으로 계산
/// 당일 기준 7일 전 데이터가 가장 최신 데이터
DateTime getUpdateDate() {
DateTime now = DateTime.now();
DateTime updateDate = now.day > 5
? DateTime(now.year, now.month - 1)
: DateTime(now.year, now.month - 2);
DateTime updateDate = DateTime(now.year, now.month, now.day - 7);
return updateDate;
}

/// Firestore에서 영화 데이터들을 가져옴
///
/// DateTime 값을 인자로 받고, 년-월(ex 2023-07)에 해당하는 컬렉션에서 영화정보들을 가져옴
/// DateTime 값을 인자로 받고, 년+월+일(ex 20230930)에 해당하는 컬렉션에서 영화정보들을 가져옴
Future<List<Movie>> getMovieFromFirestore(
FirebaseFirestore firestore, DateTime date) async {
List<Movie> movies = [];

final moviesRef = firestore
.collection('${date.year}-${date.month.toString().padLeft(2, '0')}')
.collection('${date.year}${date.month.toString().padLeft(2, '0')}${date.day.toString().padLeft(2, '0')}')
.withConverter<Movie>(
fromFirestore: (snapshot, _) => Movie.fromJson(snapshot.data()!),
toFirestore: (movie, _) => movie.toJson(), //do not use
Expand All @@ -60,7 +60,7 @@ Future<List<Movie>> getMovieFromFirestore(
Future<int> updateMovieInfo() async {
List<Future<List<Movie>>> futures = [];
int movieCount = 0;
DateTime lastUpdatedDate = await getLastUpdatedDate(); // year, month
DateTime lastUpdatedDate = await getLastUpdatedDate(); // year, month, day

DateTime updateDate = getUpdateDate();

Expand All @@ -71,7 +71,7 @@ Future<int> updateMovieInfo() async {
// now.compareTo(now); // 0

while (true) {
lastUpdatedDate = DateTime(lastUpdatedDate.year, lastUpdatedDate.month + 1);
lastUpdatedDate = DateTime(lastUpdatedDate.year, lastUpdatedDate.month, lastUpdatedDate.day + 1);
if (updateDate.compareTo(lastUpdatedDate) < 0) {
break;
}
Expand All @@ -90,7 +90,7 @@ Future<int> updateMovieInfo() async {
}
}

await setLastUpdatedDate(updateDate.year, updateDate.month);
await setLastUpdatedDate(updateDate.year, updateDate.month, updateDate.day);

return movieCount;
}
4 changes: 2 additions & 2 deletions lib/pages/update_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class _UpdatePageState extends State<UpdatePage> {
child: Column(
children: [
Text(
"현재 업데이트 : ${snapshot.data.year}-${snapshot.data.month.toString().padLeft(2, '0')}",
"현재 업데이트\n${snapshot.data.year}-${snapshot.data.month.toString().padLeft(2, '0')}-${snapshot.data.day.toString().padLeft(2, '0')}",
style: TextStyle(
decoration: TextDecoration.none,
fontSize: 30.0.sp,
Expand All @@ -342,7 +342,7 @@ class _UpdatePageState extends State<UpdatePage> {
),
SizedBox(height: 24.h,),
Text(
"최신 업데이트 : ${updateDate.year}-${updateDate.month.toString().padLeft(2, '0')}",
"최신 업데이트\n${updateDate.year}-${updateDate.month.toString().padLeft(2, '0')}-${updateDate.day.toString().padLeft(2, '0')}",
style: TextStyle(
decoration: TextDecoration.none,
fontSize: 30.0.sp,
Expand Down
2 changes: 1 addition & 1 deletion lib/setting/setting_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SettingManager with ChangeNotifier {
// 업데이트를 통해 값 변경됨
prefs.setInt('updatedYear', int.parse(jsonData['initialDataDate'].split('-')[0]));
prefs.setInt('updatedMonth', int.parse(jsonData['initialDataDate'].split('-')[1]));

prefs.setInt('updatedDay', int.parse(jsonData['initialDataDate'].split('-')[2]));

}

Expand Down

0 comments on commit 490e2c7

Please sign in to comment.