Skip to content

Commit c848542

Browse files
committed
update usage docs and improve sample
1 parent b0b132e commit c848542

13 files changed

Lines changed: 247 additions & 17 deletions

File tree

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,53 @@ dependencies {
2525
}
2626
```
2727

28+
## Usage
29+
```java
30+
import android.os.Bundle;
31+
import android.support.v7.app.AppCompatActivity;
32+
import android.widget.ListView;
33+
import com.github.lykmapipo.sqlbrite.migrations.SQLBriteOpenHelper;
34+
import com.squareup.sqlbrite.BriteDatabase;
35+
import rx.Subscription;
36+
import rx.android.schedulers.AndroidSchedulers;
37+
import rx.schedulers.Schedulers;
38+
39+
public class MainActivity extends AppCompatActivity {
40+
41+
private BriteDatabase database;
42+
private BriteAdapter adapter;
43+
private Subscription subscription;
44+
private ListView listView;
45+
46+
47+
@Override
48+
protected void onCreate(Bundle savedInstanceState) {
49+
super.onCreate(savedInstanceState);
50+
setContentView(R.layout.activity_main);
51+
database = SQLBriteOpenHelper.get(getApplicationContext(), "brite", 1);
52+
adapter = new BriteAdapter(getApplicationContext());
53+
listView = (ListView) findViewById(R.id.list);
54+
listView.setAdapter(adapter);
55+
}
56+
57+
@Override
58+
protected void onResume() {
59+
super.onResume();
60+
subscription = database.createQuery("brites", "SELECT * FROM brites")
61+
.mapToList(Brite.MAPPER)
62+
.subscribeOn(Schedulers.io())
63+
.observeOn(AndroidSchedulers.mainThread())
64+
.subscribe(adapter);
65+
}
66+
67+
@Override
68+
protected void onPause() {
69+
super.onPause();
70+
subscription.unsubscribe();
71+
}
72+
}
73+
```
74+
2875
## Migrations
2976
All migrations must be named after database version. So if you start a new project your first migration will be `1.yaml`.
3077
When upgrading to new version the the migration file must have the name corresponding to the new database version i.e

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111

1212
defaultConfig {
1313
applicationId "com.github.lykmapipo.sqlbrite.migrations.sample"
14-
minSdkVersion 15
14+
minSdkVersion 14
1515
targetSdkVersion 25
1616
versionCode 1
1717
versionName "1.0"

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
package="com.github.lykmapipo.sqlbrite.migrations.sample">
44

55
<application
6-
android:name=".SampleApp"
76
android:allowBackup="true"
87
android:icon="@mipmap/ic_launcher"
98
android:label="@string/app_name"
109
android:supportsRtl="true"
1110
android:theme="@style/AppTheme">
12-
<activity android:name=".ui.MainActivity">
11+
<activity android:name=".MainActivity">
1312
<intent-filter>
1413
<action android:name="android.intent.action.MAIN"/>
1514

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
up:
2+
- CREATE TABLE IF NOT EXISTS brites (_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT)
3+
seeds:
4+
- INSERT INTO brites (name) values("Test Debug 1")
5+
- INSERT INTO brites (name) values("Test Debug 2")
6+
- INSERT INTO brites (name) values("Test Debug 3")
7+
- INSERT INTO brites (name) values("Test Debug 4")
8+
down:
9+
- DROP TABLE brites
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.lykmapipo.sqlbrite.migrations.sample;
2+
3+
import android.database.Cursor;
4+
import rx.functions.Func1;
5+
6+
/**
7+
* Created by lally on 3/18/17.
8+
*/
9+
public class Brite {
10+
11+
static final Func1<Cursor, Brite> MAPPER = new Func1<Cursor, Brite>() {
12+
@Override public Brite call(Cursor cursor) {
13+
return new Brite(
14+
cursor.getLong(cursor.getColumnIndexOrThrow("_id")),
15+
cursor.getString(cursor.getColumnIndexOrThrow("name"))
16+
);
17+
}
18+
};
19+
20+
private Long id;
21+
private String name;
22+
23+
public Brite(String name) {
24+
this.name = name;
25+
}
26+
27+
public Brite(Long id, String name) {
28+
this.id = id;
29+
this.name = name;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public Long getId() {
41+
return id;
42+
}
43+
44+
@Override
45+
public boolean equals(Object o) {
46+
if (this == o) return true;
47+
if (o == null || getClass() != o.getClass()) return false;
48+
49+
Brite brite = (Brite) o;
50+
51+
if (id != null ? !id.equals(brite.id) : brite.id != null) return false;
52+
return name != null ? name.equals(brite.name) : brite.name == null;
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
int result = id != null ? id.hashCode() : 0;
58+
result = 31 * result + (name != null ? name.hashCode() : 0);
59+
return result;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "Brite{" +
65+
"id=" + id +
66+
", name='" + name + '\'' +
67+
'}';
68+
}
69+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.github.lykmapipo.sqlbrite.migrations.sample;
2+
3+
import android.content.Context;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.BaseAdapter;
8+
import android.widget.TextView;
9+
import rx.functions.Action1;
10+
11+
import java.util.Collections;
12+
import java.util.List;
13+
14+
/**
15+
* Created by lally on 3/19/17.
16+
*/
17+
public class BriteAdapter extends BaseAdapter implements Action1<List<Brite>>{
18+
private final LayoutInflater inflater;
19+
20+
private List<Brite> items = Collections.emptyList();
21+
22+
public BriteAdapter(Context context) {
23+
this.inflater = LayoutInflater.from(context);
24+
}
25+
26+
@Override
27+
public void call(List<Brite> items) {
28+
this.items = items;
29+
notifyDataSetChanged();
30+
}
31+
32+
@Override
33+
public int getCount() {
34+
return items.size();
35+
}
36+
37+
@Override
38+
public Brite getItem(int position) {
39+
return items.get(position);
40+
}
41+
42+
@Override
43+
public long getItemId(int position) {
44+
return getItem(position).getId();
45+
}
46+
47+
@Override
48+
public boolean hasStableIds() {
49+
return true;
50+
}
51+
52+
@Override
53+
public View getView(int position, View convertView, ViewGroup parent) {
54+
if (convertView == null) {
55+
convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
56+
}
57+
58+
Brite item = getItem(position);
59+
TextView convertView1 = (TextView) convertView;
60+
convertView1.setText(item.getName() + " (" + position + ")");
61+
convertView1.setTextColor(R.color.colorBlack);
62+
63+
return convertView;
64+
}
65+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.lykmapipo.sqlbrite.migrations.sample;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.widget.ListView;
6+
import com.github.lykmapipo.sqlbrite.migrations.SQLBriteOpenHelper;
7+
import com.squareup.sqlbrite.BriteDatabase;
8+
import rx.Subscription;
9+
import rx.android.schedulers.AndroidSchedulers;
10+
import rx.schedulers.Schedulers;
11+
12+
public class MainActivity extends AppCompatActivity {
13+
14+
private BriteDatabase database;
15+
private BriteAdapter adapter;
16+
private Subscription subscription;
17+
private ListView listView;
18+
19+
20+
@Override
21+
protected void onCreate(Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
setContentView(R.layout.activity_main);
24+
database = SQLBriteOpenHelper.get(getApplicationContext(), "brite", 1);
25+
adapter = new BriteAdapter(getApplicationContext());
26+
listView = (ListView) findViewById(R.id.list);
27+
listView.setAdapter(adapter);
28+
}
29+
30+
@Override
31+
protected void onResume() {
32+
super.onResume();
33+
subscription = database.createQuery("brites", "SELECT * FROM brites")
34+
.mapToList(Brite.MAPPER)
35+
.subscribeOn(Schedulers.io())
36+
.observeOn(AndroidSchedulers.mainThread())
37+
.subscribe(adapter);
38+
}
39+
40+
@Override
41+
protected void onPause() {
42+
super.onPause();
43+
subscription.unsubscribe();
44+
}
45+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
android:paddingRight="@dimen/activity_horizontal_margin"
99
android:paddingTop="@dimen/activity_vertical_margin">
1010

11-
<TextView
12-
android:layout_width="wrap_content"
13-
android:layout_height="wrap_content"
14-
android:text="Hello World!" android:id="@+id/textView"/>
15-
<Button
16-
android:text="Sync Device"
17-
android:layout_width="wrap_content"
18-
android:layout_height="wrap_content" android:layout_below="@+id/textView"
19-
android:layout_centerHorizontal="true" android:layout_marginTop="187dp" android:id="@+id/sync"/>
11+
<ListView
12+
android:id="@+id/list"
13+
android:layout_width="match_parent"
14+
android:layout_height="match_parent"
15+
/>
2016
</RelativeLayout>

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<color name="colorPrimary">#3F51B5</color>
44
<color name="colorPrimaryDark">#303F9F</color>
55
<color name="colorAccent">#FF4081</color>
6+
<color name="colorBlack">#000000</color>
67
</resources>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_name">Push</string>
2+
<string name="app_name">SQLBrite - Migrations</string>
33
</resources>

0 commit comments

Comments
 (0)