Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SQLDelight interaction example (#814)
- Loading branch information
1 parent
2f10dc5
commit c02e1fc
Showing
21 changed files
with
545 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...ample-app/src/main/java/com/pushtorefresh/storio2/sample/sqldelight/CustomersAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.pushtorefresh.storio2.sample.sqldelight; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.support.annotation.NonNull; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import com.pushtorefresh.storio2.sample.R; | ||
import com.pushtorefresh.storio2.sample.sqldelight.entities.Customer; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import butterknife.Bind; | ||
import butterknife.ButterKnife; | ||
|
||
public class CustomersAdapter extends RecyclerView.Adapter<CustomersAdapter.ViewHolder> { | ||
|
||
@NonNull | ||
private final LayoutInflater layoutInflater; | ||
|
||
@NonNull | ||
private List<Customer> customers = Collections.emptyList(); | ||
|
||
public CustomersAdapter(@NonNull LayoutInflater layoutInflater) { | ||
this.layoutInflater = layoutInflater; | ||
} | ||
|
||
public void setCustomers(@NonNull List<Customer> customers) { | ||
this.customers = customers; | ||
notifyDataSetChanged(); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return customers.size(); | ||
} | ||
|
||
@Override | ||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
View itemView = layoutInflater.inflate(R.layout.list_item_customer, parent, false); | ||
return new ViewHolder(itemView); | ||
} | ||
|
||
@SuppressLint("SetTextI18n") | ||
@Override | ||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { | ||
final Customer customer = customers.get(position); | ||
|
||
holder.name.setText(String.format("%s %s", customer.name(), customer.surname())); | ||
holder.city.setText(customer.city()); | ||
} | ||
|
||
static class ViewHolder extends RecyclerView.ViewHolder { | ||
|
||
@Bind(R.id.list_item_customer_name) | ||
TextView name; | ||
|
||
@Bind(R.id.list_item_customer_city) | ||
TextView city; | ||
|
||
public ViewHolder(@NonNull View itemView) { | ||
super(itemView); | ||
ButterKnife.bind(this, itemView); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
storio-sample-app/src/main/java/com/pushtorefresh/storio2/sample/sqldelight/SQLUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.pushtorefresh.storio2.sample.sqldelight; | ||
|
||
import android.content.ContentValues; | ||
import android.database.Cursor; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.pushtorefresh.storio2.sqlite.StorIOSQLite; | ||
import com.pushtorefresh.storio2.sqlite.operations.put.PutResolver; | ||
import com.pushtorefresh.storio2.sqlite.operations.put.PutResult; | ||
import com.pushtorefresh.storio2.sqlite.queries.InsertQuery; | ||
import com.pushtorefresh.storio2.sqlite.queries.RawQuery; | ||
import com.squareup.sqldelight.RowMapper; | ||
import com.squareup.sqldelight.SqlDelightStatement; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.pushtorefresh.storio2.sqlite.operations.put.PutResult.newInsertResult; | ||
|
||
public final class SQLUtils { | ||
|
||
private SQLUtils() { | ||
} | ||
|
||
@NonNull | ||
public static PutResolver<ContentValues> makeSimpleContentValuesInsertPutResolver(@NonNull final String tableName) { | ||
return new PutResolver<ContentValues>() { | ||
@NonNull | ||
final InsertQuery insert = InsertQuery.builder() | ||
.table(tableName) | ||
.build(); | ||
|
||
@NonNull | ||
@Override | ||
public PutResult performPut(@NonNull StorIOSQLite storIOSQLite, @NonNull ContentValues contentValues) { | ||
final long insertedId = storIOSQLite.lowLevel().insert(insert, contentValues); | ||
return newInsertResult(insertedId, tableName); | ||
} | ||
}; | ||
} | ||
|
||
@NonNull | ||
public static RawQuery makeReadQuery(@NonNull SqlDelightStatement statement) { | ||
return RawQuery.builder() | ||
.query(statement.statement) | ||
.args(statement.args) | ||
.observesTables(statement.tables) | ||
.build(); | ||
} | ||
|
||
@NonNull | ||
public static RawQuery makeWriteQuery(@NonNull SqlDelightStatement statement) { | ||
return RawQuery.builder() | ||
.query(statement.statement) | ||
.args(statement.args) | ||
.affectsTables(statement.tables) | ||
.build(); | ||
} | ||
|
||
@NonNull | ||
public static <T> List<T> mapFromCursor(@NonNull Cursor cursor, @NonNull RowMapper<? extends T> mapper) { | ||
try { | ||
final List<T> result = new ArrayList<T>(cursor.getCount()); | ||
while (cursor.moveToNext()) { | ||
result.add(mapper.map(cursor)); | ||
} | ||
return result; | ||
} finally { | ||
cursor.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.