Skip to content

Commit

Permalink
60s delay from the first device's connectes to activate wals, 120 mis…
Browse files Browse the repository at this point in the history
…s seconds as threshold before displaying the 'sync now' to user.
  • Loading branch information
beastoin committed Oct 7, 2024
1 parent c95a7c5 commit f8779fd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/lib/pages/memories/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class _MemoriesPageState extends State<MemoriesPage> with AutomaticKeepAliveClie
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: memoryProvider.missingWals.isNotEmpty
child: memoryProvider.missingWalsInSeconds > 120
? GestureDetector(
onTap: () {
routeToPage(context, const SyncPage());
Expand All @@ -95,7 +95,7 @@ class _MemoriesPageState extends State<MemoriesPage> with AutomaticKeepAliveClie
child: ListTile(
leading: const Icon(Icons.record_voice_over_rounded),
title: Text(
'You have ${secondsToHumanReadable(memoryProvider.missingWals.map((val) => val.seconds).reduce((a, b) => a + b))} of conversation locally, sync now?',
'You have ${secondsToHumanReadable(memoryProvider.missingWalsInSeconds)} of conversation locally, sync now?',
style: const TextStyle(color: Colors.white, fontSize: 16),
),
),
Expand Down
6 changes: 4 additions & 2 deletions app/lib/pages/onboarding/wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ class _OnboardingWrapperState extends State<OnboardingWrapper> with TickerProvid
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (isSignedIn()) {
// && !SharedPreferencesUtil().onboardingCompleted
context.read<HomeProvider>().setupHasSpeakerProfile();
_goNext();
if (mounted) {
context.read<HomeProvider>().setupHasSpeakerProfile();
_goNext();
}
}
});
super.initState();
Expand Down
23 changes: 15 additions & 8 deletions app/lib/providers/capture_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:friend_private/backend/schema/structured.dart';
import 'package:friend_private/backend/schema/transcript_segment.dart';
import 'package:friend_private/providers/memory_provider.dart';
import 'package:friend_private/providers/message_provider.dart';
import 'package:friend_private/services/devices.dart';
import 'package:friend_private/services/notifications.dart';
import 'package:friend_private/services/services.dart';
import 'package:friend_private/services/sockets/sdcard_socket.dart';
Expand Down Expand Up @@ -41,8 +42,9 @@ class CaptureProvider extends ChangeNotifier

ServerMemory? get inProgressMemory => _inProgressMemory;

bool _walFeatureEnabled = false;
IWalService get _wal => ServiceManager.instance().wal;
bool _walFeatureEnabled = true;
IWalService get _walService => ServiceManager.instance().wal;
IDeviceService get _deviceService => ServiceManager.instance().device;

void updateProviderInstances(MemoryProvider? mp, MessageProvider? p) {
memoryProvider = mp;
Expand Down Expand Up @@ -153,19 +155,24 @@ class CaptureProvider extends ChangeNotifier
_bleBytesStream = await _getBleAudioBytesListener(id, onAudioBytesReceived: (List<int> value) {
if (value.isEmpty) return;

// support: opus
var isWalSupported = codec == BleAudioCodec.opus && _walFeatureEnabled;
if (isWalSupported) {
_wal.onByteStream(value);
// support: opus codec, 1m from the first device connectes
var deviceFirstConnectedAt = _deviceService.getFirstConnectedAt();
var isWalEnabled = codec == BleAudioCodec.opus &&
(deviceFirstConnectedAt != null &&
deviceFirstConnectedAt.isBefore(DateTime.now().subtract(const Duration(seconds: 60)))) &&
_walFeatureEnabled;
if (isWalEnabled) {
_walService.onByteStream(value);
}

// send ws
if (_socket?.state == SocketServiceState.connected) {
final trimmedValue = value.sublist(3);
_socket?.send(trimmedValue);

// synced
if (isWalSupported) {
_wal.onBytesSync(value);
if (isWalEnabled) {
_walService.onBytesSync(value);
}
}
});
Expand Down
17 changes: 10 additions & 7 deletions app/lib/providers/memory_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal

List<ServerMemory> processingMemories = [];

IWalService get _wal => ServiceManager.instance().wal;
IWalService get _walService => ServiceManager.instance().wal;

List<Wal> _missingWals = [];
List<Wal> get missingWals => _missingWals;
int get missingWalsInSeconds =>
_missingWals.isEmpty ? 0 : _missingWals.map((val) => val.seconds).reduce((a, b) => a + b);

double _walsSyncedProgress = 0.0;
double get walsSyncedProgress => _walsSyncedProgress;
Expand All @@ -36,12 +39,12 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal
Map<String, dynamic>? syncResult;

MemoryProvider() {
_wal.subscribe(this, this);
_walService.subscribe(this, this);
_preload();
}

_preload() async {
_missingWals = await _wal.getMissingWals();
_missingWals = await _walService.getMissingWals();
notifyListeners();
}

Expand Down Expand Up @@ -311,19 +314,19 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal
@override
void dispose() {
_processingMemoryWatchTimer?.cancel();
_wal.unsubscribe(this);
_walService.unsubscribe(this);
super.dispose();
}

@override
void onNewMissingWal(Wal wal) async {
_missingWals = await _wal.getMissingWals();
_missingWals = await _walService.getMissingWals();
notifyListeners();
}

@override
void onWalSynced(Wal wal, {ServerMemory? memory}) async {
_missingWals = await _wal.getMissingWals();
_missingWals = await _walService.getMissingWals();
notifyListeners();
}

Expand All @@ -338,7 +341,7 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal
Future<Map<String, dynamic>?> syncWals() async {
_walsSyncedProgress = 0.0;
setIsSyncing(true);
var res = await _wal.syncAll(progress: this);
var res = await _walService.syncAll(progress: this);
syncResult = res.$1;
syncCompleted = true;
setIsSyncing(false);
Expand Down
14 changes: 14 additions & 0 deletions app/lib/services/devices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ abstract class IDeviceService {

void subscribe(IDeviceServiceSubsciption subscription, Object context);
void unsubscribe(Object context);

DateTime? getFirstConnectedAt();
}

enum DeviceServiceStatus {
Expand Down Expand Up @@ -49,6 +51,8 @@ class DeviceService implements IDeviceService {

DeviceServiceStatus get status => _status;

DateTime? _firstConnectedAt;

@override
Future<void> discover({
String? desirableDeviceId,
Expand Down Expand Up @@ -249,9 +253,19 @@ class DeviceService implements IDeviceService {

// connect
await _connectToDevice(deviceId);

if (_firstConnectedAt == null) {
_firstConnectedAt = DateTime.now();
}

return _connection;
} finally {
mutex = false;
}
}

@override
DateTime? getFirstConnectedAt() {
return _firstConnectedAt;
}
}

0 comments on commit f8779fd

Please sign in to comment.