Skip to content

Commit

Permalink
rfac: refactor the implementation logic
Browse files Browse the repository at this point in the history
Signed-off-by: Aman <[email protected]>
  • Loading branch information
aman-singh7 committed Jun 5, 2022
1 parent e309aa3 commit 4d723e9
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 204 deletions.
8 changes: 8 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ linter:
non_constant_identifier_names: false

constant_identifier_names: false

no_leading_underscores_for_local_identifiers: false

library_private_types_in_public_api: false

depend_on_referenced_packages: false

use_build_context_synchronously: false
2 changes: 1 addition & 1 deletion lib/models/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class UserAttributes {
this.educationalInstitute,
});
String? name;
String email;
String? email;
bool subscribed;
DateTime? createdAt;
DateTime? updatedAt;
Expand Down
31 changes: 17 additions & 14 deletions lib/ui/views/ib/builders/ib_webview_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ class IbWebViewBuilder extends MarkdownElementBuilder {

return Html(
data: textContent,
customRender: {
'iframe': (RenderContext context, Widget child) {
final width = MediaQuery.of(context.buildContext).size.width;
final height = (width * 9) / 16;
customRenders: {
tagMatcher('iframe'): CustomRender.widget(
widget: (context, child) {
final width = MediaQuery.of(context.buildContext).size.width;
final height = (width * 9) / 16;

return SizedBox(
width: width,
height: height,
child: WebView(
initialUrl: context.tree.element?.attributes['src'],
javascriptMode: JavascriptMode.unrestricted,
initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy.always_allow,
),
);
},
return SizedBox(
width: width,
height: height,
child: WebView(
initialUrl: context.tree.element?.attributes['src'],
javascriptMode: JavascriptMode.unrestricted,
initialMediaPlaybackPolicy:
AutoMediaPlaybackPolicy.always_allow,
),
);
},
),
},
);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/ui/views/ib/ib_landing_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ class _IbLandingViewState extends State<IbLandingView> {
? Showcase(
key: _model.toc,
description: 'Show Table of Contents',
child: IconButton(
icon: const Icon(Icons.menu_book_rounded),
onPressed: value as VoidCallback,
),
onTargetClick: () {
_model.onShowCased('toc');
if (_key.currentState!.isDrawerOpen) Get.back();
Future.delayed(
const Duration(milliseconds: 200),
value,
value as VoidCallback,
);
},
disposeOnTap: true,
child: IconButton(
icon: const Icon(Icons.menu_book_rounded),
onPressed: value as VoidCallback,
),
)
: Container();
},
Expand Down
60 changes: 11 additions & 49 deletions lib/ui/views/profile/profile_view.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mobile_app/cv_theme.dart';
import 'package:mobile_app/locator.dart';
import 'package:mobile_app/models/user.dart';
import 'package:mobile_app/services/local_storage_service.dart';
import 'package:mobile_app/ui/components/cv_tab_bar.dart';
import 'package:mobile_app/ui/views/base_view.dart';
import 'package:mobile_app/ui/views/profile/user_favourites_view.dart';
Expand All @@ -26,14 +24,6 @@ class ProfileView extends StatefulWidget {

class _ProfileViewState extends State<ProfileView> {
late ProfileViewModel _model;
String? userId;

@override
void initState() {
super.initState();
userId =
widget.userId ?? locator<LocalStorageService>().currentUser?.data.id;
}

Widget _buildProfileImage() {
return Padding(
Expand Down Expand Up @@ -84,9 +74,7 @@ class _ProfileViewState extends State<ProfileView> {
}

Widget _buildEditProfileButton() {
var _localStorageService = locator<LocalStorageService>();
if (_localStorageService.isLoggedIn &&
userId == _localStorageService.currentUser!.data.id) {
if (_model.isLoggedIn && _model.isPersonalProfile) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
primary: CVTheme.primaryColor,
Expand All @@ -113,37 +101,6 @@ class _ProfileViewState extends State<ProfileView> {
return Container();
}

Widget _buildSubscribeToMailComponent() {
var _attrs = _model.user?.data.attributes;

var _localStorageService = locator<LocalStorageService>();
if (_localStorageService.isLoggedIn &&
userId == _localStorageService.currentUser!.data.id) {
return Container(
padding: const EdgeInsets.symmetric(vertical: 4),
alignment: Alignment.centerLeft,
child: RichText(
text: TextSpan(
style: Theme.of(context).textTheme.bodyText1,
children: <TextSpan>[
TextSpan(
text: 'Subscribed to mails : ',
style: Theme.of(context).textTheme.bodyText1?.copyWith(
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: _attrs?.subscribed.toString(),
)
],
),
),
);
}

return Container();
}

Widget _buildProfileCard() {
var _attrs = _model.user?.data.attributes;

Expand Down Expand Up @@ -184,7 +141,11 @@ class _ProfileViewState extends State<ProfileView> {
'Educational Institute',
_attrs?.educationalInstitute,
),
_buildSubscribeToMailComponent(),
if (_model.isLoggedIn && _model.isPersonalProfile)
_buildProfileComponent(
'Subscribed to mails',
_attrs?.subscribed.toString(),
),
_buildEditProfileButton()
],
),
Expand All @@ -196,7 +157,7 @@ class _ProfileViewState extends State<ProfileView> {
}

Widget _buildProjectsTabBar() {
if (userId == null) return Container();
if (_model.userId == null) return Container();
return Expanded(
child: Card(
shape: RoundedRectangleBorder(
Expand Down Expand Up @@ -224,8 +185,8 @@ class _ProfileViewState extends State<ProfileView> {
),
body: TabBarView(
children: [
UserProjectsView(userId: userId!),
UserFavouritesView(userId: userId!),
UserProjectsView(userId: _model.userId!),
UserFavouritesView(userId: _model.userId!),
],
),
),
Expand All @@ -239,7 +200,8 @@ class _ProfileViewState extends State<ProfileView> {
return BaseView<ProfileViewModel>(
onModelReady: (model) {
_model = model;
_model.fetchUserProfile(userId);
_model.userId = widget.userId;
_model.fetchUserProfile();
},
builder: (context, model, child) => Scaffold(
appBar: widget.userId != null
Expand Down
2 changes: 1 addition & 1 deletion lib/viewmodels/ib/ib_page_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class IbPageViewModel extends BaseModel {
if (!state.tocButton) _list.add(keysMap['toc']);

if (_list.isNotEmpty) {
WidgetsBinding.instance!.addPostFrameCallback((_) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Future.delayed(const Duration(milliseconds: 800), () {
showCaseWidgetState.startShowCase(_list);
});
Expand Down
22 changes: 19 additions & 3 deletions lib/viewmodels/profile/profile_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ import 'package:mobile_app/models/failure_model.dart';
import 'package:mobile_app/models/projects.dart';
import 'package:mobile_app/models/user.dart';
import 'package:mobile_app/services/API/users_api.dart';
import 'package:mobile_app/services/local_storage_service.dart';
import 'package:mobile_app/viewmodels/base_viewmodel.dart';

class ProfileViewModel extends BaseModel {
// ViewState Keys
String FETCH_USER_PROFILE = 'fetch_user_profile';

final UsersApi _usersApi = locator<UsersApi>();
final LocalStorageService _localStorageService =
locator<LocalStorageService>();

String? _userId;

String? get userId => _userId;

set userId(String? id) {
_userId = id ?? _localStorageService.currentUser?.data.id;
}

User? _user;

Expand All @@ -21,6 +32,11 @@ class ProfileViewModel extends BaseModel {
notifyListeners();
}

bool get isLoggedIn => _localStorageService.isLoggedIn;

bool get isPersonalProfile =>
_localStorageService.currentUser?.data.id == _userId;

Project? _updatedProject;

Project? get updatedProject => _updatedProject;
Expand All @@ -30,11 +46,11 @@ class ProfileViewModel extends BaseModel {
notifyListeners();
}

Future? fetchUserProfile(String? userId) async {
if (userId == null) return;
Future? fetchUserProfile() async {
if (_userId == null) return;
setStateFor(FETCH_USER_PROFILE, ViewState.Busy);
try {
user = await _usersApi.fetchUser(userId);
user = await _usersApi.fetchUser(_userId!);

setStateFor(FETCH_USER_PROFILE, ViewState.Success);
} on Failure catch (f) {
Expand Down
Loading

0 comments on commit 4d723e9

Please sign in to comment.