Skip to content

Commit

Permalink
feat: add flutter_deck_client package
Browse files Browse the repository at this point in the history
  • Loading branch information
mkobuolys committed Apr 7, 2024
1 parent 4893801 commit d779f37
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/flutter_deck_client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 0.1.0

- feat: add flutter_deck_client
21 changes: 21 additions & 0 deletions packages/flutter_deck_client/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Mangirdas Kazlauskas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions packages/flutter_deck_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# flutter_deck_client

A common client interface and models for the [flutter_deck](https://pub.dev/packages/flutter_deck) package.

## Usage

To implement a new client for [flutter_deck](https://pub.dev/packages/flutter_deck), implement [FlutterDeckClient](https://github.com/mkobuolys/flutter_deck/tree/main/packages/flutter_deck_client/lib/src/flutter_deck_client.dart) interface with your own logic:

```dart
class MyFlutterDeckClient implements FlutterDeckClient {
const MyFlutterDeckClient();
@override
Stream<FlutterDeckState> get flutterDeckStateStream { <...> }
@override
void init([FlutterDeckState? state]) { <...> }
@override
void dispose() { <...> }
@override
void updateState(FlutterDeckState state) { <...> }
}
```

## Note on breaking changes

Strongly prefer non-breaking changes (such as adding a method to the interface) over breaking changes for this package.

See https://flutter.dev/go/platform-interface-breaking-changes for a discussion on why a less-clean interface is preferable to a breaking change.
1 change: 1 addition & 0 deletions packages/flutter_deck_client/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../../analysis_options.yaml
5 changes: 5 additions & 0 deletions packages/flutter_deck_client/lib/flutter_deck_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// A common client interface and models for the flutter_deck package.
library flutter_deck_client;

export 'src/flutter_deck_client.dart';
export 'src/models/models.dart';
31 changes: 31 additions & 0 deletions packages/flutter_deck_client/lib/src/flutter_deck_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter_deck_client/src/models/models.dart';

/// A common client interface for the flutter_deck package.
///
/// To implement a custom client, implement this interface and use it in your
/// flutter_deck presentation.
abstract interface class FlutterDeckClient {
/// Initializes the client with the given [state].
///
/// In flutter_deck, the presentation will call this method with the initial
/// state of the presentation. The presenter view will also call this method,
/// but with a null state.
void init([FlutterDeckState? state]);

/// Disposes the client.
///
/// This method should be called when the client is no longer needed.
void dispose();

/// Updates the client state with the given [state].
///
/// This method is called by both the presentation and the presenter view to
/// keep the [FlutterDeckState] in sync.
void updateState(FlutterDeckState state);

/// Returns the state as a stream of [FlutterDeckState] objects.
///
/// This stream is used by flutter_deck framework to keep the presentation and
/// the presenter view in sync.
Stream<FlutterDeckState> get flutterDeckStateStream;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'package:meta/meta.dart';

/// Represents the state of a flutter_deck presentation.
///
/// This class is a single source of truth for the state of a flutter_deck
/// presentation. It is used by both the presentation and the presenter view to
/// keep the state in sync.
@immutable
class FlutterDeckState {
/// Creates a new [FlutterDeckState] with the given parameters.
///
/// The [locale] and [themeMode] parameters are required.
///
/// The [markerEnabled], [slideIndex], and [slideStep] parameters are
/// optional.
const FlutterDeckState({
required this.locale,
required this.themeMode,
this.markerEnabled = false,
this.slideIndex = 0,
this.slideStep = 1,
});

/// Creates a new [FlutterDeckState] from a JSON object.
FlutterDeckState.fromJson(Map<String, dynamic> json)
: locale = json['locale'] as String,
markerEnabled = json['markerEnabled'] as bool,
slideIndex = json['slideIndex'] as int,
slideStep = json['slideStep'] as int,
themeMode = json['themeMode'] as String;

/// Converts this [FlutterDeckState] to a JSON object.
Map<String, dynamic> toJson() => {
'locale': locale,
'markerEnabled': markerEnabled,
'slideIndex': slideIndex,
'slideStep': slideStep,
'themeMode': themeMode,
};

/// The locale of the presentation.
final String locale;

/// Whether the marker is enabled.
final bool markerEnabled;

/// The index of the current slide.
final int slideIndex;

/// The step of the current slide.
final int slideStep;

/// The theme mode of the presentation.
final String themeMode;

/// Creates a copy of this [FlutterDeckState] with the given parameters
FlutterDeckState copyWith({
String? locale,
bool? markerEnabled,
int? slideIndex,
int? slideStep,
String? themeMode,
}) {
return FlutterDeckState(
locale: locale ?? this.locale,
markerEnabled: markerEnabled ?? this.markerEnabled,
slideIndex: slideIndex ?? this.slideIndex,
slideStep: slideStep ?? this.slideStep,
themeMode: themeMode ?? this.themeMode,
);
}

/// Overrides the equality operator to compare two [FlutterDeckState] objects.
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is FlutterDeckState &&
other.locale == locale &&
other.markerEnabled == markerEnabled &&
other.slideIndex == slideIndex &&
other.slideStep == slideStep &&
other.themeMode == themeMode;
}

/// Overrides the hash code getter to return the hash code of this object.
@override
int get hashCode =>
locale.hashCode ^
markerEnabled.hashCode ^
slideIndex.hashCode ^
slideStep.hashCode ^
themeMode.hashCode;
}
1 change: 1 addition & 0 deletions packages/flutter_deck_client/lib/src/models/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'flutter_deck_state.dart';
21 changes: 21 additions & 0 deletions packages/flutter_deck_client/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: flutter_deck_client
description: A common client interface and models for the flutter_deck package.
version: 0.1.0
homepage: https://github.com/mkobuolys/flutter_deck
repository: https://github.com/mkobuolys/flutter_deck/tree/main/packages/flutter_deck_client
issue_tracker: https://github.com/mkobuolys/flutter_deck/issues
topics:
- flutterdeck
- presentation
- slides
funding:
- https://github.com/sponsors/mkobuolys

environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
meta: ^1.11.0

dev_dependencies:
very_good_analysis: ^5.1.0

0 comments on commit d779f37

Please sign in to comment.