-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: getting data on schedule screen and wip schedule state mgmt
- Loading branch information
1 parent
a15a2d0
commit 8e8adc3
Showing
10 changed files
with
297 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:method_conf_app/data/umbraco/models/schedule_grid.dart'; | ||
|
||
import 'package:method_conf_app/env.dart'; | ||
|
||
Future<List<List<String?>>> getScheduleGrid(String conferenceId) async { | ||
var url = Uri.parse( | ||
'${Env.umbracoBaseUrl}/api/v1/conference/$conferenceId/schedule'); | ||
|
||
final res = await http.get(url); | ||
|
||
return ScheduleGrid.fromJson(json.decode(res.body)).scheduleGrid; | ||
} |
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,28 @@ | ||
import 'package:method_conf_app/data/umbraco/get_child_nodes_of_type.dart'; | ||
import 'package:method_conf_app/data/umbraco/get_items.dart'; | ||
import 'package:method_conf_app/data/umbraco/models/api_content_model_base.dart'; | ||
import 'package:method_conf_app/data/umbraco/models/session.dart'; | ||
import 'package:method_conf_app/data/umbraco/models/track.dart'; | ||
|
||
const maximumScheduleItems = 100; | ||
|
||
Future<List<ApiContentModelBase>> getScheduleItems(conferenceId) async { | ||
final sessions = await getFirstChildNodeOfType( | ||
nodeId: conferenceId, | ||
type: 'sessions', | ||
); | ||
|
||
if (sessions == null) { | ||
return []; | ||
} | ||
|
||
var response = await getItems( | ||
fetch: 'descendants:${sessions.id}', | ||
expand: 'properties[\$all]', | ||
take: 100, | ||
); | ||
|
||
return response.items | ||
.where((item) => item is Track || item is Session) | ||
.toList(); | ||
} |
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
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,118 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:method_conf_app/data/get_schedule_grid.dart'; | ||
import 'package:method_conf_app/data/get_schedule_items.dart'; | ||
import 'package:method_conf_app/data/umbraco/models/api_content_model_base.dart'; | ||
import 'package:method_conf_app/data/umbraco/models/session.dart'; | ||
import 'package:method_conf_app/data/umbraco/models/track.dart'; | ||
import 'package:method_conf_app/providers/conference_provider.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
const _scheduleItemsStorageKey = 'app-schedule-items'; | ||
const _scheduleGridStorageKey = 'app-schedule-grid'; | ||
|
||
typedef Schedule = (List<ApiContentModelBase>, List<List<String?>>); | ||
|
||
class ScheduleProvider extends ChangeNotifier { | ||
ConferenceProvider conferenceProvider; | ||
|
||
Schedule? _schedule; | ||
|
||
Schedule? get schedule => _schedule; | ||
|
||
set schedule(Schedule? value) { | ||
_schedule = value; | ||
notifyListeners(); | ||
} | ||
|
||
List<List<String?>> get grid => schedule?.$2 ?? []; | ||
|
||
List<Track> get tracks => schedule?.$1.whereType<Track>().toList() ?? []; | ||
|
||
List<Session> get sessions => | ||
schedule?.$1.whereType<Session>().toList() ?? []; | ||
|
||
ScheduleProvider({required this.conferenceProvider}); | ||
|
||
Future<void> init() async { | ||
await conferenceProvider.init(enableBackgroundRefresh: false); | ||
|
||
schedule ??= await load(); | ||
|
||
if (schedule == null) { | ||
await refresh(); | ||
} else { | ||
refresh(); | ||
} | ||
} | ||
|
||
Future<Schedule?> load() async { | ||
var prefs = await SharedPreferences.getInstance(); | ||
|
||
final itemsJson = prefs.getStringList(_scheduleItemsStorageKey); | ||
final gridJson = prefs.getStringList(_scheduleGridStorageKey); | ||
|
||
final items = itemsJson | ||
?.map((item) => ApiContentModelBase.fromJson(json.decode(item))) | ||
.toList(); | ||
|
||
final grid = | ||
gridJson?.map((item) => json.decode(item) as List<String?>).toList(); | ||
|
||
if (items == null || grid == null) { | ||
return null; | ||
} | ||
|
||
return (items, grid); | ||
} | ||
|
||
Future<void> store(Schedule? newSchedule) async { | ||
var prefs = await SharedPreferences.getInstance(); | ||
|
||
if (newSchedule == null) { | ||
await Future.wait([ | ||
prefs.remove(_scheduleItemsStorageKey), | ||
prefs.remove(_scheduleGridStorageKey), | ||
]); | ||
return; | ||
} | ||
|
||
final (items, grid) = newSchedule; | ||
|
||
await Future.wait([ | ||
prefs.setStringList( | ||
_scheduleItemsStorageKey, | ||
items.map((item) => json.encode(item.toJson())).toList(), | ||
), | ||
prefs.setStringList( | ||
_scheduleGridStorageKey, | ||
grid.map((item) => json.encode(item)).toList(), | ||
), | ||
]); | ||
} | ||
|
||
Future<Schedule?> fetch() async { | ||
var conference = conferenceProvider.conference; | ||
|
||
if (conference == null) { | ||
return null; | ||
} | ||
|
||
final itemsFuture = getScheduleItems(conference.id); | ||
final gridFuture = getScheduleGrid(conference.id); | ||
|
||
final items = await itemsFuture; | ||
final grid = await gridFuture; | ||
|
||
return (items, grid); | ||
} | ||
|
||
Future<void> refresh() async { | ||
final newSchedule = await fetch(); | ||
|
||
schedule = newSchedule; | ||
|
||
await store(newSchedule); | ||
} | ||
} |
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,72 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:method_conf_app/data/umbraco/models/session.dart'; | ||
import 'package:method_conf_app/data/umbraco/models/track.dart'; | ||
import 'package:method_conf_app/providers/schedule_provider.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
const _startColumnIndexStorageKey = 'app-schedule-start-column-index'; | ||
|
||
class ScheduleStateProvider extends ChangeNotifier { | ||
ScheduleProvider scheduleProvider; | ||
|
||
int _visibleColumns = 1; | ||
|
||
int get visibleColumns => _visibleColumns; | ||
|
||
set visibleColumns(int value) { | ||
_visibleColumns = value; | ||
notifyListeners(); | ||
} | ||
|
||
int _startColumnIndex = 0; | ||
|
||
int get startColumnIndex => _startColumnIndex; | ||
|
||
set startColumnIndex(int value) { | ||
_startColumnIndex = value; | ||
notifyListeners(); | ||
store(_startColumnIndex); | ||
} | ||
|
||
Track? get currentTrack => | ||
scheduleProvider.tracks.elementAtOrNull(startColumnIndex); | ||
|
||
List<Session> get currentSessions => | ||
scheduleProvider.grid | ||
.elementAtOrNull(startColumnIndex) | ||
?.toSet() | ||
.whereType<String>() | ||
.map((gridId) => scheduleProvider.sessions | ||
.firstWhereOrNull((session) => session.gridId == gridId)) | ||
.whereType<Session>() | ||
.toList() ?? | ||
[]; | ||
|
||
ScheduleStateProvider({required this.scheduleProvider}); | ||
|
||
Future<void> init() async { | ||
final storedStartColumnIndex = await load(); | ||
|
||
if (storedStartColumnIndex != null) { | ||
startColumnIndex = storedStartColumnIndex; | ||
} | ||
} | ||
|
||
Future<int?> load() async { | ||
var prefs = await SharedPreferences.getInstance(); | ||
|
||
return prefs.getInt(_startColumnIndexStorageKey); | ||
} | ||
|
||
Future<void> store(int? newStartColumnIndex) async { | ||
var prefs = await SharedPreferences.getInstance(); | ||
|
||
if (newStartColumnIndex == null) { | ||
await prefs.remove(_startColumnIndexStorageKey); | ||
return; | ||
} | ||
|
||
await prefs.setInt(_startColumnIndexStorageKey, newStartColumnIndex); | ||
} | ||
} |
Oops, something went wrong.