Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
[MSE-178] Add <, > and x controls to browser pt.2 (#14)
Browse files Browse the repository at this point in the history
* feat: Add host and isHttp fields to the browser controller.

* test: Add BrowserController tests

* ci: v0.0.4

* fix: Webview's dialog background

* wip: Fix color

* wip: Background color is ignored on iOS. Trying showAdaptiveDialog

* fix: Make example closer to a real HS app - disable Material3 theme.

* fix: Return more flexible variant without SafeArea and Scaffold

* fix: Add SafeArea and Scaffold to AdaChatScreen

* fix: Implement HS colors for the themes
  • Loading branch information
slava-r-epam authored Jun 17, 2024
1 parent 6ad9f23 commit 4a3dadd
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/ada_chat_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.4

- Add host and isHttp fields to the browser controller.

## 0.0.3

- Add browserSettings with webview page builder.
Expand Down
12 changes: 12 additions & 0 deletions packages/ada_chat_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) => MaterialApp(
theme: ThemeData.from(
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.orange),
useMaterial3: false,
),
darkTheme: ThemeData.from(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.orange,
brightness: Brightness.dark,
),
useMaterial3: false,
),
themeMode: ThemeMode.system,
onGenerateRoute: (route) {
switch (route.name) {
case '/ada':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ class _AdaChatScreenState extends State<AdaChatScreen> {
setState(() => _progress = 0);
},
browserSettings: BrowserSettings(
pageBuilder: (context, browser, controller) =>
PageWithControls(
controller: controller,
child: browser,
pageBuilder: (context, browser, controller) => Scaffold(
body: SafeArea(
child: PageWithControls(
controller: controller,
child: browser,
),
),
),
),
onLoaded: (data) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,30 @@ class _PageControlsState extends State<PageControls> {
onPressed: widget.controller.reload,
),
Expanded(
child: Text(
widget.controller.title,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodySmall,
child: Column(
children: [
Text(
widget.controller.title,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodySmall,
),
Row(
children: [
Icon(
widget.controller.isHttps
? Icons.lock
: Icons.lock_open,
size: 10,
),
const SizedBox(width: 8),
Text(
widget.controller.host,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.bodyMedium,
),
],
),
],
),
),
IconButton(
Expand Down
3 changes: 2 additions & 1 deletion packages/ada_chat_flutter/lib/src/ada_web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ console.log("adaSettings: " + JSON.stringify(window.adaSettings));
}

unawaited(
showDialog(
showAdaptiveDialog(
context: context,
barrierColor: Colors.transparent,
builder: (context) => CustomizedWebView(
url: url,
browserSettings: widget.browserSettings,
Expand Down
16 changes: 16 additions & 0 deletions packages/ada_chat_flutter/lib/src/browser_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:flutter_inappwebview/flutter_inappwebview.dart';
class BrowserController extends ChangeNotifier {
InAppWebViewController? _controller;
String _title = '';
String _host = '';
bool _isHttps = true;
bool _backIsAvailable = false;
bool _forwardIsAvailable = false;

Expand All @@ -22,6 +24,20 @@ class BrowserController extends ChangeNotifier {
notifyListeners();
}

String get host => _host;

void setHost(String host) {
_host = host;
notifyListeners();
}

bool get isHttps => _isHttps;

void setIsHttps(bool isHttps) {
_isHttps = isHttps;
notifyListeners();
}

bool get backIsAvailable => _backIsAvailable;

void setBackIsAvailable(bool isAvailable) {
Expand Down
5 changes: 5 additions & 0 deletions packages/ada_chat_flutter/lib/src/customized_web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class _CustomizedWebViewState extends State<CustomizedWebView> {
final title = await webViewController.getTitle();
widget.browserSettings?.control?.setTitle(title ?? '');

final webUri = await webViewController.getUrl();
widget.browserSettings?.control?.setHost(webUri?.host ?? '');
widget.browserSettings?.control
?.setIsHttps(webUri?.isScheme('https') ?? false);

final canGoBack = await webViewController.canGoBack();
widget.browserSettings?.control?.setBackIsAvailable(canGoBack);

Expand Down
2 changes: 1 addition & 1 deletion packages/ada_chat_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ada_chat_flutter
description: Flutter implementation of Ada chat
version: 0.0.3
version: 0.0.4

environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down
17 changes: 16 additions & 1 deletion packages/ada_chat_flutter/test/src/browser_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ void main() {
expect(browserController.title, newTitle);
});

test('host getter returns correct value', () {
const newHost = 'New Host';
browserController.setHost(newHost);
expect(browserController.host, newHost);
});

test('isHttps getter returns correct value', () {
browserController.setIsHttps(false);
expect(browserController.isHttps, false);
browserController.setIsHttps(true);
expect(browserController.isHttps, true);
});

test('backIsAvailable getter returns correct value', () {
browserController.setBackIsAvailable(true);
expect(browserController.backIsAvailable, true);
Expand All @@ -57,10 +70,12 @@ void main() {
browserController.addListener(() => listenerCallCount++);

browserController.setTitle('New Title');
browserController.setHost('New Host');
browserController.setIsHttps(false);
browserController.setBackIsAvailable(true);
browserController.setForwardIsAvailable(false);

expect(listenerCallCount, 3);
expect(listenerCallCount, 5);
});
});
}

0 comments on commit 4a3dadd

Please sign in to comment.