Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cog 81 widget todo list #95

Merged
merged 2 commits into from
Nov 1, 2020
Merged
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
16 changes: 16 additions & 0 deletions lib/models/todo_item_payload.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class TodoUpdatePayload {
String id;
String selectedItem;

TodoUpdatePayload({
this.id,
this.selectedItem,
});

Map toJson() {
return {
"id": id,
"selectedItem": selectedItem,
};
}
}
26 changes: 26 additions & 0 deletions lib/models/todo_list_item_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class TodoListItemModel {
String id;
String itemText;

TodoListItemModel({
this.id,
this.itemText,
});

factory TodoListItemModel.fromJson(Map<String, dynamic> json) => TodoListItemModel(
id: json['id'],
itemText: json['itemText'],
);

Map toJson() {
return {
"id": id,
"itemText": itemText,
};
}

factory TodoListItemModel.deepCopy(TodoListItemModel model) => new TodoListItemModel(
id: model.id,
itemText: model.itemText,
);
}
6 changes: 6 additions & 0 deletions lib/models/widget_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cogboardmobileapp/models/todo_list_item_model.dart';
import 'package:cogboardmobileapp/models/widget_config_model.dart';

enum WidgetStatus {
Expand Down Expand Up @@ -26,6 +27,7 @@ class DashboardWidget {
DateTime expirationDate;
int maxValue;
String selectedZabbixMetric;
List toDoListItems;

static const WIDGET_STATUS_KEY = 'widgetStatus';

Expand All @@ -39,6 +41,7 @@ class DashboardWidget {
this.expirationDate,
this.maxValue,
this.selectedZabbixMetric,
this.toDoListItems,
});

factory DashboardWidget.fromJson(Map<String, dynamic> json) => DashboardWidget(
Expand All @@ -52,6 +55,7 @@ class DashboardWidget {
: null,
maxValue: (json['maxValue']),
selectedZabbixMetric: json['selectedZabbixMetric'],
toDoListItems: json['toDoListItems'],
content: (json['content']) as Map<String, dynamic>);

factory DashboardWidget.deepCopy(DashboardWidget dashboardWidget) => new DashboardWidget(
Expand All @@ -62,6 +66,7 @@ class DashboardWidget {
disabled: dashboardWidget.disabled,
maxValue: dashboardWidget.maxValue,
selectedZabbixMetric: dashboardWidget.selectedZabbixMetric,
toDoListItems: dashboardWidget.toDoListItems,
content: new Map<String, dynamic>.from(dashboardWidget.content),
);

Expand All @@ -75,6 +80,7 @@ class DashboardWidget {
"expirationDate": expirationDate.toString(),
"maxValue": maxValue,
"selectedZabbixMetric": selectedZabbixMetric,
"toDoListItems": toDoListItems,
"content": content,
};
}
Expand Down
84 changes: 84 additions & 0 deletions lib/widgets/widgets/todo_list/todo_list_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import 'dart:convert';

import 'package:cogboardmobileapp/models/todo_item_payload.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class TodoListItem extends StatefulWidget {
final String id;
final String widgetId;
final String itemText;
final bool initiallySelected;

TodoListItem({
this.id,
this.widgetId,
this.itemText,
this.initiallySelected,
});

@override
_TodoListItemState createState() => _TodoListItemState();
}

class _TodoListItemState extends State<TodoListItem> {
bool isSelected;

@override
void initState() {
super.initState();
this.isSelected = widget.initiallySelected;
}

@override
void didUpdateWidget(TodoListItem oldWidget) {
super.didUpdateWidget(oldWidget);
setState(() {
isSelected = widget.initiallySelected;
});
}

Future<void> updateItem() async {
const url = 'http://150.254.30.119/api/widget/contentUpdate';
TodoUpdatePayload payload = TodoUpdatePayload(
id: widget.widgetId,
selectedItem: widget.id,
);
await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(payload),
);
}

void handleCheckboxUpdate() {
setState(() {
isSelected = !isSelected;
});
updateItem();
}

@override
Widget build(BuildContext context) {
return GestureDetector(
child: Row(
children: [
Checkbox(
value: isSelected,
onChanged: null,
),
Text(
widget.itemText,
style: TextStyle(
fontSize: 17,
decoration: isSelected ? TextDecoration.lineThrough : null,
),
),
],
),
onTap: handleCheckboxUpdate,
);
}
}
59 changes: 59 additions & 0 deletions lib/widgets/widgets/todo_list/todo_list_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:cogboardmobileapp/models/todo_list_item_model.dart';
import 'package:cogboardmobileapp/models/widget_model.dart';
import 'package:cogboardmobileapp/widgets/widgets/details_container.dart';
import 'package:cogboardmobileapp/widgets/widgets/todo_list/todo_list_item.dart';
import 'package:flutter/material.dart';

class TodoListWidget extends StatelessWidget {
final DashboardWidget widget;

TodoListWidget({
this.widget,
}) {
getTodoItems;
}

List<TodoListItemModel> get getTodoItems {
var todoItems = widget.toDoListItems;
var notSelectedItems = todoItems
.where((item) => !getSelectedItems.contains(
item['id'],
))
.toList();
var selectedItems = todoItems
.where((item) => getSelectedItems.contains(
item['id'],
))
.toList();
var itemsOrder = [...notSelectedItems, ...selectedItems];
return itemsOrder
.map((item) => TodoListItemModel(
itemText: item['itemText'],
id: item['id'],
))
.toList();
}

List get getSelectedItems {
return widget.content['selectedItems'];
}

@override
Widget build(BuildContext context) {
return Container(
child: DetailsContainer(
children: [
...getTodoItems
.map((item) => TodoListItem(
itemText: item.itemText,
id: item.id,
widgetId: widget.id,
initiallySelected: getSelectedItems.contains(item.id),
))
.toList(),
],
),
margin: const EdgeInsets.fromLTRB(15.0, 20.0, 0, 0),
);
}
}
4 changes: 4 additions & 0 deletions lib/widgets/widgets/widget_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:cogboardmobileapp/widgets/widgets/jenkins_job/jenkins_job_widget
import 'package:cogboardmobileapp/widgets/widgets/jira_buckets/jira_buckets_widget.dart';
import 'package:cogboardmobileapp/widgets/widgets/link_list/link_list_widget.dart';
import 'package:cogboardmobileapp/widgets/widgets/sonar_qube/sonar_qube_widget.dart';
import 'package:cogboardmobileapp/widgets/widgets/todo_list/todo_list_widget.dart';
import 'package:cogboardmobileapp/widgets/widgets/world_clock/world_clock_widget.dart';
import 'package:cogboardmobileapp/widgets/widgets/zabbix/zabbix_widget.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -44,6 +45,8 @@ class WidgetDetails extends StatelessWidget {
return ZabbixWidget(widget: widget);
case WidgetTypes.LINK_LIST_WIDGET:
return LinkListWidget(widget: widget);
case WidgetTypes.TODO_LIST_WIDGET:
return TodoListWidget(widget: widget);
default:
return Container(
child: null,
Expand All @@ -61,6 +64,7 @@ class WidgetDetails extends StatelessWidget {
case WidgetTypes.AEM_BUNDLE_INFO_WIDGET:
case WidgetTypes.JIRA_BUCKETS_WIDGET:
case WidgetTypes.LINK_LIST_WIDGET:
case WidgetTypes.TODO_LIST_WIDGET:
return true;
case WidgetTypes.CHECKBOX_WIDGET:
case WidgetTypes.WORLD_CLOCK_WIDGET:
Expand Down