Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import "package:flutter/material.dart";
import "package:permission_handler/permission_handler.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:vidar/configuration.dart";
import "package:vidar/pages/contact_list.dart";
import "package:vidar/pages/no_sms_permission.dart";
import "package:vidar/utils/common_object.dart";
import "package:vidar/utils/settings.dart";
import "package:vidar/utils/sms.dart";
import "package:vidar/utils/storage.dart";

Expand All @@ -13,10 +15,28 @@ void main() async {
WidgetsFlutterBinding.ensureInitialized();

externalConfiguration();
await loadData(CommonObject.contactList);

await loadData(CommonObject.contactList, CommonObject.settings);
// If last logon is past the wipeout time -> wipe keys
if (Settings.allowWipeoutTime &&
CommonObject.lastLogon != null &&
DateTime.now().difference(CommonObject.lastLogon!).inDays >
Settings.wipeoutTime) {
if (Settings.keepLogs) {
CommonObject.logger!.info(
"Last logon was more than ${Settings.wipeoutTime} days, wiping all keys...",
);
}
CommonObject.contactList.wipeKeys();
wipeSecureStorage();
}
SmsConstants(await retrieveSmsConstantsMap());
smsStatus = await Permission.sms.request();
CommonObject.lastLogon = DateTime.now();
await (await SharedPreferences.getInstance()).setString(
"lastlogon",
CommonObject.lastLogon!.toIso8601String(),
);

runApp(const VidarApp());
}
Expand Down
6 changes: 3 additions & 3 deletions lib/pages/edit_contact.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class _EditContactPageState extends State<EditContactPage> {
contact.name = newName!;
contact.encryptionKey = newKey!;
contact.phoneNumber = newPhoneNumber!;
saveData(CommonObject.contactList, CommonObject.settings);
saveData(CommonObject.contactList);
clearNavigatorAndPush(context, ChatPage(contact));
}

Expand Down Expand Up @@ -159,7 +159,7 @@ class _EditContactPageState extends State<EditContactPage> {
contact.name = newName!;
contact.encryptionKey = newKey!;
contact.phoneNumber = newPhoneNumber!;
saveData(CommonObject.contactList, CommonObject.settings);
saveData(CommonObject.contactList);
CommonObject.logger!.info(
"New contact ${contact.uuid} has been saved",
);
Expand All @@ -186,7 +186,7 @@ class _EditContactPageState extends State<EditContactPage> {
contact.name = newName!;
contact.encryptionKey = newKey!;
contact.phoneNumber = newPhoneNumber!;
saveData(CommonObject.contactList, CommonObject.settings);
saveData(CommonObject.contactList);
clearNavigatorAndPush(context, const ContactListPage());
}
}
Expand Down
45 changes: 43 additions & 2 deletions lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "package:vidar/utils/storage.dart";
import "package:vidar/widgets/boolean_setting.dart";
import "package:vidar/widgets/buttons.dart";
import "package:vidar/widgets/color_set_select.dart";
import "package:vidar/widgets/int_setting.dart";

class SettingsPage extends StatefulWidget {
const SettingsPage({super.key});
Expand All @@ -19,7 +20,21 @@ class SettingsPage extends StatefulWidget {
}

class _SettingsPageState extends State<SettingsPage> {
_SettingsPageState();
_SettingsPageState() {
allowWipeoutTime = LightBooleanSetting(
initValue: allowWipeoutTimeValue,
settingText: "Require login every X days",
onChanged: (final bool value) {
setState(() {
allowWipeoutTimeValue = !allowWipeoutTimeValue;
});
},
);
}

late final LightBooleanSetting allowWipeoutTime;

bool allowWipeoutTimeValue = Settings.allowWipeoutTime;

final BooleanSetting allowUnencryptedMessages = BooleanSetting(
setting: Settings.allowUnencryptedMessages,
Expand All @@ -45,6 +60,12 @@ class _SettingsPageState extends State<SettingsPage> {
selectedSet: Settings.colorSet.name,
);

final IntSetting wipeoutTime = IntSetting(
setting: Settings.wipeoutTime,
settingText: "Max logon interval (days)",
maxLength: 4,
);

@override
void initState() {
super.initState();
Expand All @@ -55,6 +76,17 @@ class _SettingsPageState extends State<SettingsPage> {
Settings.keepLogs = keepLogs.setting;
Settings.showEncryptionKeyInEditContact =
showEncryptionKeyInEditContact.setting;
Settings.allowUnencryptedMessages = allowUnencryptedMessages.setting;
Settings.allowWipeoutTime = allowWipeoutTimeValue;
if (allowWipeoutTimeValue) {
if (wipeoutTime.setting < 1) {
Settings.allowWipeoutTime = false;
Settings.wipeoutTime = 0;
} else {
Settings.allowWipeoutTime = true;
Settings.wipeoutTime = wipeoutTime.setting;
}
}
if (Settings.keepLogs) {
final PermissionStatus manageExternalStorageStatus = await Permission
.manageExternalStorage
Expand Down Expand Up @@ -94,7 +126,7 @@ class _SettingsPageState extends State<SettingsPage> {
Settings.colorSet = getColorSetFromName(colorSetSelect.selectedSet);

if (mounted) {
saveSettings(CommonObject.settings, context: context);
saveSettings(context: context);
clearNavigatorAndPush(context, const ContactListPage());
}
}
Expand All @@ -117,6 +149,15 @@ class _SettingsPageState extends State<SettingsPage> {
allowUnencryptedMessages,
keepLogs,
showEncryptionKeyInEditContact,
Column(
children: <Widget>[
allowWipeoutTime,
Visibility(
visible: allowWipeoutTimeValue,
child: wipeoutTime,
),
],
),
showMessageBarHints,
colorSetSelect,
],
Expand Down
4 changes: 1 addition & 3 deletions lib/utils/common_object.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import "package:logging/logging.dart";
import "package:vidar/utils/contact.dart";
import "package:vidar/utils/conversation.dart";
import "package:vidar/utils/settings.dart";

class CommonObject {
static ContactList contactList = ContactList(<Contact>[]);
static Settings settings = Settings();
static Logger? logger;
static List<String> logs = <String>[];

static Conversation? currentConversation;
static DateTime? lastLogon;
}
26 changes: 12 additions & 14 deletions lib/utils/settings.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "package:vidar/utils/colors.dart";
import "package:vidar/utils/common_object.dart";

/// Static class for storing the active user settings of the program.
class Settings {
Expand All @@ -10,42 +9,41 @@ class Settings {

static bool showEncryptionKeyInEditContact = false;

static bool allowWipeoutTime = false;

static int wipeoutTime = 0;

static bool showMessageBarHints = true;

static ColorSet colorSet = vidarColorSet;

/// Get map of the state of all instance variable of Settings.
Map<String, dynamic> toMap() {
static Map<String, dynamic> toMap() {
return <String, dynamic>{
"allowUnencryptedMessages": allowUnencryptedMessages,
"keepLogs": keepLogs,
"showEncryptionKeyInEditContact": showEncryptionKeyInEditContact,
"allowWipeoutTime": allowWipeoutTime,
"wipeoutTime": wipeoutTime,
"showMessageBarHints": showMessageBarHints,
"colorSet": colorSet.name,
};
}

/// Set state of all instance variables of Settings from map.
/// If setting is not found then it goes to the default setting
void fromMap(final Map<String, dynamic> map) {
static void fromMap(final Map<String, dynamic> map) {
keepLogs = map["keepLogs"] as bool? ?? keepLogs;
showEncryptionKeyInEditContact =
map["showEncryptionKeyInEditContact"] as bool? ??
showEncryptionKeyInEditContact;
showMessageBarHints =
map["showMessageBarHints"] as bool? ?? showMessageBarHints;
allowWipeoutTime = map["allowWipeoutTime"] as bool? ?? allowWipeoutTime;
wipeoutTime = map["wipeoutTime"] as int? ?? wipeoutTime;
colorSet = getColorSetFromName(map["colorSet"] as String? ?? "default");

final bool? newAllowUnencryptedMessages =
map["allowUnencryptedMessages"]! as bool?;
if (newAllowUnencryptedMessages == null) {
if (keepLogs) {
CommonObject.logger!.info(
"allowUnencryptedMessages was not in map, defaulting to $allowUnencryptedMessages",
);
}
} else {
allowUnencryptedMessages = newAllowUnencryptedMessages;
}
allowUnencryptedMessages =
map["allowUnencryptedMessages"]! as bool? ?? allowUnencryptedMessages;
}
}
34 changes: 20 additions & 14 deletions lib/utils/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import "package:vidar/utils/settings.dart";
import "package:vidar/widgets/error_popup.dart";

Future<void> saveData(
final ContactList contactList,
final Settings settings, {
final ContactList contactList, {
final BuildContext? context,
}) async {
try {
Expand All @@ -27,7 +26,7 @@ Future<void> saveData(
}

_saveKeys(contactList);
saveSettings(settings, sharedPreferences: prefs);
saveSettings(sharedPreferences: prefs);

await prefs.setStringList("contacts", jsonContacts);

Expand All @@ -52,8 +51,7 @@ Future<void> saveData(
}

Future<void> loadData(
final ContactList contactList,
final Settings settings, {
final ContactList contactList, {
final BuildContext? context,
}) async {
try {
Expand All @@ -63,6 +61,12 @@ Future<void> loadData(
prefs.getStringList("contacts") ?? <String>[];

final String? jsonSettings = prefs.getString("settings");
final String? lastLogon = prefs.getString("lastlogon");
if (lastLogon == null) {
CommonObject.lastLogon = null;
} else {
CommonObject.lastLogon = DateTime.tryParse(lastLogon);
}

final List<Contact> listOfContacts = <Contact>[];

Expand All @@ -76,7 +80,7 @@ Future<void> loadData(
_loadKeys(contactList);

if (jsonSettings != null) {
settings.fromMap(jsonDecode(jsonSettings) as Map<String, dynamic>);
Settings.fromMap(jsonDecode(jsonSettings) as Map<String, dynamic>);
}

if (Settings.keepLogs) {
Expand All @@ -100,23 +104,25 @@ Future<void> loadData(
}

/// Only saves settings, to save settings and contacts use [saveData]
Future<void> saveSettings(
final Settings settings, {
Future<void> saveSettings({
final SharedPreferences? sharedPreferences,
final BuildContext? context,
}) async {
try {
final SharedPreferences prefs =
sharedPreferences ?? await SharedPreferences.getInstance();
await prefs.setString("settings", jsonEncode(settings.toMap()));
await prefs.setString("settings", jsonEncode(Settings.toMap()));
if (Settings.keepLogs) {
// Showing Settings.keepLogs is redundant but it's there for consistency
CommonObject.logger!.config("""
======== SETTINGS ========
Allow Unencrypted Messages: ${Settings.allowUnencryptedMessages}
Keep Logs: ${Settings.keepLogs}
Color set: ${Settings.colorSet.name}
Show Message Bar Hints: ${Settings.showMessageBarHints}
======== SETTINGS ========
Allow unencrypted messages: ${Settings.allowUnencryptedMessages}
Keep Logs: ${Settings.keepLogs}
Color set: ${Settings.colorSet.name}
Show message bar hints: ${Settings.showMessageBarHints}
Show encryption key in edit contact: ${Settings.showEncryptionKeyInEditContact}
Allow wipeout: ${Settings.allowWipeoutTime}
Wipeout time: ${Settings.wipeoutTime} days
""");
}
} on Exception catch (error, stackTrace) {
Expand Down
Loading