Skip to content

Commit

Permalink
add: sharedpreference
Browse files Browse the repository at this point in the history
  • Loading branch information
ho2ri2s committed Jun 14, 2020
1 parent 16613d2 commit 5cbb756
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
2 changes: 0 additions & 2 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_state_notifier/flutter_state_notifier.dart';
import 'package:morning_weakers/infrastructure/firebase_auth_service.dart';
import 'package:morning_weakers/models/models.dart';
import 'package:morning_weakers/pages/all/all_page.dart';
import 'package:morning_weakers/pages/group_result/group_result_page.dart';
import 'package:morning_weakers/pages/login/login_page.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SampleController extends StateNotifier<SampleState> with LocatorMixin {
if (firebaseUser == null) {
return;
}
final Hackathon hackathon = await hackathonRepository.getHackathon(hackathonRepository.currentHackathonId);
final Hackathon hackathon = await hackathonRepository.getHackathon(await hackathonRepository.currentHackathonId());
state = state.copyWith(hackathon: hackathon);
}

Expand Down Expand Up @@ -70,7 +70,7 @@ class SampleController extends StateNotifier<SampleState> with LocatorMixin {
return;
}
await hackathonRepository.joinHackathon(
dummyHackathon(id: hackathonRepository.currentHackathonId),
dummyHackathon(id: await hackathonRepository.currentHackathonId()),
[dummyTechnicalStack()],
2,
'備考だよ',
Expand All @@ -82,7 +82,7 @@ class SampleController extends StateNotifier<SampleState> with LocatorMixin {
if (firebaseUser == null) {
return;
}
await questionnaireRepository.createQuestionnaire(hackathonRepository.currentHackathonId, dummyQuestionnaire());
await questionnaireRepository.createQuestionnaire(await hackathonRepository.currentHackathonId(), dummyQuestionnaire());
}

Future<void> getParticipants() async {
Expand All @@ -91,7 +91,7 @@ class SampleController extends StateNotifier<SampleState> with LocatorMixin {
return;
}
final List<Participant> participants =
await participantRepository.getParticipants(hackathonRepository.currentHackathonId);
await participantRepository.getParticipants(await hackathonRepository.currentHackathonId());
state = state.copyWith(participants: participants);
}

Expand All @@ -100,7 +100,7 @@ class SampleController extends StateNotifier<SampleState> with LocatorMixin {
if (firebaseUser == null) {
return;
}
final List<Group> groups = await groupRepository.getGroups(hackathonRepository.currentHackathonId);
final List<Group> groups = await groupRepository.getGroups(await hackathonRepository.currentHackathonId());
state = state.copyWith(groups: groups);
}

Expand All @@ -109,6 +109,6 @@ class SampleController extends StateNotifier<SampleState> with LocatorMixin {
if (firebaseUser == null) {
return;
}
await groupRepository.createGroups(hackathonRepository.currentHackathonId, [dummyGroup()]);
await groupRepository.createGroups(await hackathonRepository.currentHackathonId(), [dummyGroup()]);
}
}
17 changes: 11 additions & 6 deletions lib/repositories/hackathon_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:morning_weakers/infrastructure/firebase_auth_service.dart';
import 'package:morning_weakers/models/models.dart';
import 'package:shared_preferences/shared_preferences.dart';

// TODO: コンストラクタor init時にSharedPreferencesからhackathonIdを取得して変数に保持したい
class HackathonRepository {
HackathonRepository(this.authService);

static const String HACKATHON_ID_KEY = 'hackathonId';
final FirebaseAuthService authService;
final Firestore _firestore = Firestore.instance;

String _currentHackathonId;
Future<SharedPreferences> prefs = SharedPreferences.getInstance();

String get currentHackathonId => _currentHackathonId;
Future<String> currentHackathonId() async {
return Future.value((await prefs).getString(HACKATHON_ID_KEY));
}

/// ハッカソンの新規作成
Future<void> createHackathon(Hackathon hackathon) async {
Expand All @@ -21,15 +26,15 @@ class HackathonRepository {
..remove('groups')
..remove('notifications');
debugPrint(hackMap.toString());
await hackRef.setData(hackMap).whenComplete(() {
await hackRef.setData(hackMap).whenComplete(() async {
// participantsのSubCollectionに代入
final CollectionReference participantsRef = hackRef.collection('participants');
hackathon.participants.forEach((participant) async {
await participantsRef.add(participant.toJson()..remove('id'));
});
// TODO: 作成者も強制的にハッカソンに参加する仕様。あとで話し合う
_updateJoined(hackathon.copyWith(id: hackRef.documentID));
_currentHackathonId = hackRef.documentID;
await _updateJoined(hackathon.copyWith(id: hackRef.documentID));
await (await prefs).setString(HACKATHON_ID_KEY, hackRef.documentID);
});
}

Expand All @@ -56,7 +61,7 @@ class HackathonRepository {
final List<Map<String, dynamic>> groups = await getJsonList('groups');
final List<Map<String, dynamic>> notifications = await getJsonList('notifications');

_currentHackathonId = hackathonId;
await (await prefs).setString(HACKATHON_ID_KEY, hackRef.documentID);

return Future.value(Hackathon.fromJson(hackSnapshot.data
..putIfAbsent('id', () => hackRef.documentID)
Expand Down
28 changes: 28 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,34 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.24.1"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.7+3"
shared_preferences_macos:
dependency: transitive
description:
name: shared_preferences_macos
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+10"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.2+7"
shelf:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies:
cloud_firestore: 0.13.6
firebase_storage: 3.1.6
rxdart: 0.24.1
shared_preferences: 0.5.7+3

cupertino_icons: ^0.1.3
intl: ^0.15.7
Expand Down

0 comments on commit 5cbb756

Please sign in to comment.