Skip to content

Commit

Permalink
Merge pull request #1964 from c3bryant/scroll-position-regression
Browse files Browse the repository at this point in the history
Revert "Merge pull request #1935 from tkiyohar/addScrollPositionSaving"
  • Loading branch information
c3bryant authored Jul 31, 2023
2 parents 870e164 + cf88326 commit 67dd6bb
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 343 deletions.
7 changes: 4 additions & 3 deletions lib/app_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import 'package:campus_mobile_experimental/core/providers/weather.dart';
import 'package:campus_mobile_experimental/ui/navigator/top.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';

Expand Down Expand Up @@ -150,6 +149,10 @@ List<SingleChildWidget> dependentServices = [
userDataProvider.cardsDataProvider = cardsDataProvider;
cardsDataProvider
..loadSavedData().then((value) {
// Update available cards
cardsDataProvider.updateAvailableCards(
userDataProvider.authenticationModel!.ucsdaffiliation);

// Student card activation
if (userDataProvider.isLoggedIn &&
(userDataProvider.userProfileModel!.classifications?.student ??
Expand Down Expand Up @@ -259,12 +262,10 @@ List<SingleChildWidget> dependentServices = [
ChangeNotifierProxyProvider<UserDataProvider, MessagesDataProvider>(
create: (_) {
var messageDataProvider = MessagesDataProvider();
debugPrint("ChangeNotifierProxyProvider");
return messageDataProvider;
},
lazy: false,
update: (_, userDataProvider, messageDataProvider) {
debugPrint("ChangeNotifierProxyProvider");
messageDataProvider!.userDataProvider = userDataProvider;
messageDataProvider.fetchMessages(true);
return messageDataProvider;
Expand Down
20 changes: 7 additions & 13 deletions lib/core/providers/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import 'package:campus_mobile_experimental/core/providers/user.dart';
import 'package:campus_mobile_experimental/core/services/messages.dart';
import 'package:flutter/material.dart';

import '../../ui/navigator/bottom.dart';

//MESSAGES API UNIX TIMESTAMPS IN MILLISECONDS NOT SECONDS

ScrollController notificationScrollController = ScrollController();

class MessagesDataProvider extends ChangeNotifier {
MessagesDataProvider() {
/// DEFAULT STATES
Expand All @@ -18,15 +14,16 @@ class MessagesDataProvider extends ChangeNotifier {
_messageService = MessageService();
_statusText = NotificationsConstants.statusFetching;
_hasMoreMessagesToLoad = false;
notificationScrollController.addListener(() {
_scrollController = ScrollController();
_scrollController!.addListener(() {
var triggerFetchMoreSize =
0.9 * notificationScrollController.position.maxScrollExtent;
if (notificationScrollController.position.pixels > triggerFetchMoreSize) {
0.9 * _scrollController!.position.maxScrollExtent;

if (_scrollController!.position.pixels > triggerFetchMoreSize) {
if (!_isLoading! && _hasMoreMessagesToLoad!) {
fetchMessages(false);
}
}
setNotificationsScrollOffset(notificationScrollController.offset);
});
}

Expand All @@ -37,6 +34,7 @@ class MessagesDataProvider extends ChangeNotifier {
int? _previousTimestamp;
String? _statusText;
bool? _hasMoreMessagesToLoad;
ScrollController? _scrollController;

/// MODELS
List<MessageElement?>? _messages;
Expand Down Expand Up @@ -167,15 +165,11 @@ class MessagesDataProvider extends ChangeNotifier {

/// SIMPLE GETTERS
bool? get isLoading => _isLoading;

String? get error => _error;

DateTime? get lastUpdated => _lastUpdated;

String? get statusText => _statusText;

bool? get hasMoreMessagesToLoad => _hasMoreMessagesToLoad;

ScrollController? get scrollController => _scrollController;
UserDataProvider? get userDataProvider => _userDataProvider;

List<MessageElement?>? get messages {
Expand Down
7 changes: 2 additions & 5 deletions lib/core/providers/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:campus_mobile_experimental/core/providers/cards.dart';
import 'package:campus_mobile_experimental/core/providers/notifications.dart';
import 'package:campus_mobile_experimental/core/services/authentication.dart';
import 'package:campus_mobile_experimental/core/services/user.dart';
import 'package:campus_mobile_experimental/ui/navigator/bottom.dart';
import 'package:encrypt/encrypt.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -214,8 +213,7 @@ class UserDataProvider extends ChangeNotifier {
if (username != null && encryptedPassword != null) {
final String base64EncodedWithEncryptedPassword =
base64.encode(utf8.encode(username + ':' + encryptedPassword));
resetHomeScrollOffset();
resetNotificationsScrollOffset();

if (await _authenticationService
.silentLogin(base64EncodedWithEncryptedPassword)) {
await updateAuthenticationModel(_authenticationService.data);
Expand Down Expand Up @@ -247,8 +245,6 @@ class UserDataProvider extends ChangeNotifier {
_error = null;
_isLoading = true;
notifyListeners();
resetHomeScrollOffset();
resetNotificationsScrollOffset();
_pushNotificationDataProvider
.unregisterDevice(_authenticationModel!.accessToken);
updateAuthenticationModel(AuthenticationModel.fromJson({}));
Expand All @@ -261,6 +257,7 @@ class UserDataProvider extends ChangeNotifier {
await box.clear();
await FirebaseAnalytics().logEvent(name: 'loggedOut');
_isLoading = false;

notifyListeners();
}

Expand Down
32 changes: 16 additions & 16 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,23 @@ class CampusMobile extends StatelessWidget {
return MultiProvider(
providers: providers,
child: GetMaterialApp(
debugShowCheckedModeBanner: true,
title: 'UC San Diego',
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(secondary: darkAccentColor),
),
darkTheme: darkTheme.copyWith(
colorScheme:
darkTheme.colorScheme.copyWith(secondary: lightAccentColor),
),
initialRoute: showOnboardingScreen
? RoutePaths.OnboardingInitial
: RoutePaths.BottomNavigationBar,
onGenerateRoute: campusMobileRouter.Router.generateRoute,
navigatorObservers: [
observer,
],
debugShowCheckedModeBanner: true,
title: 'UC San Diego',
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(secondary: darkAccentColor),
),
darkTheme: darkTheme.copyWith(
colorScheme:
darkTheme.colorScheme.copyWith(secondary: lightAccentColor),
),
initialRoute: showOnboardingScreen
? RoutePaths.OnboardingInitial
: RoutePaths.BottomNavigationBar,
onGenerateRoute: campusMobileRouter.Router.generateRoute,
navigatorObservers: [
observer,
],
),
);
}
}
33 changes: 7 additions & 26 deletions lib/ui/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import 'package:campus_mobile_experimental/ui/events/events_card.dart';
import 'package:campus_mobile_experimental/ui/finals/finals_card.dart';
import 'package:campus_mobile_experimental/ui/my_chart/my_chart_card.dart';
import 'package:campus_mobile_experimental/ui/myucsdchart/myucsdchart.dart';
import 'package:campus_mobile_experimental/ui/navigator/bottom.dart';
import 'package:campus_mobile_experimental/ui/navigator/top.dart';
import 'package:campus_mobile_experimental/ui/news/news_card.dart';
import 'package:campus_mobile_experimental/ui/notices/notices_card.dart';
Expand All @@ -34,29 +33,13 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni_links2/uni_links.dart';



class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
final _controller = ScrollController(
initialScrollOffset: getHomeScrollOffset(),
);

InternetConnectivityProvider? _connectivityProvider;

_HomeState() : super() {
_controller.addListener(
() {
setHomeScrollOffset(_controller.offset);
},
);
}


Future<Null> initUniLinks(BuildContext context) async {
// deep links are received by this method
// the specific host needs to be added in AndroidManifest.xml and Info.plist
Expand Down Expand Up @@ -103,16 +86,14 @@ class _HomeState extends State<Home> {
Widget build(BuildContext context) {
initUniLinks(context);
_connectivityProvider = Provider.of<InternetConnectivityProvider>(context);
final _children = createList(context);
return Padding(
padding: EdgeInsets.symmetric(horizontal: cardMargin, vertical: 0.0),
child: ListView(
controller: _controller,
padding: EdgeInsets.only(
top: cardMargin + 2.0, right: 0.0, bottom: 0.0, left: 0.0),
children: createList(context),
),
);
padding: EdgeInsets.symmetric(horizontal: cardMargin, vertical: 0.0),
child: ListView(
padding: EdgeInsets.only(
top: cardMargin + 2.0, right: 0.0, bottom: 0.0, left: 0.0),
children: createList(context),
),
);
}

List<Widget> createList(BuildContext context) {
Expand Down
37 changes: 1 addition & 36 deletions lib/ui/navigator/bottom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,6 @@ import 'package:campus_mobile_experimental/ui/profile/profile.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

//---saved scroll offsets for Home Screen---
var _homeScrollOffset = 0.0;

double getHomeScrollOffset() {
return _homeScrollOffset;
}

void setHomeScrollOffset(double currentScrollOffset) {
_homeScrollOffset = currentScrollOffset;
}

void resetHomeScrollOffset () {
_homeScrollOffset = 0.0;
}
//-------------------------------------------------

//---saved scroll offsets for Notification Screen---
var _notificationsScrollOffset = 0.0;

double getNotificationsScrollOffset() {
return _notificationsScrollOffset;
}

void setNotificationsScrollOffset(double currentScrollOffset) {
_notificationsScrollOffset = currentScrollOffset;
}

void resetNotificationsScrollOffset () {
_notificationsScrollOffset = 0.0;
}
//-------------------------------------------------

class BottomTabBar extends StatefulWidget {
@override
_BottomTabBarState createState() => _BottomTabBarState();
Expand All @@ -61,10 +29,7 @@ class _BottomTabBarState extends State<BottomTabBar> {
appBar: PreferredSize(
preferredSize: Size.fromHeight(42),
child: Provider.of<CustomAppBar>(context).appBar),
body:
PushNotificationWrapper(
child: currentTab[provider.currentIndex]
),
body: PushNotificationWrapper(child: currentTab[provider.currentIndex]),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: provider.currentIndex,
Expand Down
9 changes: 9 additions & 0 deletions lib/ui/notifications/notifications.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:campus_mobile_experimental/ui/notifications/notifications_list_view.dart';
import 'package:flutter/material.dart';

class NotificationsTabView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return NotificationsListView();
}
}
75 changes: 27 additions & 48 deletions lib/ui/notifications/notifications_list_view.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';

import 'package:campus_mobile_experimental/app_constants.dart';
import 'package:campus_mobile_experimental/core/models/notifications.dart';
import 'package:campus_mobile_experimental/core/providers/bottom_nav.dart';
Expand All @@ -12,72 +13,50 @@ import 'package:provider/provider.dart';
import 'package:uni_links2/uni_links.dart';
import 'package:url_launcher/url_launcher.dart';

import '../navigator/bottom.dart';

bool hideListView = false; // debug

class NotificationsListView extends StatefulWidget {
@override
State<NotificationsListView> createState() => _NotificationsListViewState();
}

class _NotificationsListViewState extends State<NotificationsListView> {

@override
initState() {
super.initState();
hideListView = true;
WidgetsBinding.instance
.addPostFrameCallback((_) {
notificationScrollController.jumpTo(getNotificationsScrollOffset());
setState(() {
hideListView = false;
});
});
}

class NotificationsListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
initUniLinks(context);
return Offstage(
offstage: hideListView,
child: RefreshIndicator(
child: buildListView(context),
onRefresh: () {
return Provider.of<MessagesDataProvider>(context, listen: false)
.fetchMessages(true);
},
color: Theme.of(context).colorScheme.secondary,
),
return RefreshIndicator(
child: buildListView(context),
onRefresh: () => Provider.of<MessagesDataProvider>(context, listen: false)
.fetchMessages(true),
color: Theme.of(context).colorScheme.secondary,
);
}

Widget buildListView(BuildContext context) {
Widget Function(BuildContext context, int index)? itemBuilder;
int itemCount = 0;
if (Provider.of<MessagesDataProvider>(context).messages!.length == 0) {
if (Provider.of<MessagesDataProvider>(context).error == null) {
if (Provider.of<MessagesDataProvider>(context).isLoading!) {
// empty notifications view until they load in
} else {
itemBuilder =
(BuildContext context, int index) => _buildNoMessagesText();
itemCount = 1;
return ListView.separated(
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) =>
_buildNoMessagesText(),
controller:
Provider.of<MessagesDataProvider>(context).scrollController,
itemCount: 1,
separatorBuilder: (BuildContext context, int index) => Divider(),
);
}
} else {
itemBuilder = (BuildContext context, int index) => _buildErrorText();
itemCount = 1;
return ListView.separated(
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) => _buildErrorText(),
controller:
Provider.of<MessagesDataProvider>(context).scrollController,
itemCount: 1,
separatorBuilder: (BuildContext context, int index) => Divider(),
);
}
}
if (itemCount == 0) {
itemBuilder = _buildMessage;
itemCount = Provider.of<MessagesDataProvider>(context).messages!.length;
}
return ListView.separated(
physics: AlwaysScrollableScrollPhysics(),
itemBuilder: itemBuilder!,
controller: notificationScrollController,
itemCount: itemCount,
itemBuilder: _buildMessage,
controller: Provider.of<MessagesDataProvider>(context).scrollController,
itemCount: Provider.of<MessagesDataProvider>(context).messages!.length,
separatorBuilder: (BuildContext context, int index) => Divider(),
);
}
Expand Down
Loading

0 comments on commit 67dd6bb

Please sign in to comment.