-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Make pure package] Remove dartz, rxdart (#23)
* Remove deps * Impl CacheStream and CacheStreamBuilder * Move cache_stream_widget out * Add comments, naming * Bump 1.1.0 version
- Loading branch information
Showing
7 changed files
with
149 additions
and
67 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
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,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); | ||
} | ||
}); | ||
} |
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
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,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(); | ||
} | ||
} |
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