Commit 857e6c2f authored by Jeremie TISSERAND's avatar Jeremie TISSERAND
Browse files

Set Java 8 compatibility

Use DatabaseManagement to manage database instead of using static methods
Clean database
parent 9ff8466a
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
</code_scheme>
</component>
\ No newline at end of file
......@@ -24,7 +24,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -16,6 +16,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
......@@ -25,5 +29,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.couchbase.lite:couchbase-lite-android:2.0.0'
api 'com.couchbase.lite:couchbase-lite-android:2.0.0'
}
......@@ -3,15 +3,14 @@ package com.ippon.article.clientmobile;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Result;
import com.ippon.article.clientmobile.database.CouchbaseLite;
import com.ippon.article.clientmobile.database.DatabaseManagement;
import java.net.URISyntaxException;
import java.util.ArrayList;
......@@ -30,67 +29,91 @@ public class ArticleActivity extends AppCompatActivity {
private final static String GATEWAY_PWD = "test";
private Database database;
private final DatabaseManagement databaseManagement = new DatabaseManagement();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
TextView tag = findViewById(R.id.tag);
tag.setText(GATEWAY_USER);
}
@Override
protected void onStart() {
super.onStart();
initDatabase();
updateArticleView();
initCleanButton();
}
private void initCleanButton() {
Button clickButton = findViewById(R.id.clean);
clickButton.setOnClickListener(button -> {
try {
databaseManagement.deleteDatabase();
initDatabase();
updateArticleView();
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Database deletion", e);
}
});
}
private void initDatabase() {
// Init & Configure database
try {
database = CouchbaseLite.openDatabase(this.getApplicationContext());
//CouchbaseLite.createDocument(database);
databaseManagement.openDatabase(this.getApplicationContext(), e -> updateArticleView());
CouchbaseLite.configureReplication(database, GATEWAY_URL + GATEWAY_DB, GATEWAY_USER, GATEWAY_PWD);
databaseManagement.configureReplication(GATEWAY_URL + GATEWAY_DB, GATEWAY_USER, GATEWAY_PWD);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Application startup", e);
} catch (URISyntaxException uri) {
Log.e(TAG, "Gateway URL syntax problem", uri);
}
}
private void updateArticleView() {
List<String> updatedItems = loadArticles();
fillArticles(updatedItems);
}
Button clickButton = (Button) findViewById(R.id.refresh);
clickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Result> results = Collections.emptyList();
try {
results = CouchbaseLite.findAllDocuments(database, "com.ippon.article.couchbase.server.article.Article");
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Refresh documents from database", e);
}
Log.i(TAG, "----------RESULTS---------" + results.size());
List<String> items = new ArrayList<>();
for (Result result : results) {
Log.i(TAG, "------------------------");
if (result.getString("content") != null) {
Log.i(TAG, result.getString("content"));
items.add(result.getString("content"));
}
private List<String> loadArticles() {
List<Result> results = Collections.emptyList();
try {
results = databaseManagement.findAllDocuments("com.ippon.article.couchbase.server.article.Article");
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Refresh documents from database", e);
}
}
Log.i(TAG, "----------RESULTS---------" + results.size());
List<String> items = new ArrayList<>();
fillArticles(items);
for (Result result : results) {
Log.i(TAG, "------------------------");
if (result.getString("content") != null) {
Log.i(TAG, result.getString("content"));
items.add(result.getString("content"));
}
});
}
return items;
}
private void fillArticles(List<String> items) {
ListView articleView = (ListView) findViewById(R.id.articles);
ListView articleView = findViewById(R.id.articles);
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
articleView.setAdapter(itemsAdapter);
}
......
package com.ippon.article.clientmobile.database;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import com.couchbase.lite.BasicAuthenticator;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.DataSource;
import com.couchbase.lite.Database;
import com.couchbase.lite.DatabaseChangeListener;
import com.couchbase.lite.DatabaseConfiguration;
import com.couchbase.lite.Document;
import com.couchbase.lite.Endpoint;
import com.couchbase.lite.Expression;
import com.couchbase.lite.ListenerToken;
import com.couchbase.lite.LogDomain;
import com.couchbase.lite.LogLevel;
import com.couchbase.lite.MutableDocument;
import com.couchbase.lite.Query;
import com.couchbase.lite.QueryBuilder;
import com.couchbase.lite.Replicator;
import com.couchbase.lite.ReplicatorChange;
import com.couchbase.lite.ReplicatorChangeListener;
import com.couchbase.lite.ReplicatorConfiguration;
import com.couchbase.lite.Result;
import com.couchbase.lite.ResultSet;
......@@ -34,42 +31,43 @@ import java.util.List;
* Created by jeremie on 04/05/18.
*/
public class CouchbaseLite {
public class DatabaseManagement {
private final static String TAG = "CouchbaseLite";
private final static String TAG = "DatabaseManagement";
private Database database;
private ListenerToken databaseListenerToken;
private Replicator replicator;
private ListenerToken replicatorListenerToken;
@Nullable
public static Database openDatabase(Context androidContext) throws CouchbaseLiteException {
public void openDatabase(Context androidContext, DatabaseChangeListener listener) throws CouchbaseLiteException {
// Get the database (and create it if it doesn’t exist).
DatabaseConfiguration config = new DatabaseConfiguration(androidContext);
return new Database("localdb", config);
database = new Database("localdb", config);
databaseListenerToken = database.addChangeListener(listener);
}
public static void createDocument(Database database) throws CouchbaseLiteException {
// Create a new document (i.e. a record) in the database.
MutableDocument mutableDoc = new MutableDocument()
.setFloat("version", 2.0F)
.setString("type", "SDK");
// Save it to the database.
database.save(mutableDoc);
// Update a document.
mutableDoc = database.getDocument(mutableDoc.getId()).toMutable();
mutableDoc.setString("language", "Java");
try {
database.save(mutableDoc);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
public void deleteDatabase() throws CouchbaseLiteException {
// Remove listeners
replicator.removeChangeListener(replicatorListenerToken);
database.removeChangeListener(databaseListenerToken);
// Stop replication
replicator.stop();
while (replicator.getStatus().getActivityLevel() != Replicator.ActivityLevel.STOPPED) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.e(TAG, "Replication stop", e);
}
}
Document document = database.getDocument(mutableDoc.getId());
// Log the document ID (generated by the database) and properties
Log.i(TAG, "Document ID :: " + document.getId());
Log.i(TAG, "Learning " + document.getString("language"));
database.delete();
}
public static List<Result> findAllDocuments(Database database, String javaClass) throws CouchbaseLiteException {
public List<Result> findAllDocuments(String javaClass) throws CouchbaseLiteException {
// Create a query to fetch documents of type SDK.
Query query = QueryBuilder.select(SelectResult.property("content"))
.from(DataSource.database(database))
......@@ -81,7 +79,7 @@ public class CouchbaseLite {
return resultsList;
}
public static void configureReplication(Database database, String gatewayUrl, String gatewayUser, String gatewayPwd) throws CouchbaseLiteException, URISyntaxException {
public void configureReplication(String gatewayUrl, String gatewayUser, String gatewayPwd) throws URISyntaxException {
Database.setLogLevel(LogDomain.REPLICATOR, LogLevel.VERBOSE);
Database.setLogLevel(LogDomain.NETWORK, LogLevel.VERBOSE);
......@@ -96,20 +94,18 @@ public class CouchbaseLite {
replConfig.setContinuous(true);
// Create replicator.
Replicator replicator = new Replicator(replConfig);
replicator = new Replicator(replConfig);
// Listen to replicator change events.
replicator.addChangeListener(new ReplicatorChangeListener() {
@Override
public void changed(ReplicatorChange change) {
if (change.getStatus().getError() != null) {
Log.i(TAG, "Error code :: " + change.getStatus().getError().getCode());
}
replicatorListenerToken = replicator.addChangeListener(change -> {
if (change.getStatus().getError() != null) {
Log.i(TAG, "Error code :: " + change.getStatus().getError().getCode());
}
});
// Start replication.
replicator.start();
}
}
......@@ -7,6 +7,17 @@
tools:context="com.ippon.article.clientmobile.ArticleActivity"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="@+id/clean"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="5dp"
android:text="Clean"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside" />
<ListView
android:id="@+id/articles"
android:layout_width="0dp"
......@@ -17,17 +28,26 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/refresh" />
app:layout_constraintTop_toBottomOf="@+id/clean" />
<Button
android:id="@+id/refresh"
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:text="Refresh"
app:layout_constraintBottom_toTopOf="@+id/articles"
android:layout_height="18dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Articles : "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tag"
android:layout_width="174dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
......@@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
......
#Thu May 03 21:47:31 CEST 2018
#Mon May 07 08:41:19 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment