Skip to content

Commit

Permalink
enable support for incognito keyboard
Browse files Browse the repository at this point in the history
- issue: #22

Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed Sep 29, 2022
1 parent 310278b commit 8f25602
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 41 deletions.
20 changes: 12 additions & 8 deletions lib/data/preference_and_config.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Package imports:
import 'package:shared_preferences/shared_preferences.dart';

class AppSecurePreferencesStorage {
class PreferencesStorage {
static SharedPreferences? _preferences;

static const _keyPassPhraseHash = 'passphrasehash';
static const _keyAllowUndecryptLoginFlag = 'undecryptLoginFlag';
static const _keyIsThemeDark = 'isthemedark';
static const _keyKeyboardIncognito = 'keyboardIcognito';

static Future init() async =>
_preferences = await SharedPreferences.getInstance();
Expand All @@ -19,18 +20,21 @@ class AppSecurePreferencesStorage {
static Future setAllowUndecryptLoginFlag(bool flag) async =>
await _preferences?.setBool(_keyAllowUndecryptLoginFlag, flag);
static bool getAllowUndecryptLoginFlag() {
bool? localFlag = _preferences?.getBool(_keyAllowUndecryptLoginFlag);
localFlag ??= true;
return localFlag;
return _preferences?.getBool(_keyAllowUndecryptLoginFlag) ?? true;
}

static Future setIsThemeDark(bool flag) async =>
await _preferences?.setBool(_keyIsThemeDark, flag);

static bool getIsThemeDark() {
bool? localFlag = _preferences?.getBool(_keyIsThemeDark);
localFlag ??= true;
return localFlag;
return _preferences?.getBool(_keyIsThemeDark) ?? true;
}

static Future<void> setKeyboardIncognito(bool flag) async =>
await _preferences?.setBool(_keyKeyboardIncognito, flag);

static bool getKeyboardIncognito() {
return _preferences?.getBool(_keyKeyboardIncognito) ?? true;
}
}

Expand All @@ -44,7 +48,7 @@ class PhraseHandler {

class UnDecryptedLoginControl {
static getAllowLogUnDecrypted() =>
AppSecurePreferencesStorage.getAllowUndecryptLoginFlag();
PreferencesStorage.getAllowUndecryptLoginFlag();

static bool noDecryptionFlag = false;
static setNoDecryptionFlag(bool flag) => noDecryptionFlag = flag;
Expand Down
4 changes: 2 additions & 2 deletions lib/dialogs/change_passphrase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class _ChangePassphraseDialogState extends State<ChangePassphraseDialog> {
},
validator: (passphrase) {
return sha256.convert(utf8.encode(passphrase!)).toString() !=
AppSecurePreferencesStorage.getPassPhraseHash()
PreferencesStorage.getPassPhraseHash()
? validationErrorMsg
: null;
},
Expand Down Expand Up @@ -272,7 +272,7 @@ class _ChangePassphraseDialogState extends State<ChangePassphraseDialog> {

// Update SHA256 signature of passphrase
if (form.validate()) {
AppSecurePreferencesStorage.setPassPhraseHash(
PreferencesStorage.setPassPhraseHash(
sha256
.convert(utf8.encode(_newConfirmPassphraseController.text))
.toString(),
Expand Down
11 changes: 5 additions & 6 deletions lib/dialogs/toggle_undecrypt_flag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ToggleUndecryptionFlag extends StatefulWidget {

class _ToggleUndecryptionFlagState extends State<ToggleUndecryptionFlag> {
List<bool> _isSelected = [
AppSecurePreferencesStorage.getAllowUndecryptLoginFlag(),
!AppSecurePreferencesStorage.getAllowUndecryptLoginFlag()
PreferencesStorage.getAllowUndecryptLoginFlag(),
!PreferencesStorage.getAllowUndecryptLoginFlag()
];

@override
Expand Down Expand Up @@ -121,10 +121,10 @@ Once ON, You'll need to choose the second option on the login screen to see your
//color: Colors.black,
color: Colors.grey,
fillColor: Colors.lightBlue.shade900,
borderColor: AppSecurePreferencesStorage.getIsThemeDark()
borderColor: PreferencesStorage.getIsThemeDark()
? NordColors.snowStorm.darkest
: Colors.grey.withOpacity(0.8),
selectedBorderColor: AppSecurePreferencesStorage.getIsThemeDark()
selectedBorderColor: PreferencesStorage.getIsThemeDark()
? NordColors.snowStorm.darkest
: Colors.grey.withOpacity(0.8),
borderRadius: BorderRadius.circular(5),
Expand Down Expand Up @@ -163,8 +163,7 @@ Once ON, You'll need to choose the second option on the login screen to see your
this._isSelected[0] = false;
this._isSelected[1] = true;
}
AppSecurePreferencesStorage.setAllowUndecryptLoginFlag(
this._isSelected[0]);
PreferencesStorage.setAllowUndecryptLoginFlag(this._isSelected[0]);
});
}
}
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Future main() async {
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
await AppSecurePreferencesStorage.init();
await PreferencesStorage.init();
runApp(SafeNotesApp());
}

Expand All @@ -36,7 +36,7 @@ class SafeNotesApp extends StatelessWidget {
theme: AppThemes.lightTheme,
darkTheme: AppThemes.darkTheme,

home: AppSecurePreferencesStorage.getPassPhraseHash() != null
home: PreferencesStorage.getPassPhraseHash() != null
? EncryptionPhraseLoginPage()
: SetEncryptionPhrasePage(),
);
Expand Down
7 changes: 3 additions & 4 deletions lib/models/app_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import 'package:flutter_nord_theme/flutter_nord_theme.dart';
import 'package:safenotes/data/preference_and_config.dart';

class ThemeProvider extends ChangeNotifier {
ThemeMode themeMode = AppSecurePreferencesStorage.getIsThemeDark()
? ThemeMode.dark
: ThemeMode.light;
ThemeMode themeMode =
PreferencesStorage.getIsThemeDark() ? ThemeMode.dark : ThemeMode.light;

bool get isDarkMode => themeMode == ThemeMode.dark;
void toggleTheme(bool isOn) {
themeMode = isOn ? ThemeMode.dark : ThemeMode.light;
AppSecurePreferencesStorage.setIsThemeDark(isOn);
PreferencesStorage.setIsThemeDark(isOn);
notifyListeners();
}
}
Expand Down
5 changes: 2 additions & 3 deletions lib/models/file_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class FileHandler {
String fileName = AppInfo.getExportFileName();
final bool isExportEncrypted =
ExportEncryptionControl.getIsExportEncrypted();
final String passHash =
AppSecurePreferencesStorage.getPassPhraseHash().toString();
final String passHash = PreferencesStorage.getPassPhraseHash().toString();

String preFixToRecord = '{ "records" : ';
String postFixToRecord = ', "recordHandlerHash" : ' +
Expand Down Expand Up @@ -77,7 +76,7 @@ class FileHandler {

Future<String?> selectFileAndDoImport(BuildContext context) async {
String? dataFromFileAsString = await getFileAsString();
String? currentPassHash = AppSecurePreferencesStorage.getPassPhraseHash();
String? currentPassHash = PreferencesStorage.getPassPhraseHash();

if (dataFromFileAsString == null) {
return "File not picked!";
Expand Down
18 changes: 15 additions & 3 deletions lib/views/add_edit_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import 'package:flutter_keyboard_size/flutter_keyboard_size.dart';

// Project imports:
import 'package:safenotes/data/database_handler.dart';
import 'package:safenotes/data/preference_and_config.dart';
import 'package:safenotes/models/safenote.dart';
import 'package:safenotes/widgets/note_widget.dart';

import 'package:flutter_nord_theme/flutter_nord_theme.dart';

class AddEditNotePage extends StatefulWidget {
final SafeNote? note;

Expand Down Expand Up @@ -63,16 +66,25 @@ class _AddEditNotePageState extends State<AddEditNotePage> {

Widget buildButton() {
final isFormValid = title.isNotEmpty && description.isNotEmpty;
final double buttonFontSize = 17.0;
final String buttonText = 'Save';

return Padding(
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
//onPrimary: Colors.white,
backgroundColor: isFormValid ? null : Colors.grey.shade700,
backgroundColor: isFormValid
? (PreferencesStorage.getIsThemeDark()
? null
: NordColors.polarNight.darkest)
: Colors.grey.shade700,
),
onPressed: addOrUpdateNote,
child: Text('Save'),
child: Text(
buttonText,
style:
TextStyle(fontSize: buttonFontSize, fontWeight: FontWeight.bold),
),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/views/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class _NotesPageState extends State<NotesPage> {
}

Widget _divide({required double topPadding}) {
final bool isDarkTheme = AppSecurePreferencesStorage.getIsThemeDark();
final bool isDarkTheme = PreferencesStorage.getIsThemeDark();

return Padding(
padding: EdgeInsets.only(top: topPadding),
Expand Down
4 changes: 2 additions & 2 deletions lib/views/login_views/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class _EncryptionPhraseLoginPageState extends State<EncryptionPhraseLoginPage> {
String? _passphraseValidator(String? passphrase) {
final wrongPhraseMsg = 'Wrong encryption Phrase!';
return sha256.convert(utf8.encode(passphrase!)).toString() !=
AppSecurePreferencesStorage.getPassPhraseHash()
PreferencesStorage.getPassPhraseHash()
? wrongPhraseMsg
: null;
}
Expand Down Expand Up @@ -147,7 +147,7 @@ class _EncryptionPhraseLoginPageState extends State<EncryptionPhraseLoginPage> {
if (form.validate()) {
final phrase = passPhraseController.text;
if (sha256.convert(utf8.encode(phrase)).toString() ==
AppSecurePreferencesStorage.getPassPhraseHash()) {
PreferencesStorage.getPassPhraseHash()) {
_snackBarMessage(context, 'Decrypting your notes!');

PhraseHandler.initPass(phrase);
Expand Down
2 changes: 1 addition & 1 deletion lib/views/login_views/set_passphrase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class _SetEncryptionPhrasePageState extends State<SetEncryptionPhrasePage> {
_snackBarMessage(context, 'Encryption Phrase Set!');

// Setting hash for PassPhrase in share prefrences
AppSecurePreferencesStorage.setPassPhraseHash(
PreferencesStorage.setPassPhraseHash(
sha256.convert(utf8.encode(enteredPassphrase)).toString());
PhraseHandler.initPass(enteredPassphrase);
UnDecryptedLoginControl.setNoDecryptionFlag(false);
Expand Down
48 changes: 39 additions & 9 deletions lib/widgets/note_widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Flutter imports:
import 'package:flutter/material.dart';

// Project imports:
import 'package:safenotes/data/preference_and_config.dart';

class NoteFormWidget extends StatelessWidget {
final String? title;
final String? description;
Expand Down Expand Up @@ -40,8 +43,11 @@ class NoteFormWidget extends StatelessWidget {
final double fontSize = 24.0;
final int maxLinesToShowAtTimeTitle = 2;
final String titleHint = 'Title';
//Disable IMEPL if keyboard incognito mode is true
final bool enableIMEPLFlag = !PreferencesStorage.getKeyboardIncognito();

return TextFormField(
enableIMEPersonalizedLearning: enableIMEPLFlag,
maxLines: maxLinesToShowAtTimeTitle,
initialValue: this.title,
enableInteractiveSelection: true,
Expand Down Expand Up @@ -71,20 +77,15 @@ class NoteFormWidget extends StatelessWidget {
}

Widget buildDescription(BuildContext context) {
final double height = MediaQuery.of(context).size.height;
final EdgeInsets padding = MediaQuery.of(context).padding;
final double keyboard = MediaQuery.of(context).viewInsets.bottom;
final double topFixed = 200.0;
final double effectiveHeighOfDevice =
height - padding.top - padding.bottom - topFixed - keyboard;

final double adaptiveScreenFactorForDescription = 25.0;
// maxLine is used in resizing description field on keyboard activation or dismissal
final int maxLinesToShowAtTimeDescription =
(effectiveHeighOfDevice / adaptiveScreenFactorForDescription).round();
computeMaxLine(context: context, fontHeight: 30.0);
final double fontSize = 18.0;
final String hintDescription = 'Type something...';
final bool enableIMEPLFlag = !PreferencesStorage.getKeyboardIncognito();

return TextFormField(
enableIMEPersonalizedLearning: enableIMEPLFlag,
maxLines: maxLinesToShowAtTimeDescription,
initialValue: this.description,
enableInteractiveSelection: true,
Expand All @@ -111,4 +112,33 @@ class NoteFormWidget extends StatelessWidget {
? descriptionCantBeEmptyMsg
: null;
}

int computeMaxLine(
{required BuildContext context, required double fontHeight}) {
final double totalHeight = MediaQuery.of(context).size.height;
final EdgeInsets paddingInsets = MediaQuery.of(context).padding;
final double keyboard = MediaQuery.of(context).viewInsets.bottom;
final double padding = paddingInsets.top + paddingInsets.bottom;

double totalHeightRatio = (totalHeight - padding) / 100;
double fontHeightRatio = fontHeight / 100;
double theXfactor = totalHeightRatio / 3.2;
/*
When Keyboard is on screen:-
Theoretical Ratios for top:description:keyboard
theoreticalTitleNTopHeightRatio = x;
theoreticalDescriptinHeightRatio = x*1.2;
theoreticalKeyboardHeightRatio = x;
x + x*1.2 + x = totalHeightRatio (i.e total height of screen)
From above:
if keyboard is on-screen:
theoreticalDescriptinHeightRatio = x*1.2
if keyboard not on screen:
theoreticalDescriptinHeightRatio = x*2.2 (keyboard space is taken by description)
*/
double descriptionRatio = theXfactor * 2.2;
if (keyboard > 1) descriptionRatio = theXfactor * 1.2;
return (descriptionRatio / fontHeightRatio).round();
}
}
5 changes: 5 additions & 0 deletions lib/widgets/search_widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Flutter imports:
import 'package:flutter/material.dart';

// Project imports:
import 'package:safenotes/data/preference_and_config.dart';

class SearchWidget extends StatefulWidget {
final String text;
final ValueChanged<String> onChanged;
Expand All @@ -26,6 +29,7 @@ class _SearchWidgetState extends State<SearchWidget> {
final styleHint = TextStyle(color: Colors.black54);
final style = widget.text.isEmpty ? styleHint : styleActive;
final searchBoxRadius = 7.0;
final bool enableIMEPLFlag = !PreferencesStorage.getKeyboardIncognito();

return Container(
height: 42,
Expand All @@ -37,6 +41,7 @@ class _SearchWidgetState extends State<SearchWidget> {
),
padding: const EdgeInsets.symmetric(horizontal: 8),
child: TextField(
enableIMEPersonalizedLearning: enableIMEPLFlag,
controller: controller,
enableInteractiveSelection: true,
//autofocus: true,
Expand Down

0 comments on commit 8f25602

Please sign in to comment.