Skip to content
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class PostExample extends StatelessWidget {
// callType: CallType.callAsStream,
// secondsOfStream: 1,
// customized your loading (default widget is CircularProgressIndicator)
// loader:CustomLoading(),
// loader:(context)=> CustomLoading(),

// handle errors
onError: (RocketException exception, Function() reload) {
Expand All @@ -267,7 +267,7 @@ class PostExample extends StatelessWidget {
),
);
},
builder: (context,state) {
builder: (context) {
return SizedBox(
height: MediaQuery.of(context).size.height * 0.852,
child: ListView.builder(
Expand Down
28 changes: 28 additions & 0 deletions packages/flutter_rocket/example/lib/utils/widget_states.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_rocket/flutter_rocket.dart';

class WidgetStates {
static Widget onLoading(BuildContext context) => const Center(
child: CircularProgressIndicator(
color: Colors.red,
),
);
static Widget onError(RocketException exception, Function() reload) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(exception.exception),
if (exception.statusCode != HttpStatus.ok) ...[
Text(exception.response),
Text(Rocket.get(rocketRequestKey)
.msgByStatusCode(exception.statusCode))
],
TextButton(onPressed: reload, child: const Text("retry"))
],
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CounterExample extends StatelessWidget {
///
/// In this case, we are returning a `Text` widget that displays
/// the current count value of the `Counter` instance.
builder: (context, modelState) {
builder: (context) {
return Text(
counter.count.toString(),
style: Theme.of(context).textTheme.headlineMedium,
Expand Down
3 changes: 1 addition & 2 deletions packages/flutter_rocket/example/lib/views/photo_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ class PhotoExample extends StatelessWidget {
width: MediaQuery.of(context).size.width,
child: RocketView(
model: photo,
loader: const CircularProgressIndicator(),
// get 5000 items
call: () => GetPhotos.getPhotos(photo),
builder: (context, modelState) {
builder: (context) {
return ListView.builder(
itemCount: photo.all!.length,
itemBuilder: (BuildContext context, int index) {
Expand Down
25 changes: 4 additions & 21 deletions packages/flutter_rocket/example/lib/views/post_view.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:developer';
import 'dart:io';

import 'package:example/models/post_model.dart';
import 'package:example/requests/post_request.dart';
import 'package:example/utils/widget_states.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rocket/flutter_rocket.dart';

Expand Down Expand Up @@ -47,27 +47,10 @@ class PostExample extends StatelessWidget {
// callType: CallType.callAsStream,
// secondsOfStream: 1,
// customized your loading (default widget is CircularProgressIndicator)
loader: const CircularProgressIndicator(),

onLoading: WidgetStates.onLoading,
// handle errors
onError: (RocketException exception, Function() reload) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(exception.exception),
if (exception.statusCode != HttpStatus.ok) ...[
Text(exception.response),
Text(Rocket.get(rocketRequestKey)
.msgByStatusCode(exception.statusCode))
],
TextButton(
onPressed: reload, child: const Text("retry"))
],
),
);
},
builder: (context, state) {
onError: WidgetStates.onError,
builder: (context) {
return SizedBox(
height: MediaQuery.of(context).size.height * 0.852,
child: ListView.builder(
Expand Down
5 changes: 3 additions & 2 deletions packages/flutter_rocket/example/lib/views/todos_view.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:example/models/todo.dart';
import 'package:example/requests/todos_request.dart';
import 'package:example/utils/widget_states.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rocket/flutter_rocket.dart';

Expand All @@ -23,9 +24,9 @@ class TodosExample extends StatelessWidget {
body: RocketView(
model: todoModel,
call: () => GetTodos.getTodos(todoModel),
loader: const CircularProgressIndicator(),
onLoading: WidgetStates.onLoading,
callType: CallType.callIfModelEmpty,
builder: (context, state) {
builder: (context) {
return ListView.builder(
itemCount: todoModel.all!.length,
itemBuilder: (context, index) {
Expand Down
5 changes: 3 additions & 2 deletions packages/flutter_rocket/example/lib/views/user_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:example/models/user_submodel/address_submodel.dart';
import 'package:example/models/user_submodel/company_submodel.dart';
import 'package:example/models/user_submodel/geo_submodel.dart';
import 'package:example/requests/user_request.dart';
import 'package:example/utils/widget_states.dart';
import 'package:flutter/material.dart';
import 'package:flutter_rocket/flutter_rocket.dart';

Expand Down Expand Up @@ -38,8 +39,8 @@ class UserExample extends StatelessWidget {
// your model
model: users,
// your widget for show data from model
loader: const CircularProgressIndicator(),
builder: (context, modelState) {
onLoading: WidgetStates.onLoading,
builder: (context) {
return ListView.builder(
itemCount: users.all!.length,
itemBuilder: (BuildContext context, int index) {
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter_rocket/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0

flutter_rocket: ^0.0.1
flutter_rocket:
path: ../../flutter_rocket



Expand Down
6 changes: 2 additions & 4 deletions packages/flutter_rocket/example/test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'dummy_data.dart';

void main() {
group('Test Post model (multi, fromJson, toJson, updateFields, modelState)',
() {
group('Test Post model (multi, fromJson, toJson, updateFields)', () {
final post = Post();
const String newTitle = "New title";
test("Test Post model (multi, fromJson, toJson)", () {
Expand Down Expand Up @@ -52,8 +51,7 @@ void main() {
});
});

group('Test Todo model (multi, fromJson, toJson, updateFields, modelState)',
() {
group('Test Todo model (multi, fromJson, toJson, updateFields)', () {
final todos = Todos();
test("Test Todo model (multi, fromJson, toJson)", () {
// Test setMulti, fromJson & toJson
Expand Down
4 changes: 3 additions & 1 deletion packages/flutter_rocket/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version: 0.0.1
repository: https://github.com/JahezAcademy/flutter_rocket/flutter_rocket
issue_tracker: https://github.com/JahezAcademy/flutter_rocket/labels/flutter_rocket
homepage: https://github.com/JahezAcademy/flutter_rocket
publish_to: 'none'

environment:
sdk: '>=3.0.0 <4.0.0'
Expand All @@ -14,7 +15,8 @@ dependencies:
sdk: flutter
rocket_model: ^0.0.1
rocket_listenable: ^0.0.1
rocket_view: ^0.0.1
rocket_view:
path: ../rocket_view
rocket_mini_view: ^0.0.1
rocket_client: ^0.0.1
rocket_singleton: ^0.0.1
Expand Down
6 changes: 5 additions & 1 deletion packages/rocket_view/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.1

* TODO: Describe initial release.
* [https://pub.dev/packages/mvc_rocket/changelog](old Changelogs)

## 0.0.2
* Removed `state` from `builder` method
* Changed `loader` to `onLoading`
4 changes: 2 additions & 2 deletions packages/rocket_view/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class TodoList extends StatelessWidget {
return RocketView(
model: todoModel,
call: () => GetTodos.getTodos(todoModel),
loader: const CircularProgressIndicator(),
loader: (context) => Center(child: CircularProgressIndicator()),
callType: CallType.callIfModelEmpty,
builder: (context, state) {
builder: (context) {
return ListView.builder(
itemCount: todoModel.all!.length,
itemBuilder: (context, index) {
Expand Down
7 changes: 3 additions & 4 deletions packages/rocket_view/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RocketViewExample extends StatelessWidget {
// Pass the Todo object to the RocketView widget as the model
model: todos,
// Define how the Todo list should be displayed
builder: (context, state) {
builder: (context) {
return ListView.builder(
itemCount: todos.all!.length,
itemBuilder: (context, index) {
Expand Down Expand Up @@ -97,10 +97,9 @@ class RocketViewExample extends StatelessWidget {
// Define how to handle errors
onError: (error, reload) => Text('Error: ${error.exception}'),
// Add a loading indicator while the Todo list is being fetched
loader: const Center(
onLoading: (BuildContext context) => Center(
child: CircularProgressIndicator(
color: Colors.red,
),
color: Theme.of(context).colorScheme.primary),
),
),
);
Expand Down
23 changes: 13 additions & 10 deletions packages/rocket_view/lib/src/rocket_view_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'enums.dart';

typedef OnError = Widget Function(RocketException error, Function() reload);

typedef OnLoading = Widget Function(BuildContext);

class RocketView<T> extends StatefulWidget {
/// A widget that helps to manage the state of a `RocketModel` and handle the different states of the data.
///
Expand Down Expand Up @@ -64,7 +66,7 @@ class RocketView<T> extends StatefulWidget {
this.call = _myDefaultFunc,
this.callType = CallType.callAsFuture,
this.secondsOfStream = 1,
this.loader,
this.onLoading = _defaultOnLoading,
this.onError = _defaultOnError,
}) : super(key: key) {
/// Call the `call` function based on the `callType` parameter.
Expand Down Expand Up @@ -112,8 +114,14 @@ class RocketView<T> extends StatefulWidget {
);
}

static Widget _defaultOnLoading(BuildContext context) {
return const Center(
child: CircularProgressIndicator(),
);
}

/// The function that builds the widget tree based on the state.
final Widget Function(BuildContext, RocketState) builder;
final Widget Function(BuildContext) builder;

/// The function that fetches data.
final dynamic Function() call;
Expand All @@ -125,7 +133,7 @@ class RocketView<T> extends StatefulWidget {
final int secondsOfStream;

/// The widget to display while data is loading.
final Widget? loader;
final OnLoading onLoading;

/// The `RocketModel` object that holds the data and state.
final RocketModel<T> model;
Expand Down Expand Up @@ -212,21 +220,16 @@ class ViewRocketState extends State<RocketView> {
_handleStates() {
switch (widget.model.state) {
case RocketState.loading:
return Center(child: widget.loader);
return widget.onLoading(context);
case RocketState.done:
return widget.builder(context, widget.model.state);
return widget.builder(context);
case RocketState.failed:
return widget.onError(widget.model.exception, reload);
}
}

@override
Widget build(BuildContext context) {
/// Call the builder function if the loader is `null`.
if (widget.loader == null) {
return widget.builder(context, widget.model.state);
}

/// Return the appropriate widget tree based on the `RocketState`.
return _handleStates();
}
Expand Down
3 changes: 2 additions & 1 deletion packages/rocket_view/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: rocket_view
description: Package View widget based on RocketModel.
version: 0.0.1
version: 0.0.2
repository: https://github.com/JahezAcademy/flutter_rocket/tree/dev/packages/rocket_view
issue_tracker: https://github.com/JahezAcademy/flutter_rocket/labels/rocket_view
homepage: https://github.com/JahezAcademy/flutter_rocket
publish_to: 'none'

environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down
18 changes: 7 additions & 11 deletions packages/rocket_view/test/rocket_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ void main() {
MaterialApp(
home: RocketView<int>(
model: rocketModel,
builder: (context, state) => const Text('Data is loading...'),
loader: const CircularProgressIndicator(),
builder: (context) => const Text('Data is loading...'),
),
),
);
Expand All @@ -38,7 +37,7 @@ void main() {
MaterialApp(
home: RocketView<int>(
model: rocketModel,
builder: (context, state) => Text('Data is ${rocketModel.data}'),
builder: (context) => Text('Data is ${rocketModel.data}'),
),
),
);
Expand All @@ -62,9 +61,8 @@ void main() {
response: 'Data not found',
);
},
builder: (context, state) => Text('Data is ${rocketModel.data}'),
builder: (context) => Text('Data is ${rocketModel.data}'),
onError: (error, reload) => Text('Error: ${error.exception}'),
loader: const CircularProgressIndicator(),
),
),
);
Expand All @@ -86,12 +84,11 @@ void main() {
home: RocketView<int>(
model: rocketModel,
call: () async {},
builder: (context, state) => Text('Data is ${rocketModel.data}'),
builder: (context) => Text('Data is ${rocketModel.data}'),
onError: (error, reload) => TextButton(
onPressed: reload,
child: Text('Error: ${error.exception}. Tap to retry.'),
),
loader: const CircularProgressIndicator(),
),
),
);
Expand All @@ -100,7 +97,6 @@ void main() {
expect(rocketModel.state == RocketState.loading, isTrue);
expect(rocketModel.exception.statusCode, equals(404));
});
;

testWidgets('RocketView calls call function when CallType is callAsFuture',
(WidgetTester tester) async {
Expand All @@ -111,7 +107,7 @@ void main() {
MaterialApp(
home: RocketView<int>(
model: rocketModel,
builder: (context, state) => Text('Data is ${rocketModel.data}'),
builder: (context) => Text('Data is ${rocketModel.data}'),
call: () {
callFunctionCalled = true;
},
Expand All @@ -136,7 +132,7 @@ void main() {
MaterialApp(
home: RocketView<int>(
model: rocketModel,
builder: (context, state) => Text('Data is ${rocketModel.data}'),
builder: (context) => Text('Data is ${rocketModel.data}'),
call: () {
callFunctionCalled = true;
},
Expand All @@ -160,7 +156,7 @@ void main() {
MaterialApp(
home: RocketView<int>(
model: rocketModel,
builder: (context, state) => Text('Data is ${rocketModel.data}'),
builder: (context) => Text('Data is ${rocketModel.data}'),
call: () {
callFunctionCalled = true;
},
Expand Down