Skip to content

Commit

Permalink
Replica Fixes/ Fix App caching lot of data (#1042)
Browse files Browse the repository at this point in the history
* Added ability to user go back to replica home.

* Fixed issue with focus node.

* Started working on caching implemenation.

* Clean up cache if goes above limit also create singleton.

* Dispose controller.

* Remove unnecessary comments.

* add to controller if widget is mounted.
  • Loading branch information
jigar-f authored Apr 9, 2024
1 parent ae42dde commit 521fc16
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 125 deletions.
72 changes: 42 additions & 30 deletions lib/common/ui/base_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class BaseScreen extends StatelessWidget {
final bool automaticallyImplyLeading;
final List<Widget>? persistentFooterButtons;

final VoidCallback? onBackButtonPressed;

BaseScreen({
this.title,
this.actions,
Expand All @@ -34,6 +36,7 @@ class BaseScreen extends StatelessWidget {
this.showAppBar = true,
this.automaticallyImplyLeading = true,
this.persistentFooterButtons,
this.onBackButtonPressed,
Key? key,
}) : super(key: key) {
this.foregroundColor = foregroundColor ?? black;
Expand Down Expand Up @@ -67,45 +70,54 @@ class BaseScreen extends StatelessWidget {
appBar: !showAppBar
? null
: PreferredSize(
preferredSize: Size.fromHeight(appBarHeight+verticalCorrection),
preferredSize:
Size.fromHeight(appBarHeight + verticalCorrection),
child: SafeArea(
child: Column(
children: [
ConnectivityWarning(
dy: verticalCorrection,
),
AppBar(
automaticallyImplyLeading: automaticallyImplyLeading,
leading: automaticallyImplyLeading ? IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.of(context).pop(),
) : null,
title: title is String
? CText(
title,
style: tsHeading3
.copiedWith(color: foregroundColor)
.short,
)
: title,
elevation: 1,
shadowColor: grey3,
foregroundColor: foregroundColor,
backgroundColor: backgroundColor,
iconTheme: IconThemeData(color: foregroundColor),
centerTitle: centerTitle,
titleSpacing: 0,
actions: actions,
),
],
children: [
ConnectivityWarning(
dy: verticalCorrection,
),
AppBar(
automaticallyImplyLeading: automaticallyImplyLeading,
leading: automaticallyImplyLeading
? IconButton(
icon: const Icon(Icons.arrow_back,
color: Colors.black),
onPressed: () {
if (onBackButtonPressed != null) {
onBackButtonPressed?.call();
return;
}
Navigator.of(context).pop();
})
: null,
title: title is String
? CText(
title,
style: tsHeading3
.copiedWith(color: foregroundColor)
.short,
)
: title,
elevation: 1,
shadowColor: grey3,
foregroundColor: foregroundColor,
backgroundColor: backgroundColor,
iconTheme: IconThemeData(color: foregroundColor),
centerTitle: centerTitle,
titleSpacing: 0,
actions: actions,
),
],
),
),
),
body: Padding(
padding: EdgeInsetsDirectional.only(
start: padHorizontal ? 16 : 0,
end: padHorizontal ? 16 : 0,
top: padVertical ? 16:0,
top: padVertical ? 16 : 0,
bottom: padVertical ? 16 : 0,
),
child: body,
Expand Down
2 changes: 1 addition & 1 deletion lib/common/ui/custom/text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class _CTextFieldState extends State<CTextField> {

@override
void dispose() {
widget.controller.focusNode.dispose();
widget.controller.dispose();
super.dispose();
}

Expand Down
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:ui' as ui;

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:lantern/app.dart';
import 'package:lantern/common/common.dart';
import 'package:lantern/common/common_desktop.dart';
import 'package:lantern/replica/ui/utils.dart';
import 'package:window_manager/window_manager.dart';

import 'catcher_setup.dart';
Expand Down Expand Up @@ -48,6 +50,9 @@ Future<void> main() async {
});
} else {
await _initGoogleMobileAds();
// Due to replica we are using lot of cache
// clear if goes to above limit
CustomCacheManager().clearCacheIfExceeded();
}

await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
Expand Down
125 changes: 74 additions & 51 deletions lib/replica/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import 'package:lantern/replica/search.dart';
/// ReplicaHomeScreen is the entrypoint for the user to search through Replica.
/// See docs/replica_home.png for a preview
class ReplicaHomeScreen extends StatefulWidget {
const ReplicaHomeScreen({super.key});

@override
State<StatefulWidget> createState() => _ReplicaHomeScreenState();
}

class _ReplicaHomeScreenState extends State<ReplicaHomeScreen> {
final _formKey = GlobalKey<FormState>(debugLabel: 'replicaSearchInput');
late final _textEditingController =
CustomTextEditingController(formKey: _formKey);
CustomTextEditingController? _textEditingController;

late bool showResults = false;
late String currentQuery = '';
late int currentTab = 0;
Expand All @@ -29,7 +31,7 @@ class _ReplicaHomeScreenState extends State<ReplicaHomeScreen> {
super.initState();
replicaModel.getSearchTerm().then((String cachedSearchTerm) {
if (cachedSearchTerm.isNotEmpty) {
_textEditingController.initialValue = cachedSearchTerm;
_textEditingController?.initialValue = cachedSearchTerm;
setState(() {
currentQuery = cachedSearchTerm;
showResults = true;
Expand All @@ -46,23 +48,48 @@ class _ReplicaHomeScreenState extends State<ReplicaHomeScreen> {
doGetCachedTab();
}

@override
void dispose() {
_textEditingController?.dispose();
_formKey.currentState?.dispose();
super.dispose();
}

void doGetCachedTab() async {
final cachedTab = await replicaModel.getSearchTab();
setSearchTab(int.parse(cachedTab));
}

@override
Widget build(BuildContext context) {
// We are showing the ReplicaSearchScreen here since we want the bottom tabs to be visible (they are not if it's its own route)
// <08-23-22, kalli> Not ideal UX - maybe add a spinner? Not sure
// <09-07-22, kalli> Update after testing a debug build - this is not very noticeable.
if (showResults) {
return ReplicaSearchScreen(
currentQuery: currentQuery,
currentTab: currentTab,
onBackButtonPressed: onBackButtonPressed,
);
}
// we need to initialize controller again coz we when switch widget controller is getting disposed
_textEditingController = CustomTextEditingController(formKey: _formKey);
return _buildSearchView();
}

void Function() renderNewDialog(BuildContext context, setSearchTab) {
return CDialog(
iconPath: ImagePaths.newspaper,
title: 'replica_new_discover'.i18n,
description: 'replica_new_discover_news'.i18n,
agreeText: 'replica_check_it_out'.i18n,
agreeAction: () async {
await replicaModel.setShowNewBadge(false);
await setSearchTab(5); // News is index 5
return true;
},
includeCancel: false,
).show(context);
}

Widget _buildSearchView() {
// No active query, return the landing search bar instead
return GestureDetector(
onTap: () {
Expand All @@ -73,40 +100,42 @@ class _ReplicaHomeScreenState extends State<ReplicaHomeScreen> {
actionButton: renderFap(context),
centerTitle: true,
title: 'discover'.i18n,
automaticallyImplyLeading: false,
body: Stack(
children: [
SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsetsDirectional.only(bottom: 46, top: 30),
child: CAssetImage(
path: ImagePaths.lantern_logo,
size: 72,
),
SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsetsDirectional.only(bottom: 46, top: 30),
child: CAssetImage(
path: ImagePaths.lantern_logo,
size: 72,
),
Padding(
padding: const EdgeInsetsDirectional.only(start: 10.0, end: 10.0),
child: SearchField(
controller: _textEditingController,
search: (query) async {
await replicaModel.setSearchTerm(query);
if (query != '') {
setState(() {
currentQuery = query;
showResults = true;
});
}
},
onClear: () async {
await replicaModel.setSearchTerm('');
await replicaModel.setSearchTab(0);
},
),
),
Padding(
padding: const EdgeInsetsDirectional.only(
start: 10.0, end: 10.0),
child: SearchField(
controller: _textEditingController!,
search: (query) async {
await replicaModel.setSearchTerm(query);
if (query != '') {
setState(() {
currentQuery = query;
showResults = true;
});
}
},
onClear: () async {
await replicaModel.setSearchTerm('');
await replicaModel.setSearchTab(0);
},
),
renderDiscoverText(),
),
renderDiscoverText(),
],
),
),
Expand All @@ -117,21 +146,6 @@ class _ReplicaHomeScreenState extends State<ReplicaHomeScreen> {
);
}

void Function() renderNewDialog(BuildContext context, setSearchTab) {
return CDialog(
iconPath: ImagePaths.newspaper,
title: 'replica_new_discover'.i18n,
description: 'replica_new_discover_news'.i18n,
agreeText: 'replica_check_it_out'.i18n,
agreeAction: () async {
await replicaModel.setShowNewBadge(false);
await setSearchTab(5); // News is index 5
return true;
},
includeCancel: false,
).show(context);
}

Widget renderDiscoverText() {
return Padding(
padding: const EdgeInsetsDirectional.all(12.0),
Expand All @@ -158,4 +172,13 @@ class _ReplicaHomeScreenState extends State<ReplicaHomeScreen> {
},
);
}

//class methods
Future<void> onBackButtonPressed() async {
setState(() {
showResults = false;
});
await replicaModel.setSearchTerm('');
await replicaModel.setSearchTab(0);
}
}
Loading

0 comments on commit 521fc16

Please sign in to comment.