locality_social_cloud 1.0.0 locality_social_cloud: ^1.0.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/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/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:uuid/uuid.dart';
class ExampleController extends ChangeNotifier with PubSub {
int counter = 0;
@override
String getTopic() {
return 'global-click-counter5';
}
void increaseCounter(int amount) {
send('increaseCounter', {
'amount': amount
});
}
@override
void onReceive(LocalityEvent localityEvent) {
switch(localityEvent.event) {
case 'increaseCounter':
counter += localityEvent.payload['amount'] as int;
print("CLICK COUNTER IS AT "+counter.toString());
notifyListeners();
break;
}
}
}
void main() async
{
LocalitySocialCloud.configure(app_id: 'Locality App', app_secret: '6mSgwtp85n7BQA5c6dcDsioMwuH6AHWtYBICpkdZvN4=');
LoggedInUserOrError asd = await Auth.login("123", "pwd");
String signature = Auth.sign_payload_for_server_password("f098ad", "pwd");
print(signature);
databaseFactory = databaseFactoryFfi;
sqfliteFfiInit();
PubSubSupervisor.cache = Cache(await MessageRepository.getInstance());
LoggedInUserOrError loggedInUserOrError = await Auth.login("123", "pwd");
if ( loggedInUserOrError.isUser() ) {
LocalitySocialCloud.connect(loggedInUserOrError.getUser());
LoggedInUser localityUser = loggedInUserOrError.getUser();
controllerExample(localityUser);
discoverUsersExample(localityUser);
} else {
print("ERROR: "+loggedInUserOrError.authError.toString());
}
geoChannelExample();
}
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,
lifetime_seconds: 30
);
geoIndex.getNearbyEntities(90.7128, -74.0060, 6).then((value){
print(value.toString());
});
});
}
void controllerExample(LoggedInUser loggedInUser) {
ExampleController exampleController = ExampleController();
LocalitySocialCloud.supervise(exampleController, key: ChaCha20Key.fromString('example-key'));
for(int i = 0; i < 12; i++) {
exampleController.increaseCounter(1);
}
}
void discoverUsersExample(LoggedInUser loggedInUser) {
DiscoverUsers discoverUsers = LocalitySocialCloud.discoverUsers();
discoverUsers.startingWith('Maltii');
discoverUsers.addListener(() {
print("WE DISCOVERED SOME USERS!");
discoverUsers.discoveredUsers.forEach((user) {
print("WE DISCOVERED: "+user.id);
});
});
}
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: ");
topicAuth.secureChannels.forEach((element) {
print("SECURE CHANNEL "+element.toString());
});
});
}