Merge branch 'master' into feature/release-2.7

This commit is contained in:
Roland Geider
2026-08-26 13:52:53 +02:00
18 changed files with 2018 additions and 2558 deletions

View File

@@ -27,6 +27,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:powersync/powersync.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:stream_transform/stream_transform.dart';
import 'package:wger/core/http_overrides.dart';
import 'package:wger/core/logs.dart';
import 'package:wger/core/network/auth_http_client.dart';
import 'package:wger/core/network/network_provider.dart';
@@ -106,16 +107,16 @@ Future<PowerSyncDatabase> powerSyncInstance(Ref ref) async {
// Gated on the network adapter only, never on the reachability probe: a
// probe failure is an indication, an unreachable backend is PowerSync's own
// retry loop to handle (the connector throttles its log output while the
// backend does not answer). Reconnecting while already connected is safe and
// doubles as the "retry now" signal when the adapter comes back.
// backend does not answer).
void syncConnection(bool hasAdapter) {
if (hasAdapter) {
final serverUrl = ref.read(wgerBaseProvider).serverUrl;
if (serverUrl != null) {
_logger.info('Network adapter available, connecting to the sync service');
connectPowerSync(db, serverUrl, client, watchdog);
} else {
if (serverUrl == null) {
_logger.info('Network adapter available, but no server configured: not connecting');
} else if (skipAdapterReconnect(db.currentStatus)) {
_logger.fine('Sync already connected, skipping reconnect');
} else {
connectPowerSync(db, serverUrl, client, watchdog, reason: 'network adapter available');
}
} else {
_logger.info('No network adapter, disconnecting from the sync service');
@@ -214,30 +215,53 @@ Future<void> _createRawTables(PowerSyncDatabase db) async {
});
}
/// Whether an adapter-triggered connect may be skipped: only while the stream
/// is already up. The SDK's connect() is never a no-op and would abort a
/// healthy stream; every other state (connecting, error, retry delay) still
/// reconnects, which doubles as the "retry now" signal when the adapter comes
/// back. Deliberate reconnects (login, manual retry) bypass this guard.
@visibleForTesting
bool skipAdapterReconnect(SyncStatus status) => status.connected;
/// Creates a fresh [DjangoConnector] for [baseUrl] and connects [db] to it.
/// Used both at initial creation and after a logout/login cycle to pick up
/// the new user's server URL / credentials. [client] is the authenticated
/// HTTP client (see [authenticatedHttpClientProvider]); the connector reuses
/// it for its REST calls so the same `Authorization` injection and
/// pre-emptive refresh apply.
///
/// [reason] just names the trigger in the log
void connectPowerSync(
PowerSyncDatabase db,
String baseUrl,
http.Client client,
SyncStreamWatchdog watchdog,
) {
SyncStreamWatchdog watchdog, {
required String reason,
}) {
_logger.info('Connecting to the sync service ($reason)');
db.connect(
connector: DjangoConnector(
baseUrl: baseUrl,
apiClient: ApiClient(baseUrl, client: client),
client: client,
),
options: syncOptionsFor(baseUrl),
);
// A connect that never starts the sync client emits no status event, so
// arming the watchdog is inseparable from requesting the connection.
watchdog.onConnectRequested();
}
/// Sync options carrying the self-signed cert exemption for [baseUrl], or
/// null when there is none. The stream downloads in a separate isolate whose
/// HTTP client does not see HttpOverrides.global, so it travels explicitly.
SyncOptions? syncOptionsFor(String baseUrl) {
final exemptHost = kIsWeb ? null : WgerHttpOverrides.exemptHost(baseUrl);
return exemptHost == null
? null
: SyncOptions(httpClient: WgerHttpOverrides.syncHttpClientFactory(exemptHost));
}
/// Number of local changes still waiting in the upload queue. Emits again
/// whenever the queue changes, so consumers can show a live count.
final pendingUploadCountProvider = StreamProvider.autoDispose<int>((ref) async* {

View File

@@ -46,4 +46,4 @@ final class PowerSyncInstanceProvider
}
}
String _$powerSyncInstanceHash() => r'8eacedfa3e5d5b41168f7517cbe19400a895b130';
String _$powerSyncInstanceHash() => r'5d245fd575f1839d9920644027eb027f55b0e117';