Skip to content

Commit

Permalink
Feat: 마지막 조회했던 위치 저장 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
cmsong111 committed Jun 24, 2024
1 parent 68baef1 commit 85fee21
Show file tree
Hide file tree
Showing 18 changed files with 4,055 additions and 2,503 deletions.
1 change: 0 additions & 1 deletion .github/workflows/add_data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jobs:
- name: Add data to Firestore
run: |
python ./rpas/add_data_to_firestore.py
python ./rpas/add_data_to_firestore_v2.py
env:
END_POINT: ${{ secrets.END_POINT }}
SERVICE_KEY: ${{ secrets.SERVICE_KEY }}
Expand Down
70 changes: 8 additions & 62 deletions lib/data/repository_firebase.dart
Original file line number Diff line number Diff line change
@@ -1,81 +1,27 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:intl/intl.dart';
import 'package:nakdong_river/domain/measurement.dart';
import 'package:nakdong_river/domain/position.dart';
import 'package:nakdong_river/domain/repository.dart';
import 'package:nakdong_river/domain/measurement_repository.dart';

class RepositoryFirebaseImpl extends Repository {
class RepositoryFirebaseImpl extends MeasurementRepository {
static FirebaseFirestore instance = FirebaseFirestore.instance;

@override
Future<List<String>> getDepths(Position position) async {
var response = await instance.collection(position.code).get();
return response.docs.map((e) => e.id).toList();
}

@override
Future<List<Measurement>> getRecentData(
Position position, String depth) async {
final today = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd').format(today);
Future<List<Measurement>> getRecentData(Position position) async {
List<Measurement> dateList = [];

var response = await instance
.collection(position.code)
.doc(depth)
.collection(formattedDate)
.orderBy('msmtTm', descending: true)
.collectionGroup("data")
.where("mesure_location_code", isEqualTo: position.code)
.where("mesure_salinity", isNotEqualTo: 0)
.orderBy("mesure_date", descending: true)
.limit(10)
.get();

for (final doc in response.docs) {
dateList.add(Measurement.fromFireStore(doc.data(), position, depth));
dateList.add(Measurement.fromMap(doc.data()));
}

if (response.docs.length < 10) {
formattedDate = DateFormat('yyyy-MM-dd')
.format(today.subtract(const Duration(days: 1)));
response = await instance
.collection(position.code)
.doc(depth)
.collection(formattedDate)
.orderBy('msmtTm', descending: true)
.limit(10 - response.docs.length)
.get();

for (final doc in response.docs) {
dateList.add(Measurement.fromFireStore(doc.data(), position, depth));
}
}
return dateList;
}

@override
Future<Measurement> getRecentDataOne(Position position, String depth) async {
final today = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd').format(today);

var response = await instance
.collection(position.code)
.doc(depth)
.collection(formattedDate)
.orderBy('msmtTm', descending: true)
.limit(1)
.get();

if (response.docs.isEmpty) {
formattedDate = DateFormat('yyyy-MM-dd')
.format(today.subtract(const Duration(days: 1)));
response = await instance
.collection(position.code)
.doc(depth)
.collection(formattedDate)
.orderBy('msmtTm', descending: true)
.limit(1)
.get();
}

return Measurement.fromFireStore(
response.docs.first.data(), position, depth);
}
}
47 changes: 29 additions & 18 deletions lib/domain/measurement.dart
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:nakdong_river/domain/position.dart';

/// 측정 데이터 모델
class Measurement {
final Position position;
final String depth;
final Timestamp time;
final num salinity;
final num temperature;
final Timestamp date;
final num depth;
final String location;
final String locationCode;
final double salinity;
final double temperature;

const Measurement({
Measurement({
required this.date,
required this.depth,
required this.time,
required this.position,
required this.location,
required this.locationCode,
required this.salinity,
required this.temperature,
});

/// FireStore에서 받아온 데이터를 Measurement로 변환한다.
static Measurement fromFireStore(
Map<String, dynamic> json, Position position, String depth) {
factory Measurement.fromMap(Map<String, dynamic> map) {
return Measurement(
depth: depth,
position: position,
time: json['msmtTm'],
salinity: json['saln'],
temperature: json['wtep'],
date: map['mesure_date'],
depth: map['mesure_depths'],
location: map['mesure_location'],
locationCode: map['mesure_location_code'],
salinity: map['mesure_salinity'],
temperature: map['mesure_temperature'],
);
}

Map<String, dynamic> toMap() {
return {
'mesure_date': date,
'mesure_depths': depth,
'mesure_location': location,
'mesure_location_code': locationCode,
'mesure_salinity': salinity,
'mesure_temperature': temperature,
};
}

@override
String toString() {
return 'Measurement{position: ${position.name}, depth: $depth, time: ${time.toDate().toString()}, salinity: $salinity, temperature: $temperature}';
return 'Measurement{date: $date, depth: $depth, location: $location, locationCode: $locationCode, salinity: $salinity, temperature: $temperature}';
}
}
7 changes: 7 additions & 0 deletions lib/domain/measurement_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'package:nakdong_river/domain/measurement.dart';
import 'package:nakdong_river/domain/position.dart';

abstract class MeasurementRepository {
/// 저장되어 있는 데이터 중 가장 최근의 10개의 데이터를 반환한다.
Future<List<Measurement>> getRecentData(Position position);
}
13 changes: 0 additions & 13 deletions lib/domain/repository.dart

This file was deleted.

7 changes: 4 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:nakdong_river/domain/position.dart';
import 'package:nakdong_river/firebase_options.dart';
import 'package:nakdong_river/presentation/providers/admob_provider.dart';
import 'package:nakdong_river/presentation/providers/package_info_provider.dart';
Expand All @@ -16,11 +16,12 @@ void main() async {
options: DefaultFirebaseOptions.currentPlatform,
);
await MobileAds.instance.initialize();
var storge = const FlutterSecureStorage();
var code = await storge.read(key: 'position');
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => PositionProvider(Position.point5)),
ChangeNotifierProvider(create: (_) => PositionProvider(code)),
ChangeNotifierProvider(create: (_) => AdMobProvider()),
ChangeNotifierProvider(create: (_) => PackageInfoProvider()),
],
Expand Down
Loading

0 comments on commit 85fee21

Please sign in to comment.