~emersion/goguma

c737e1b6a4dfbba9aff11f215c7648dc4ec881e3 — Simon Ser a month ago 60e700b
Introduce Prefs
M lib/dialog/edit_profile.dart => lib/dialog/edit_profile.dart +8 -4
@@ 1,11 1,11 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../client_controller.dart';
import '../database.dart';
import '../irc.dart';
import '../models.dart';
import '../prefs.dart';

class EditProfileDialog extends StatefulWidget {
	final NetworkModel network;


@@ 64,7 64,7 @@ class _EditProfileDialogState extends State<EditProfileDialog> {
		Navigator.pop(context);

		var client = context.read<ClientProvider>().get(widget.network);
		var sharedPreferences = context.read<SharedPreferences>();
		var prefs = context.read<Prefs>();

		var nickname = _nicknameController.text;
		var realname = _realnameController.text;


@@ 77,7 77,7 @@ class _EditProfileDialogState extends State<EditProfileDialog> {
			// in our DB to avoid saving a bogus nickname.
			await client.setNickname(nickname);

			await sharedPreferences.setString('nickname', nickname);
			prefs.nickname = nickname;

			// Previous versions of Goguma stored the nickname in the DB. New
			// versions store it in SharedPreferences. Clear the DB if


@@ 91,7 91,11 @@ class _EditProfileDialogState extends State<EditProfileDialog> {
		if (realname != client.realname) {
			await client.setRealname(realname);

			await sharedPreferences.setString('realname', realname);
			if (realname != nickname) {
				prefs.realname = realname;
			} else {
				prefs.realname = null;
			}
		}
	}


M lib/main.dart => lib/main.dart +5 -10
@@ 6,7 6,6 @@ import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:workmanager/workmanager.dart';

import 'android_le.dart';


@@ 16,6 15,7 @@ import 'client_controller.dart';
import 'database.dart';
import 'models.dart';
import 'notification_controller.dart';
import 'prefs.dart';

// Debugging knobs for work manager.
const _debugWorkManager = false;


@@ 45,9 45,7 @@ void _main() async {
	}

	var notifController = NotificationController();

	var sharedPreferences = await SharedPreferences.getInstance();

	var prefs = await Prefs.load();
	var db = await DB.open();

	// Load all the data we need concurrently


@@ 63,9 61,6 @@ void _main() async {
	var unreadCounts = await unreadCountsFuture;
	var lastDeliveredTimes = await lastDeliveredTimesFuture;

	var defaultNickname = sharedPreferences.getString('nickname');
	var defaultRealname = sharedPreferences.getString('realname');

	var networkList = NetworkListModel();
	var bufferList = BufferListModel();
	var bouncerNetworkList = BouncerNetworkListModel();


@@ 86,8 81,8 @@ void _main() async {

		var clientParams = connectParamsFromServerEntry(
			serverEntry,
			defaultNickname: defaultNickname ?? 'user',
			defaultRealname: defaultRealname,
			defaultNickname: prefs.nickname,
			defaultRealname: prefs.realname,
		);
		if (networkEntry.bouncerId != null) {
			clientParams = clientParams.apply(bouncerNetId: networkEntry.bouncerId);


@@ 125,7 120,7 @@ void _main() async {
			Provider<DB>.value(value: db),
			Provider<ClientProvider>.value(value: clientProvider),
			Provider<NotificationController>.value(value: notifController),
			Provider<SharedPreferences>.value(value: sharedPreferences),
			Provider<Prefs>.value(value: prefs),
			ChangeNotifierProvider<NetworkListModel>.value(value: networkList),
			ChangeNotifierProvider<BufferListModel>.value(value: bufferList),
			ChangeNotifierProvider<BouncerNetworkListModel>.value(value: bouncerNetworkList),

M lib/page/buffer.dart => lib/page/buffer.dart +6 -3
@@ 3,7 3,6 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../ansi.dart';
import '../client.dart';


@@ 13,6 12,7 @@ import '../irc.dart';
import '../linkify.dart';
import '../models.dart';
import '../notification_controller.dart';
import '../prefs.dart';
import '../widget/network_indicator.dart';
import '../widget/swipe_action.dart';
import 'buffer_details.dart';


@@ 321,8 321,10 @@ class BufferPageState extends State<BufferPage> with WidgetsBindingObserver {
	@override
	Widget build(BuildContext context) {
		var client = context.read<Client>();
		var prefs = context.read<Prefs>();
		var buffer = context.watch<BufferModel>();
		var network = context.watch<NetworkModel>();

		var subtitle = buffer.topic ?? buffer.realname;
		var isOnline = network.state == NetworkState.synchronizing || network.state == NetworkState.online;
		var canSendMessage = isOnline;


@@ 333,8 335,9 @@ class BufferPageState extends State<BufferPage> with WidgetsBindingObserver {
			canSendMessage = canSendMessage && buffer.online != false;
		}
		var messages = buffer.messages;
		var compact = context.read<SharedPreferences>().getBool('buffer_compact') ?? false;
		var showTyping = context.read<SharedPreferences>().getBool('typing_indicator') ?? false;

		var compact = prefs.bufferCompact;
		var showTyping = prefs.typingIndicator;
		if (!client.caps.enabled.contains('message-tags')) {
			showTyping = false;
		}

M lib/page/connect.dart => lib/page/connect.dart +3 -3
@@ 1,12 1,12 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../client.dart';
import '../client_controller.dart';
import '../database.dart';
import '../irc.dart';
import '../models.dart';
import '../prefs.dart';
import 'buffer_list.dart';

class ConnectPage extends StatefulWidget {


@@ 46,7 46,7 @@ class ConnectPageState extends State<ConnectPage> {
		});

		var db = context.read<DB>();
		var sharedPreferences = context.read<SharedPreferences>();
		var prefs = context.read<Prefs>();

		// TODO: only connect once (but be careful not to loose messages
		// sent immediately after RPL_WELCOME)


@@ 79,7 79,7 @@ class ConnectPageState extends State<ConnectPage> {
			});
		}

		await sharedPreferences.setString('nickname', nicknameController.text);
		prefs.nickname = nicknameController.text;

		client = Client(clientParams);
		var network = NetworkModel(serverEntry, networkEntry, client.nick, client.realname);

M lib/page/settings.dart => lib/page/settings.dart +11 -9
@@ 1,6 1,5 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';

import 'connect.dart';


@@ 10,6 9,7 @@ import '../database.dart';
import '../dialog/edit_profile.dart';
import '../irc.dart';
import '../models.dart';
import '../prefs.dart';
import 'edit_network.dart';
import 'network_details.dart';



@@ 29,8 29,10 @@ class _SettingsPageState extends State<SettingsPage> {
	@override
	void initState() {
		super.initState();
		_compact = context.read<SharedPreferences>().getBool('buffer_compact') ?? false;
		_typing = context.read<SharedPreferences>().getBool('typing_indicator') ?? false;

		var prefs = context.read<Prefs>();
		_compact = prefs.bufferCompact;
		_typing = prefs.typingIndicator;
	}

	void _logout() {


@@ 114,10 116,10 @@ class _SettingsPageState extends State<SettingsPage> {
					leading: Icon(Icons.reorder),
					trailing: Switch(
						value: _compact,
						onChanged: (bool c) {
						onChanged: (bool enabled) {
							context.read<Prefs>().bufferCompact = enabled;
							setState(() {
								_compact = c;
								context.read<SharedPreferences>().setBool('buffer_compact', c);
								_compact = enabled;
							});
						},
					),


@@ 127,10 129,10 @@ class _SettingsPageState extends State<SettingsPage> {
					leading: Icon(Icons.border_color),
					trailing: Switch(
						value: _typing,
						onChanged: (bool c) {
						onChanged: (bool enabled) {
							context.read<Prefs>().typingIndicator = enabled;
							setState(() {
								_typing = c;
								context.read<SharedPreferences>().setBool('typing_indicator', c);
								_typing = enabled;
							});
						},
					),

A lib/prefs.dart => lib/prefs.dart +41 -0
@@ 0,0 1,41 @@
import 'package:shared_preferences/shared_preferences.dart';

const _bufferCompactKey = 'buffer_compact';
const _typingIndicatorKey = 'typing_indicator';
const _nicknameKey = 'nickname';
const _realnameKey = 'realname';

class Prefs {
	final SharedPreferences _prefs;

	Prefs._(this._prefs);

	static Future<Prefs> load() async {
		return Prefs._(await SharedPreferences.getInstance());
	}

	bool get bufferCompact => _prefs.getBool(_bufferCompactKey) ?? false;
	bool get typingIndicator => _prefs.getBool(_typingIndicatorKey) ?? false;
	String get nickname => _prefs.getString(_nicknameKey) ?? 'user';
	String? get realname => _prefs.getString(_realnameKey);

	set bufferCompact(bool enabled) {
		_prefs.setBool(_bufferCompactKey, enabled);
	}

	set typingIndicator(bool enabled) {
		_prefs.setBool(_typingIndicatorKey, enabled);
	}

	set nickname(String nickname) {
		_prefs.setString(_nicknameKey, nickname);
	}

	set realname(String? realname) {
		if (realname != null) {
			_prefs.setString(_realnameKey, realname);
		} else {
			_prefs.remove(_realnameKey);
		}
	}
}