forked from TheAlphamerc/flutter_twitter_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a82da9b
commit 86a1106
Showing
8 changed files
with
278 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
*.mode1v3 | ||
*.mode2v3 | ||
*.moved-aside | ||
*.pbxuser | ||
*.perspectivev3 | ||
**/*sync/ | ||
.sconsign.dblite | ||
.tags* | ||
**/.vagrant/ | ||
**/DerivedData/ | ||
Icon? | ||
**/Pods/ | ||
**/.symlinks/ | ||
profile | ||
xcuserdata | ||
**/.generated/ | ||
Flutter/App.framework | ||
Flutter/Flutter.framework | ||
Flutter/Flutter.podspec | ||
Flutter/Generated.xcconfig | ||
Flutter/app.flx | ||
Flutter/app.zip | ||
Flutter/flutter_assets/ | ||
Flutter/flutter_export_environment.sh | ||
ServiceDefinitions.json | ||
Runner/GeneratedPluginRegistrant.* | ||
|
||
# Exceptions to above rules. | ||
!default.mode1v3 | ||
!default.mode2v3 | ||
!default.pbxuser | ||
!default.perspectivev3 | ||
|
||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class BookmarkModel { | ||
String key; | ||
String tweetId; | ||
String createdAt; | ||
BookmarkModel({ | ||
this.key, | ||
this.tweetId, | ||
this.createdAt, | ||
}); | ||
|
||
factory BookmarkModel.fromJson(Map<dynamic, dynamic> json) => BookmarkModel( | ||
key: json["tweetId"], | ||
tweetId: json["tweetId"], | ||
createdAt: json["created_at"], | ||
); | ||
|
||
Map<String, dynamic> toJson() => { | ||
"key": key, | ||
"tweetId": tweetId, | ||
"created_at": createdAt, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:firebase_database/firebase_database.dart'; | ||
import 'package:flutter_twitter_clone/helper/shared_prefrence_helper.dart'; | ||
import 'package:flutter_twitter_clone/helper/utility.dart'; | ||
import 'package:flutter_twitter_clone/model/bookmarkModel.dart'; | ||
import 'package:flutter_twitter_clone/model/feedModel.dart'; | ||
import 'package:flutter_twitter_clone/ui/page/common/locator.dart'; | ||
import 'appState.dart'; | ||
|
||
class BookmarkState extends AppState { | ||
BookmarkState() { | ||
getDataFromDatabase(); | ||
} | ||
List<FeedModel> _tweetList; | ||
List<BookmarkModel> _bookmarkList; | ||
|
||
addBookmarkTweetToList(BookmarkModel model) { | ||
if (_bookmarkList == null) { | ||
_bookmarkList = <BookmarkModel>[]; | ||
} | ||
|
||
if (!_bookmarkList.any((element) => element.key == model.key)) { | ||
_bookmarkList.add(model); | ||
} | ||
} | ||
|
||
List<FeedModel> get tweetList => _tweetList; | ||
|
||
/// get [Notification list] from firebase realtime database | ||
void getDataFromDatabase() async { | ||
String userId = await getIt<SharedPreferenceHelper>() | ||
.getUserProfile() | ||
.then((value) => value.userId); | ||
try { | ||
if (_tweetList != null) { | ||
return; | ||
} | ||
loading = true; | ||
kDatabase | ||
.child('bookmark') | ||
.child(userId) | ||
.once() | ||
.then((DataSnapshot snapshot) async { | ||
if (snapshot.value != null) { | ||
var map = snapshot.value as Map<dynamic, dynamic>; | ||
if (map != null) { | ||
map.forEach((bookmarkKey, value) { | ||
var map = value as Map<dynamic, dynamic>; | ||
var model = BookmarkModel.fromJson(map); | ||
model.key = bookmarkKey; | ||
addBookmarkTweetToList(model); | ||
}); | ||
} | ||
|
||
if (_bookmarkList != null) { | ||
for (var bookmark in _bookmarkList) { | ||
var tweet = await getTweetDetail(bookmark.tweetId); | ||
if (tweet != null) { | ||
if (_tweetList == null) { | ||
_tweetList = <FeedModel>[]; | ||
} | ||
_tweetList.add(tweet); | ||
} | ||
} | ||
} | ||
} | ||
loading = false; | ||
}); | ||
} catch (error) { | ||
loading = false; | ||
cprint(error, errorIn: 'getDataFromDatabase'); | ||
} | ||
} | ||
|
||
/// get `Tweet` present in notification | ||
Future<FeedModel> getTweetDetail(String tweetId) async { | ||
FeedModel _tweetDetail; | ||
var snapshot = await kDatabase.child('tweet').child(tweetId).once(); | ||
if (snapshot.value != null) { | ||
var map = snapshot.value as Map<dynamic, dynamic>; | ||
_tweetDetail = FeedModel.fromJson(map); | ||
_tweetDetail.key = snapshot.key; | ||
return _tweetDetail; | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_twitter_clone/helper/enum.dart'; | ||
import 'package:flutter_twitter_clone/model/feedModel.dart'; | ||
import 'package:flutter_twitter_clone/state/bookmarkState.dart'; | ||
import 'package:flutter_twitter_clone/ui/theme/theme.dart'; | ||
import 'package:flutter_twitter_clone/widgets/customAppBar.dart'; | ||
import 'package:flutter_twitter_clone/widgets/newWidget/emptyList.dart'; | ||
import 'package:flutter_twitter_clone/widgets/tweet/tweet.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
class BookmarkPage extends StatelessWidget { | ||
const BookmarkPage({Key key}) : super(key: key); | ||
|
||
static Route<T> getRoute<T>() { | ||
return MaterialPageRoute( | ||
builder: (_) { | ||
return Provider( | ||
create: (_) => BookmarkState(), | ||
child: ChangeNotifierProvider( | ||
create: (BuildContext context) => BookmarkState(), | ||
builder: (_, child) => BookmarkPage(), | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
backgroundColor: TwitterColor.mystic, | ||
appBar: CustomAppBar( | ||
title: Text("Bookmark", style: TextStyles.titleStyle), | ||
isBackButton: true, | ||
), | ||
body: BookmarkPageBody(), | ||
); | ||
} | ||
} | ||
|
||
class BookmarkPageBody extends StatelessWidget { | ||
const BookmarkPageBody({Key key}) : super(key: key); | ||
|
||
Widget _tweet(BuildContext context, FeedModel model) { | ||
return Container( | ||
color: Colors.white, | ||
child: Tweet( | ||
model: model, | ||
type: TweetType.Tweet, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
var state = Provider.of<BookmarkState>(context); | ||
var list = state.tweetList; | ||
if (state.isbusy) { | ||
return SizedBox( | ||
height: 3, | ||
child: LinearProgressIndicator(), | ||
); | ||
} else if (list == null || list.isEmpty) { | ||
return Padding( | ||
padding: EdgeInsets.symmetric(horizontal: 30), | ||
child: EmptyList( | ||
'No Bookmark available yet', | ||
subTitle: 'When new bookmark found, they\'ll show up here.', | ||
), | ||
); | ||
} | ||
return ListView.builder( | ||
addAutomaticKeepAlives: true, | ||
itemBuilder: (context, index) => _tweet(context, list[index]), | ||
itemCount: list.length, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.