Skip to content

Realdb integrate #33

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

Open
wants to merge 4 commits into
base: master
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
4 changes: 2 additions & 2 deletions database.rules.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": false,
".write": false
".read": true,
".write": true
}
}
46 changes: 46 additions & 0 deletions functions/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion functions/lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const helloWorld = functions.https.onRequest(async (request, response) =>
name: "Radhey",
email: "radheyit@gmail.com",
phone: "8983026208",
tags: ["tagId1", "tagId2", "tagId3"],
},
kilvishId2: {
kilvishId: "KilvishId2",
Expand All @@ -26,6 +27,53 @@ export const helloWorld = functions.https.onRequest(async (request, response) =>
phone: "8208567820",
},
})

const tagRef = db.ref("tags")
await tagRef.update({
tagId1: {
owner: "kilvishId1",
name: "Radhey1",
sharedWith: ["kilvishId2"],
expenses: ["expenses1", "expenses2", "expenses3"],
},
tagId2: {
owner: "kilvishId1",
name: "Radhey2",
sharedWith: ["kilvishId2"],
expenses: ["expenses1", "expenses2", "expenses3"],
},
tagId3: {
owner: "kilvishId1",
name: "Radhey3",
sharedWith: ["kilvishId2"],
expenses: ["expenses1", "expenses2", "expenses3"],
},
})

const expensesRef = db.ref("expenses")
await expensesRef.update({
expenses1: {
timestamp: "2024-02-16 00:45:51.571338",
from: "kilvishId1",
to: "kilvishId2",
amount: 200,
tags: ["tag1", "tag2", "tag3"],
},
expenses2: {
timestamp: "2024-02-15 00:45:51.571338",
from: "kilvishId2",
to: "kilvishId1",
amount: 300,
tags: ["tag1", "tag2", "tag3"],
},
expenses3: {
timestamp: "2024-02-14 00:45:51.571338",
from: "kilvishId1",
to: "kilvishId2",
amount: 400,
tags: ["tag1", "tag2", "tag3"],
},
})
console.log(request.params)
const snapshot = await db.ref("users/kilvishId1").get()
response.send(snapshot.val())
Expand Down
52 changes: 47 additions & 5 deletions lib/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'dart:io';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:kilvish/import_expense_screen.dart';
import 'models.dart';
import 'style.dart';

import 'common_widgets.dart';
import 'detail_screen.dart';
import 'models.dart';
import 'style.dart';

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
Expand Down Expand Up @@ -36,12 +37,53 @@ class HomePageItem {
class _HomePageState extends State<HomePage> {
late List<HomePageItem> _homePageItems;

/// For get Tags info
Future<void> getTagsInfo() async {
if (FirebaseAuth.instance.currentUser != null) {
DatabaseReference user = FirebaseDatabase.instance
.ref('users/${FirebaseAuth.instance.currentUser!.uid}');
user.onValue.listen((DatabaseEvent event) {
if (event.snapshot.exists) {
Map<dynamic, dynamic> userInfo = event.snapshot.value as Map;
if ((userInfo['tags'] as List).isNotEmpty) {
_homePageItems.clear();
}
(userInfo['tags'] as List).asMap().forEach((key, tagName) async {
DataSnapshot tagInfo =
await FirebaseDatabase.instance.ref('tags/$tagName').get();
if (tagInfo.exists) {
Map<dynamic, dynamic> tagInfoMap = tagInfo.value as Map;
(tagInfoMap['expenses'] as List)
.asMap()
.forEach((key, expenseName) async {
DataSnapshot expensesInfo = await FirebaseDatabase.instance
.ref('expenses/$expenseName')
.get();
if (expensesInfo.exists) {
final expense = expensesInfo.value as Map;
_homePageItems.add(HomePageItem(
title: tagName,
lastTransactionAmount: expense['amount'],
balance: 180, // TODO Ask for how to calculate
lastTransactionActor: expense['to'],
lastTransactionDate: DateTime.parse(expense['timestamp']),
type: HomePageItemType.tag));
}
});
}
});
}
});
}
}

@override
void initState() {
super.initState();
getTagsInfo();
// TODO - subscribe to changes/updates
// TODO - build list of HomePageItems from local DB
/* pseudo code
/* pseudo code
List<HomePageItem> homePageItems = [];
for (expense in most_recent_expenses()) { //
name,type = get_type(expense); // type could be tag or url, name is tag/url name
Expand Down
28 changes: 19 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
import 'dart:io';

import 'package:cloud_functions/cloud_functions.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:kilvish/constants/dimens_constants.dart';
import 'package:kilvish/firebase_options.dart';
import 'package:kilvish/home_screen.dart';
import 'package:kilvish/import_expense_screen.dart';
import 'package:kilvish/signup_screen.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';

import 'style.dart';
import 'dart:io';
import 'package:kilvish/constants/dimens_constants.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
FirebaseFunctions.instanceFor().useFunctionsEmulator('localhost', 5001);

if(kDebugMode){
await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
// "kilvish-aa125-default-rtdb"
FirebaseDatabase.instance.useDatabaseEmulator('localhost', 9000);
FirebaseFunctions.instanceFor().useFunctionsEmulator('localhost', 5001);
}
runApp(const Kilvish());
}

class Kilvish extends StatefulWidget {
const Kilvish({Key? key}) : super(key: key);
@override
_MainScreenState createState() => _MainScreenState();
// This widget is the root of your application.
// This widget is the root of your application.
}

class _MainScreenState extends State<Kilvish> {
Expand Down Expand Up @@ -65,9 +73,11 @@ class _MainScreenState extends State<Kilvish> {
debugShowCheckedModeBanner: false,
title: 'Kilvish App',
theme: theme(),
home: _pageToShow == "SignupPage"
? const SignUpPage()
: ImportExpensePage(files: newFiles, text: ""));
home: FirebaseAuth.instance.currentUser != null
? const HomePage()
: _pageToShow == "SignupPage"
? const SignUpPage()
: ImportExpensePage(files: newFiles, text: ""));
}

ThemeData theme() {
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies:
firebase_core: ^2.25.4
cloud_functions: ^4.6.5
firebase_auth: ^4.17.4
firebase_database: ^10.4.5

dev_dependencies:
flutter_test:
Expand Down