locality_social_cloud 1.3.0 locality_social_cloud: ^1.3.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:flutter/cupertino.dart';
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/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 ChangeNotifier 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 LocalitySocialCloud.auth("123", "pwd");
String signature = LocalityAuth.signPayloadForServerPassword("f098ad", "pwd");
print(signature);
databaseFactory = databaseFactoryFfi;
sqfliteFfiInit();
PubSubSupervisor.cache = Cache(await MessageRepository.getInstance());
LocalitySocialCloud.connect(loggedInUserOrError.getUser());
GlobalClickCounter exampleController = GlobalClickCounter();
LocalitySocialCloud.supervise(exampleController, key: ChaCha20Key.fromString('example-key'));
if ( loggedInUserOrError.isError() ) {
print("ERROR: $loggedInUserOrError");
}
for(int i = 0; i < 12; i++) {
exampleController.increaseCounterByOne();
}
DiscoverUsers discoverUsers = LocalitySocialCloud.discoverUsers();
discoverUsers.startingWith('Maltii');
discoverUsers.addListener(() {
print("WE DISCOVERED SOME USERS!");
for (var user in discoverUsers.discoveredUsers) {
print("WE DISCOVERED: ${user.id}");
}
});
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");
}
});
}