Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Se hizo lo que se pudo #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c

COCOAPODS: 1.9.3
COCOAPODS: 1.10.0
17 changes: 8 additions & 9 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 51;
objects = {

/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
38E228E1252A3A180046B26A /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 38E228E0252A3A180046B26A /* GoogleService-Info.plist */; };
2BFC9F96257965920057902F /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 2BFC9F95257965920057902F /* GoogleService-Info.plist */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
Expand All @@ -33,7 +33,7 @@
/* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
38E228E0252A3A180046B26A /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../../GoogleService-Info.plist"; sourceTree = "<group>"; };
2BFC9F95257965920057902F /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
56ACAB42F8D8921F9B7C08F2 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
6827105A28BB4263052AF8B7 /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -70,7 +70,6 @@
9104DDA83F4C300E6DBB8D24 /* Pods-Runner.release.xcconfig */,
6827105A28BB4263052AF8B7 /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -115,7 +114,7 @@
97C146F01CF9000F007C117D /* Runner */ = {
isa = PBXGroup;
children = (
38E228E0252A3A180046B26A /* GoogleService-Info.plist */,
2BFC9F95257965920057902F /* GoogleService-Info.plist */,
97C146FA1CF9000F007C117D /* Main.storyboard */,
97C146FD1CF9000F007C117D /* Assets.xcassets */,
97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -193,7 +192,7 @@
files = (
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */,
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
38E228E1252A3A180046B26A /* GoogleService-Info.plist in Resources */,
2BFC9F96257965920057902F /* GoogleService-Info.plist in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,
);
Expand Down Expand Up @@ -374,7 +373,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = com.example.chatFlutterInTheDark;
PRODUCT_BUNDLE_IDENTIFIER = com.elianortega.chatFlutterInTheDark;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -510,7 +509,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = com.example.chatFlutterInTheDark;
PRODUCT_BUNDLE_IDENTIFIER = com.elianortega.chatFlutterInTheDark;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand Down Expand Up @@ -540,7 +539,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = com.example.chatFlutterInTheDark;
PRODUCT_BUNDLE_IDENTIFIER = com.elianortega.chatFlutterInTheDark;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
Expand Down
34 changes: 34 additions & 0 deletions ios/Runner/GoogleService-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CLIENT_ID</key>
<string>431061982935-qjlhpr5d7vlrl91m7nm9qhpqoq21e0vr.apps.googleusercontent.com</string>
<key>REVERSED_CLIENT_ID</key>
<string>com.googleusercontent.apps.431061982935-qjlhpr5d7vlrl91m7nm9qhpqoq21e0vr</string>
<key>API_KEY</key>
<string>AIzaSyCibYppTK2hEZCHLbBc7BJPguhyTNP_fnM</string>
<key>GCM_SENDER_ID</key>
<string>431061982935</string>
<key>PLIST_VERSION</key>
<string>1</string>
<key>BUNDLE_ID</key>
<string>com.elianortega.chatFlutterInTheDark</string>
<key>PROJECT_ID</key>
<string>flutter-in-the-dark-65476</string>
<key>STORAGE_BUCKET</key>
<string>flutter-in-the-dark-65476.appspot.com</string>
<key>IS_ADS_ENABLED</key>
<false></false>
<key>IS_ANALYTICS_ENABLED</key>
<false></false>
<key>IS_APPINVITE_ENABLED</key>
<true></true>
<key>IS_GCM_ENABLED</key>
<true></true>
<key>IS_SIGNIN_ENABLED</key>
<true></true>
<key>GOOGLE_APP_ID</key>
<string>1:431061982935:ios:94ab188ec1742773461a92</string>
</dict>
</plist>
24 changes: 17 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:chat_flutter_in_the_dark/src/app/error_app.dart';
import 'package:chat_flutter_in_the_dark/src/app/loaded_app.dart';
import 'package:chat_flutter_in_the_dark/src/app/loading_app.dart';
import 'package:chat_flutter_in_the_dark/src/application/bloc/chat_bloc.dart';
import 'package:chat_flutter_in_the_dark/src/data/repositiories/chat_repository.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

void main() {
runApp(MyApp());
Expand All @@ -12,14 +15,21 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final _initialization = Firebase.initializeApp();
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
if (snapshot.hasError) return ErrorApp();
return BlocProvider(
create: (context) => ChatBloc(chatRepository: ChatRepository())
..add(
LoadMessages(),
),
child: FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
if (snapshot.hasError) return ErrorApp();

if (snapshot.connectionState == ConnectionState.done) return ChatApp();
if (snapshot.connectionState == ConnectionState.done)
return ChatApp();

return LoadingApp();
});
return LoadingApp();
}),
);
}
}
48 changes: 48 additions & 0 deletions lib/src/application/bloc/chat_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:chat_flutter_in_the_dark/src/data/models/chat_model.dart';
import 'package:chat_flutter_in_the_dark/src/data/models/message_models.dart';
import 'package:chat_flutter_in_the_dark/src/data/repositiories/chat_repository.dart';
import 'package:meta/meta.dart';

part 'chat_event.dart';
part 'chat_state.dart';

class ChatBloc extends Bloc<ChatEvent, ChatState> {
final ChatRepository _chatRepository;
StreamSubscription _chatsSubscription;

ChatBloc({@required ChatRepository chatRepository})
: assert(chatRepository != null),
_chatRepository = chatRepository,
super(FetchingChats());

@override
Stream<ChatState> mapEventToState(
ChatEvent event,
) async* {
if (event is LoadMessages) {
yield* _mapLoadMessagesToState(event);
} else if (event is ChatsUpdated) {
yield* _mapTodosUpdateToState(event);
} else if (event is AddMessage) {
yield* _mapAddMessage(event);
}
}

Stream<ChatState> _mapLoadMessagesToState(LoadMessages event) async* {
_chatsSubscription?.cancel();
_chatsSubscription = _chatRepository.chats().listen(
(chats) => add(ChatsUpdated(chats)),
);
}

Stream<ChatState> _mapTodosUpdateToState(ChatsUpdated event) async* {
yield FetchedChats(event.chats);
}

Stream<ChatState> _mapAddMessage(AddMessage event) async* {
_chatRepository.addNewMessage(event.message);
}
}
19 changes: 19 additions & 0 deletions lib/src/application/bloc/chat_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
part of 'chat_bloc.dart';

@immutable
abstract class ChatEvent {}

class LoadMessages extends ChatEvent {}

class AddMessage extends ChatEvent {
final MessageModel message;
AddMessage(this.message);
}

class UpdateMessages extends ChatEvent {}

class ChatsUpdated extends ChatEvent {
final List<ChatModel> chats;

ChatsUpdated(this.chats);
}
16 changes: 16 additions & 0 deletions lib/src/application/bloc/chat_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
part of 'chat_bloc.dart';

@immutable
abstract class ChatState {}

class ChatInitial extends ChatState {}

class FetchingChats extends ChatState {}

class ErrorFetchingChats extends ChatState {}

class FetchedChats extends ChatState {
final List<ChatModel> chats;

FetchedChats(this.chats);
}
42 changes: 42 additions & 0 deletions lib/src/data/models/chat_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:chat_flutter_in_the_dark/src/data/models/message_models.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:meta/meta.dart';

class ChatModel {
final String id;
final String title;
final List<MessageModel> chats;
ChatModel({
@required this.id,
@required this.title,
@required this.chats,
});

static ChatModel fromSnapshot(DocumentSnapshot snap) {
return ChatModel(
id: snap.data()['id'],
title: snap.data()['title'],
chats: getMessages(snap.data()['chats']),
);
}

static List<MessageModel> getMessages(List<dynamic> chats) {
List<MessageModel> tempChats = <MessageModel>[];
for (final chat in chats) {
tempChats.add(MessageModel(text: chat));
}
return tempChats;
}

Map<String, Object> toDocument() {
return {
'title': title,
'chats': {
for (final chat in chats)
{
chat.toDocument(),
}
}
};
}
}
22 changes: 22 additions & 0 deletions lib/src/data/models/message_models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:meta/meta.dart';

class MessageModel {
final String text;
final String id;

MessageModel({@required this.text, this.id});

static MessageModel fromSnapshot(DocumentSnapshot snap) {
return MessageModel(
text: snap.data()['text'],
id: snap.data()['id'],
);
}

Map<String, Object> toDocument() {
return {
'text': text,
};
}
}
19 changes: 19 additions & 0 deletions lib/src/data/repositiories/chat_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:chat_flutter_in_the_dark/src/data/models/chat_model.dart';
import 'package:chat_flutter_in_the_dark/src/data/models/message_models.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class ChatRepository {
ChatRepository();

final chatsCollection = FirebaseFirestore.instance.collection('chats');

Future<void> addNewMessage(MessageModel message) {
return chatsCollection.add(message.toDocument());
}

Stream<List<ChatModel>> chats() {
return chatsCollection.snapshots().map((snapshot) {
return snapshot.docs.map((doc) => ChatModel.fromSnapshot(doc)).toList();
});
}
}
Loading