Skip to content

Commit

Permalink
Firebase realtime integration
Browse files Browse the repository at this point in the history
  • Loading branch information
radheyshyamjat committed Feb 16, 2024
1 parent 3e67069 commit b7f2fb8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 25 deletions.
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
}
}
50 changes: 49 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,62 @@ export const helloWorld = functions.https.onRequest(async (request, response) =>
name: "Radhey",
email: "[email protected]",
phone: "8983026208",
tags: ["tagId1", "tagId2", "tagId3"]
},
kilvishId2: {
kilvishId: "KilvishId2",
name: "dummy Name",
email: "[email protected]",
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
65 changes: 43 additions & 22 deletions lib/home_screen.dart
Original file line number Diff line number Diff line change
@@ -1,13 +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 @@ -38,31 +37,53 @@ class HomePageItem {
class _HomePageState extends State<HomePage> {
late List<HomePageItem> _homePageItems;

Future<void> getData() async {
DatabaseReference users = FirebaseDatabase.instance.ref('users');
print("users : ${users.key}");
// print("users : ${users.get()}");
final uu = await users.get();
print("REsponse is ${uu}");
print("REsponse is ${uu.exists}");
print("users : ${await users.get()}");
print("users : ${(await users.get()).exists}");
print("users : ${(await users.get()).key}");
print("users : ${(await users.get()).value}");
/// 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();
print("Here is init method");
getData();
// users.onValue.listen((DatabaseEvent event) {
// final data = event.snapshot.value;
// print("Data is here $data");
// });
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

0 comments on commit b7f2fb8

Please sign in to comment.