locality_social_cloud 1.5.0 locality_social_cloud: ^1.5.0 copied to clipboard
Write social apps with Flutter. This code connects to the backend of Locality Social Cloud to provide realtime state synchronization across devices.
example/locality_social_cloud_example.dart
import 'dart:async';
import 'dart:math' as math;
import 'package:locality_social_cloud/api/locality_auth.dart';
import 'package:locality_social_cloud/api/cache.dart';
import 'package:locality_social_cloud/api/discover_users.dart';
import 'package:locality_social_cloud/api/geo_channel.dart';
import 'package:locality_social_cloud/api/locality_social_cloud.dart';
import 'package:locality_social_cloud/api/logged_in_user.dart';
import 'package:locality_social_cloud/api/message_repository.dart';
import 'package:locality_social_cloud/api/pub_sub.dart';
import 'package:locality_social_cloud/api/pubsub_supervisor.dart';
import 'package:locality_social_cloud/api/throttled_change_notifier.dart';
import 'package:locality_social_cloud/api/topic_auth.dart';
import 'package:locality_social_cloud/api/locality_event.dart';
import 'package:m511chacha20/chacha20_key.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:uuid/uuid.dart';
class GlobalClickCounter extends ThrottledChangeNotifier with PubSub {
int counter = 0;
/// This is a global click counter. All users can increase the click counter. All users see the same click counter.
@override
String getTopic() {
return 'global-click-counter';
}
void increaseCounterByOne() {
send('increaseCounter', {'amount': 1});
}
@override
void onReceive(LocalityEvent localityEvent) {
switch (localityEvent.event) {
case 'increaseCounter':
counter += localityEvent.payload['amount'] as int;
print("CLICK COUNTER IS AT $counter");
notifyListeners();
break;
}
}
}
void main() async {
LocalitySocialCloud.configure(
appId: 'YOUR APP ID', appSecret: 'YOUR APP SECRET');
LoggedInUserOrError loggedInUserOrError =
await LocalityAuth.login("test_user_locality", "pwd");
// Used to set up SQLite on Windows
databaseFactory = databaseFactoryFfi;
sqfliteFfiInit();
PubSubSupervisor.cache = Cache(await MessageRepository.getInstance());
if (loggedInUserOrError.isError()) {
throw Exception("Could not connect to the Locality Social Cloud");
}
LocalitySocialCloud.connect(loggedInUserOrError.getUser());
GlobalClickCounter exampleController = GlobalClickCounter();
LocalitySocialCloud.supervise(exampleController,
key: ChaCha20Key.fromString('example-key'));
exampleController.timeline.whenSynchronized(() {
print("NOW SYNCHRONIZED");
print("CLICK COUNTER: ${exampleController.counter}");
});
exampleController.addListener(() {
print(
"EXAMPLE CONTROLLER WAS CLICKED. Throttled Provider ensures listeners are max notified at 120 fps for state updates.");
print("Example click counter: ${exampleController.counter}");
});
for (int i = 0; i < 12; i++) {
exampleController.increaseCounterByOne();
}
DiscoverUsers discoverUsers = LocalitySocialCloud.discoverUsers();
discoverUsers.addListener(() {
print("WE DISCOVERED SOME USERS!");
for (var user in discoverUsers.discoveredUsers) {
print("WE DISCOVERED: ${user.id}");
}
});
discoverUsers.startingWith('Maltii');
// Ensure the code has enough time to run
await Future.delayed(Duration(milliseconds: 3000));
}
void geoChannelExample() {
GeoChannel geoIndex = GeoChannel('nearby-people', metadata: "Hello!!");
geoIndex.connect(PubSubSupervisor.supervisor);
Timer.periodic(Duration(milliseconds: 1000), (timer) {
double randomLatitude = 90.7128 + math.Random().nextDouble() * 10;
double randomLongitude = -74.0060 + math.Random().nextDouble() * 10;
geoIndex.putGeoEntity(Uuid().v1(), {},
latitude: randomLatitude,
longitude: randomLongitude,
lifetimeSeconds: 30);
geoIndex.getNearbyEntities(90.7128, -74.0060, 6).then((value) {
print(value.toString());
});
});
}
void authTopicExample(LoggedInUser loggedInUser) async {
TopicAuth topicAuth = LocalitySocialCloud.loadTopicAuths(loggedInUser);
topicAuth.secureChannel(
channel: 'globally-unique-topic-id',
secret: 'generated_secret_password',
metadata: {
'img': 'https://.....jpg',
'groupname': 'New Chatgroup',
'role': 'Administrator'
});
topicAuth.addListener(() {
print("LISTING ALL SECURE CHANNELS: ");
for (var element in topicAuth.secureChannels) {
print("SECURE CHANNEL $element");
}
});
}