-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace shared_preferences for saving settings Threadstate saving proof of concept with scrolled-to post
- Loading branch information
Showing
17 changed files
with
471 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
// @dart=2.9 | ||
import 'package:chan/services/persistence.dart'; | ||
import 'package:flutter/foundation.dart' show debugDefaultTargetPlatformOverride; | ||
import 'package:flutter/material.dart'; | ||
|
||
import './main.dart'; | ||
|
||
void main() { | ||
void main() async { | ||
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; | ||
await Persistence.initialize(); | ||
runApp(ChanApp()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'package:chan/services/settings.dart'; | ||
import 'package:hive/hive.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
part 'persistence.g.dart'; | ||
|
||
class Persistence { | ||
static final threadStateBox = Hive.lazyBox<PersistentThreadState>('threadStates'); | ||
|
||
static Future<void> initialize() async { | ||
await Hive.initFlutter(); | ||
Hive.registerAdapter(ThemeSettingAdapter()); | ||
Hive.registerAdapter(AutoloadAttachmentsSettingAdapter()); | ||
Hive.registerAdapter(SavedSettingsAdapter()); | ||
await Hive.openBox<SavedSettings>('settings'); | ||
Hive.registerAdapter(PostReceiptAdapter()); | ||
Hive.registerAdapter(PersistentThreadStateAdapter()); | ||
await Hive.openLazyBox<PersistentThreadState>('threadStates'); | ||
} | ||
|
||
static Future<PersistentThreadState> getThreadState(String board, int id, {bool updateOpenedTime = false}) async { | ||
final existingState = await threadStateBox.get('$board/$id'); | ||
if (existingState != null) { | ||
if (updateOpenedTime) { | ||
existingState.lastOpenedTime = DateTime.now(); | ||
await existingState.save(); | ||
} | ||
return existingState; | ||
} | ||
else { | ||
final newState = PersistentThreadState( | ||
lastOpenedTime: updateOpenedTime ? DateTime.now() : null | ||
); | ||
threadStateBox.put('$board/$id', newState); | ||
return newState; | ||
} | ||
} | ||
} | ||
|
||
@HiveType(typeId: 3) | ||
class PersistentThreadState extends HiveObject { | ||
@HiveField(0) | ||
int? lastSeenPostId; | ||
@HiveField(1) | ||
DateTime lastOpenedTime; | ||
@HiveField(2) | ||
bool watched; | ||
@HiveField(3) | ||
final List<PostReceipt> receipts; | ||
|
||
PersistentThreadState({ | ||
this.lastSeenPostId, | ||
DateTime? lastOpenedTime, | ||
this.watched = false, | ||
this.receipts = const [] | ||
}) : this.lastOpenedTime = lastOpenedTime ?? DateTime.now(); | ||
|
||
@override | ||
String toString() => 'PersistentThreadState(lastSeenPostId: $lastSeenPostId, receipts: $receipts'; | ||
} | ||
|
||
@HiveType(typeId: 4) | ||
class PostReceipt extends HiveObject { | ||
@HiveField(0) | ||
final String password; | ||
@HiveField(1) | ||
final int id; | ||
PostReceipt({ | ||
required this.password, | ||
required this.id | ||
}); | ||
@override | ||
String toString() => 'PostReceipt(id: $id, password: $password)'; | ||
} |
Oops, something went wrong.