Skip to content

Commit

Permalink
Merge pull request #32 from SaltechSystems/example-updates
Browse files Browse the repository at this point in the history
Example updates
  • Loading branch information
bawelter committed Apr 12, 2020
2 parents 172f7e1 + 81558f1 commit 42b92ac
Show file tree
Hide file tree
Showing 46 changed files with 2,802 additions and 310 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@

* Updated Coubchbase Lite libraries to version 2.7.0
* Migrated to Android X
* Added concurrency control
* Added concurrency control

## 2.7.0+1

* Updated Examples to use bloc pattern

## 2.7.0+2

* Updated documentation
54 changes: 39 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,41 @@ For help getting started with Flutter, view the

## API References

[Swift SDK API References](https://docs.couchbase.com/mobile/2.5.0/couchbase-lite-swift/)
[Swift SDK API References](https://docs.couchbase.com/mobile/2.7.0/couchbase-lite-swift/)

[Java SDK API References](http://docs.couchbase.com/mobile/2.5.0/couchbase-lite-java)
[Java SDK API References](http://docs.couchbase.com/mobile/2.7.0/couchbase-lite-java)

*Note*: Syntax follows the Swift SDK but these are the SDKs used for the platform code.

## Local Server Setup

Download and setup Couchbase Server / Sync Gateway Community Editions on your local machine the following link
- [Sync Gatway Getting Started](https://docs.couchbase.com/sync-gateway/current/getting-started.html)
- [Couchbase Downloads](https://www.couchbase.com/downloads)

Setup beer-sample database [Local Couchbase Server](http://127.0.0.1:8091/):

- Add the beer-sample bucket: Settings > Sample Buckets
- Create a sync_gateway user in the Couchbase Server under Security
- Give sync_gateway access to the beer-sample

Start Sync Gateway:

~/Downloads/couchbase-sync-gateway/bin/sync_gateway ~/path/to/sync-gateway-config.json

*Note*: Included in this example is sync-gateway-config.json (Login => u: foo, p: bar)


## Usage example

Below is an example for the database using the BLoC pattern ( View <-> BLoC <-> Repository <-> Database )
*Note*: The protocol ws and not wss is used in the example. The easy way to implement this is to use this attribute to your AndroidManifest.xml where you allow all http for all requests:

The files can also be found in the plugin example but are not used in the main.dart. The example will be revised in the near future to use the BLoC pattern.
```xml
<application android:usesCleartextTraffic="true">
</application>
```

Below is an example for the database using the BLoC pattern ( View <-> BLoC <-> Repository <-> Database )

```dart
class AppDatabase {
Expand All @@ -74,12 +98,12 @@ class AppDatabase {
database = await Database.initWithName(dbName);
// Note wss://10.0.2.2:4984/my-database is for the android simulator on your local machine's couchbase database
ReplicatorConfiguration config =
ReplicatorConfiguration(database, "wss://10.0.2.2:4984/my-database");
ReplicatorConfiguration(database, "ws://10.0.2.2:4984/beer-sample");
config.replicatorType = ReplicatorType.pushAndPull;
config.continuous = true;
// Using self signed certificate
config.pinnedServerCertificate = "assets/cert-android.cer";
//config.pinnedServerCertificate = "assets/cert-android.cer";
config.authenticator = BasicAuthenticator(username, password);
replicator = Replicator(config);
Expand All @@ -105,22 +129,22 @@ class AppDatabase {
replicator.addChangeListener((ReplicatorChange event) async {
if (event.status.activity == ReplicatorActivityLevel.stopped) {
await database.close();
// Change listeners will be
//replicator.removeChangeListener(_replicatorListenerToken);
await replicator.removeChangeListener(_replicatorListenerToken);
await replicator.dispose();
_replicatorListenerToken = null;
}
});
await replicator.stop();
}
Future<Document> createDocument(Map<String, dynamic> map) async {
var id = "mydocument::${Uuid().v1()}";
Future<Document> createDocumentIfNotExists(String id, Map<String, dynamic> map) async {
try {
var doc = MutableDocument(id: id, data: map);
if (await database.saveDocument(doc)) {
return doc;
var oldDoc = await database.document(id);
if (oldDoc != null) return oldDoc;
var newDoc = MutableDocument(id: id, data: map);
if (await database.saveDocument(newDoc)) {
return newDoc;
} else {
return null;
}
Expand Down Expand Up @@ -180,7 +204,7 @@ class AppDatabase {
rethrow;
}
return ObservableResponse<T>(subject.debounce(Duration(seconds: 1)), () {
return ObservableResponse<T>(subject, () {
removeListener();
subject.close();
});
Expand Down
15 changes: 1 addition & 14 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
def PLUGIN = "couchbase_lite";
def ANDROIDX_WARNING = "flutterPluginsAndroidXWarning";
gradle.buildFinished { buildResult ->
if (buildResult.failure && !rootProject.ext.has(ANDROIDX_WARNING)) {
println ' *********************************************************'
println 'WARNING: This version of ' + PLUGIN + ' will break your Android build if it or its dependencies aren\'t compatible with AndroidX.'
println ' See https://goo.gl/CP92wY for more information on the problem and how to fix it.'
println ' This warning prints for all Android build failures. The real root cause of the error may be unrelated.'
println ' *********************************************************'
rootProject.ext.set(ANDROIDX_WARNING, true);
}
}

group 'com.saltechsystems.couchbase_lite'
version '1.0-SNAPSHOT'

Expand All @@ -21,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

Expand Down
4 changes: 3 additions & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
5 changes: 5 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
1 change: 1 addition & 0 deletions example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_info":"// This is a generated file; do not edit or check into version control.","dependencyGraph":[{"name":"couchbase_lite","dependencies":[]},{"name":"package_info","dependencies":[]},{"name":"url_launcher","dependencies":["url_launcher_web","url_launcher_macos"]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]}]}
18 changes: 18 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,21 @@ A few resources to get you started if this is your first Flutter project:
For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.

## Local Server Setup

Download and setup Couchbase Server / Sync Gateway Community Editions on your local machine the following link
- [Sync Gatway Getting Started](https://docs.couchbase.com/sync-gateway/current/getting-started.html)
- [Couchbase Downloads](https://www.couchbase.com/downloads)

Setup beer-sample database [Local Couchbase Server](http://127.0.0.1:8091/):

- Add the beer-sample bucket: Settings > Sample Buckets
- Create a sync_gateway user in the Couchbase Server under Security
- Give sync_gateway access to the beer-sample

Start Sync Gateway:

~/Downloads/couchbase-sync-gateway/bin/sync_gateway ~/path/to/sync-gateway-config.json

*Note*: Included in this example is sync-gateway-config.json (Login => u: foo, p: bar)
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:name="io.flutter.app.FlutterApplication"
android:label="couchbase_lite_example"
android:icon="@mipmap/ic_launcher"
android:networkSecurityConfig="@xml/network_security_config"
tools:replace="android:label">
<activity
android:name=".MainActivity"
Expand Down
13 changes: 13 additions & 0 deletions example/android/app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">10.0.2.2</domain>
<domain includeSubdomains="false">10.0.3.2</domain>
</domain-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

Expand Down
3 changes: 3 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
10 changes: 10 additions & 0 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/saltech/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/saltech/Documents/FlutterProjects/couchbase_lite/example"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "FLUTTER_FRAMEWORK_DIR=/Users/saltech/flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
97 changes: 97 additions & 0 deletions example/lib/app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2020-present the Saltech Systems authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:couchbase_lite_example/pages/home_page.dart';
import 'package:flutter/material.dart';
import 'colors.dart';

import 'package:couchbase_lite_example/data/api_provider.dart';
import 'package:couchbase_lite_example/data/repository.dart';
import 'package:couchbase_lite_example/pages/login.dart';

enum AppMode { production, development }

class MyApp extends StatefulWidget {
MyApp(this.mode);

final AppMode mode;

@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Werk Sheets',
theme: _kTheme,
routes: {
'/home': (BuildContext context) => HomePage(),
'/': (BuildContext context) => LoginPage(widget.mode),
},
);
}

bool isActive() => mounted;

@override
void dispose() {
ApiProvider.instance.client.close();
Repository.instance.dispose();
super.dispose();
}
}

final ThemeData _kTheme = _buildTheme();

ThemeData _buildTheme() {
final ThemeData base = ThemeData(
// Define the default brightness and colors.
brightness: Brightness.light,
primaryColor: kPrimaryColor,
accentColor: kAccentColor,

// Define the default font family.
fontFamily: 'Rubik');

return base.copyWith(
buttonTheme: base.buttonTheme.copyWith(
textTheme: ButtonTextTheme.primary,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(4.0)),
),
),
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4.0)),
),
),
textTheme: _buildTextTheme(base.textTheme),
//primaryTextTheme: _buildTextTheme(base.primaryTextTheme),
accentTextTheme: _buildTextTheme(base.accentTextTheme),
);
}

TextTheme _buildTextTheme(TextTheme base) {
return base.apply(
displayColor: kPrimaryColor,
bodyColor: kPrimaryColor,
);
}
Loading

0 comments on commit 42b92ac

Please sign in to comment.