Skip to content

Commit

Permalink
[Make pure package] Remove dartz, rxdart (#23)
Browse files Browse the repository at this point in the history
* Remove deps

* Impl CacheStream and CacheStreamBuilder

* Move cache_stream_widget out

* Add comments, naming

* Bump 1.1.0 version
  • Loading branch information
mduccc authored Apr 22, 2021
1 parent ea6d018 commit 29fc839
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 67 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Flutter calendar week
A calendar widget by week
Flutter calendar week UI package


##### IOS | Android:
<img src="https://i.imgur.com/Qv78xwO.png" width="40%" height="40%"/> <img src="https://i.imgur.com/oUQYCbC.png" width="43.29%" height="42.45%"/>
Expand Down
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ linter:
- library_prefixes
- non_constant_identifier_names
- directives_ordering
- lines_longer_than_80_chars
- unnecessary_lambdas
- prefer_expression_function_bodies
29 changes: 29 additions & 0 deletions lib/src/cache_stream_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
import 'package:flutter_calendar_week/src/utils/cache_stream.dart';

/// [CacheStreamBuilder] work only with a [CacheStream]
class CacheStreamBuilder<T> extends StreamBuilder {
final T initialData;
final CacheStream<T> cacheStream;
final AsyncWidgetBuilder cacheBuilder;

CacheStreamBuilder(
{this.cacheStream, this.cacheBuilder, this.initialData, Key key})
: super(
key: key,
initialData: initialData,
stream: cacheStream.stream,

/// Return [lastValue] of [cacheStream] if [snapshot] has no data or error
builder: (BuildContext context, AsyncSnapshot snapshot) {
if ((snapshot.hasError || !snapshot.hasData) &&
cacheStream.lastValue != null) {
return cacheBuilder(
context,
AsyncSnapshot.withData(
ConnectionState.done, cacheStream.lastValue));
} else {
return cacheBuilder(context, snapshot);
}
});
}
104 changes: 54 additions & 50 deletions lib/src/calendar_week.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_calendar_week/src/models/decoration_item.dart';
import 'package:flutter_calendar_week/src/models/week_item.dart';
Expand All @@ -7,10 +9,9 @@ import 'package:flutter_calendar_week/src/utils/separate_weeks.dart';
import 'package:flutter_calendar_week/src/utils/compare_date.dart';

import 'package:flutter_calendar_week/src/strings.dart';
import 'package:rxdart/subjects.dart';
import 'package:flutter_calendar_week/src/utils/cache_stream.dart';

class CalendarWeekController {

/*
Example:
CalendarWeek(
Expand Down Expand Up @@ -285,8 +286,11 @@ class CalendarWeek extends StatefulWidget {
}

class _CalendarWeekState extends State<CalendarWeek> {
/// [BehaviorSubject] emit last date pressed
final BehaviorSubject<DateTime> _subject = BehaviorSubject<DateTime>();
/// [_streamController] for emit date press event
final CacheStream<DateTime> _cacheStream = CacheStream<DateTime>();

/// [_stream] for listen date change event
Stream<DateTime> _stream;

/// Page controller
PageController _pageController;
Expand All @@ -297,13 +301,14 @@ class _CalendarWeekState extends State<CalendarWeek> {
widget.controller ?? _defaultCalendarController;

void _jumToDateHandler(DateTime dateTime) {
_subject.add(dateTime);
_cacheStream.add(dateTime);
_pageController.animateToPage(widget.controller._currentWeekIndex,
duration: Duration(milliseconds: 300), curve: Curves.ease);
}

void _setUp() {
assert(_calendarController.hasClient == false);
_stream ??= _cacheStream.stream.asBroadcastStream();
_calendarController
.._weeks.clear()
.._weeks.addAll(separateWeeks(
Expand Down Expand Up @@ -409,54 +414,53 @@ class _CalendarWeekState extends State<CalendarWeek> {

/// Date item layout
Widget _dateItem(DateTime date) => DateItem(
today: _calendarController._today,
date: date,
dateStyle: compareDate(date, _calendarController._today)
? widget.todayDateStyle
: date != null && (date.weekday == 6 || date.weekday == 7)
? widget.weekendsStyle
: widget.dateStyle,
pressedDateStyle: widget.datePressedStyle,
backgroundColor: widget.dateBackgroundColor,
todayBackgroundColor: widget.todayBackgroundColor,
pressedBackgroundColor: widget.datePressedBackgroundColor,
decorationAlignment: () {
/// If date is contain in decorations list, use decorations Alignment
if (widget.decorations.isNotEmpty) {
final List<DecorationItem> matchDate = widget.decorations
.where((ele) => compareDate(ele.date, date))
.toList();
return matchDate.isNotEmpty
? matchDate[0].decorationAlignment
: FractionalOffset.center;
}
return FractionalOffset.center;
}(),
dayShapeBorder: widget.dayShapeBorder,
onDatePressed: (datePressed) {
_calendarController._selectedDate = datePressed;
widget.onDatePressed(datePressed);
},
onDateLongPressed: (datePressed) {
_calendarController._selectedDate = datePressed;
widget.onDateLongPressed(datePressed);
},
decoration: () {
/// If date is contain in decorations list, use decorations Widget
if (widget.decorations.isNotEmpty) {
final List<DecorationItem> matchDate = widget.decorations
.where((ele) => compareDate(ele.date, date))
.toList();
return matchDate.isNotEmpty ? matchDate[0].decoration : null;
}
return null;
}(),
subject: _subject,
);
today: _calendarController._today,
date: date,
dateStyle: compareDate(date, _calendarController._today)
? widget.todayDateStyle
: date != null && (date.weekday == 6 || date.weekday == 7)
? widget.weekendsStyle
: widget.dateStyle,
pressedDateStyle: widget.datePressedStyle,
backgroundColor: widget.dateBackgroundColor,
todayBackgroundColor: widget.todayBackgroundColor,
pressedBackgroundColor: widget.datePressedBackgroundColor,
decorationAlignment: () {
/// If date is contain in decorations list, use decorations Alignment
if (widget.decorations.isNotEmpty) {
final List<DecorationItem> matchDate = widget.decorations
.where((ele) => compareDate(ele.date, date))
.toList();
return matchDate.isNotEmpty
? matchDate[0].decorationAlignment
: FractionalOffset.center;
}
return FractionalOffset.center;
}(),
dayShapeBorder: widget.dayShapeBorder,
onDatePressed: (datePressed) {
_calendarController._selectedDate = datePressed;
widget.onDatePressed(datePressed);
},
onDateLongPressed: (datePressed) {
_calendarController._selectedDate = datePressed;
widget.onDateLongPressed(datePressed);
},
decoration: () {
/// If date is contain in decorations list, use decorations Widget
if (widget.decorations.isNotEmpty) {
final List<DecorationItem> matchDate = widget.decorations
.where((ele) => compareDate(ele.date, date))
.toList();
return matchDate.isNotEmpty ? matchDate[0].decoration : null;
}
return null;
}(),
cacheStream: _cacheStream);

@override
void dispose() {
super.dispose();
if (!_subject.isClosed) _subject.close();
_cacheStream.close();
}
}
23 changes: 12 additions & 11 deletions lib/src/date_item.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'package:flutter_calendar_week/src/cache_stream_widget.dart';
import 'package:flutter_calendar_week/src/utils/compare_date.dart';
import 'package:flutter_calendar_week/src/utils/cache_stream.dart';

class DateItem extends StatefulWidget {
/// Today
Expand Down Expand Up @@ -39,13 +40,13 @@ class DateItem extends StatefulWidget {
/// Decoration widget
final Widget decoration;

/// [BehaviorSubject] for emit, listen last date pressed
final BehaviorSubject<DateTime> subject;
/// [cacheStream] for emit date press event
final CacheStream<DateTime> cacheStream;

DateItem({
@required this.today,
@required this.date,
@required this.subject,
@required this.cacheStream,
this.dateStyle,
this.pressedDateStyle,
this.backgroundColor = Colors.transparent,
Expand All @@ -71,13 +72,13 @@ class __DateItemState extends State<DateItem> {

@override
Widget build(BuildContext context) => widget.date != null
? StreamBuilder(
stream: widget.subject,
builder: (_, data) {
/// Set default each [builder] is called
? CacheStreamBuilder<DateTime>(
cacheStream: widget.cacheStream,
cacheBuilder: (_, data) {
/// Set default each [builder] is called
_defaultBackgroundColor = widget.backgroundColor;

/// Set default style each [builder] is called
/// Set default style each [builder] is called
_defaultTextStyle = widget.dateStyle;

/// Check and set [Background] of today
Expand Down Expand Up @@ -150,13 +151,13 @@ class __DateItemState extends State<DateItem> {

/// Handler press event
void _onPressed() {
widget.subject.add(widget.date);
widget.cacheStream.add(widget.date);
widget.onDatePressed(widget.date);
}

/// Handler long press event
void _onLongPressed() {
widget.subject.add(widget.date);
widget.cacheStream.add(widget.date);
widget.onDateLongPressed(widget.date);
}
}
50 changes: 50 additions & 0 deletions lib/src/utils/cache_stream.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'dart:async';

/// [BaseCacheStream] cache last value of Stream
abstract class BaseCacheStream<T> {
/// Expose a [stream] for external
Stream<T> get stream;

/// Add event to [BaseCacheStream]
void add(T args);

/// Last value of [stream]
T get lastValue;

/// Close [BaseCacheStream]
void close();
}

/// [CacheStream] implement [BaseCacheStream]
class CacheStream<T> implements BaseCacheStream<T> {
/// [_streamController] for add and listen event
StreamController<T> _streamController = StreamController();
Stream<T> _stream;
T _lastValue;

/// init [_stream] and listen for update [_lastValue]
CacheStream() {
_stream ??= _streamController.stream.asBroadcastStream();
_stream.listen((value) {
if (value != null) {
_lastValue = value;
}
});
}

@override
Stream<T> get stream => _stream;

@override
void add(T event) {
_streamController.add(event);
}

@override
T get lastValue => _lastValue;

@override
void close() {
_streamController.close();
}
}
6 changes: 2 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
name: flutter_calendar_week
description: Flutter calendar week UI package
version: 1.0.3+2
version: 1.1.0
authors:
- mduccc
- mducc1412@gmail.com
homepage: https://github.com/mduccc

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.6.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
dartz: ^0.9.2
rxdart: ^0.25.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 29fc839

Please sign in to comment.