Skip to content

Instantly share code, notes, and snippets.

Avatar
🌞
Free

Hieu Vu hieuwu

🌞
Free
View GitHub Profile
@hieuwu
hieuwu / WeatherItemAdapter.kt
Last active Jun 12, 2022
Multiple view types in RecyclerView - Naive approach
View WeatherItemAdapter.kt
class WeatherItemAdapter :
ListAdapter<Weather, WeatherItemAdapter.WeatherItemViewHolder>(DiffCallback) {
// Other implementation for DiffCallback
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeatherItemViewHolder {
when (viewType) {
WeatherViewType.DAY.ordinal -> {
val view = LayoutInflater.from(parent.context).inflate(R.layout.weather_day_item, parent, false)
return WeatherDayItemViewHolder(view)
View OrderDao.kt
@Dao
interface OrderDao {
//Get an order along with its line items by orderId
@Transaction
@Query("SELECT * FROM orders WHERE orderId = :id")
fun getById(id: String): Flow<OrderWithLineItems>
//Get an order by its status
@Transaction
@Query("SELECT * FROM orders WHERE status = :status LIMIT 1 ")
@hieuwu
hieuwu / LineItemDao.kt
Last active Mar 21, 2022
Line Item dao
View LineItemDao.kt
@Dao
interface LineItemDao {
//Get all line item in along with its product
@Transaction
@Query("SELECT * FROM line_items ")
fun getAll(): Flow<List<LineItemAndProduct>>
//Get line item in an order by orderId
@Transaction
@Query("SELECT * FROM line_items WHERE orderId = :orderId")
@hieuwu
hieuwu / OrderWithLineItem.kt
Created Mar 21, 2022
Order and Line Item Many to one relationship
View OrderWithLineItem.kt
data class OrderWithLineItems(
@Embedded var order: Order,
@Relation(
parentColumn = "orderId",
entityColumn = "orderId",
entity = LineItem::class
)
val lineItemList: MutableList<LineItemAndProduct>
)
@hieuwu
hieuwu / LineItemAndProduct.kt
Created Mar 21, 2022
Line Item and Product One to one relationship
View LineItemAndProduct.kt
data class LineItemAndProduct(
@Embedded val lineItem: LineItem?,
@Relation(
parentColumn = "productId",
entityColumn = "productId",
entity = Product::class
)
val product: Product?
)
@hieuwu
hieuwu / order.kt
Created Mar 21, 2022
Order Entity
View order.kt
@Entity(tableName = "orders")
data class Order(
@PrimaryKey
@ColumnInfo(name = "orderId")
val id: String,
@ColumnInfo(name = "status")
var status: String,
@ColumnInfo(name = "address")
@hieuwu
hieuwu / LineItem.kt
Last active Mar 21, 2022
Line item entity
View LineItem.kt
@Entity(tableName = "line_items")
data class LineItem(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "lineItemId")
val id: Long,
@ColumnInfo(name = "productId")
val productId: String,
@ColumnInfo(name = "orderId")
@hieuwu
hieuwu / product.kt
Last active Mar 21, 2022
Product Entity
View product.kt
@Entity(tableName = "products")
data class Product(
@PrimaryKey
@NonNull
@ColumnInfo(name = "productId")
val id: String,
@ColumnInfo(name = "name")
var name: String?,
@hieuwu
hieuwu / FluentBindingAdapterMvxRecyclerView.cs
Last active Jan 24, 2022
Customized fluent binding with adapter, view holder for MvxRecyclerView
View FluentBindingAdapterMvxRecyclerView.cs
public class PersonItemAdapter : MvxRecyclerAdapter
{
public PersonItemAdapter(IMvxAndroidBindingContext bindingContext)
: base(bindingContext)
{
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, BindingContext.LayoutInflaterHolder);
View CountTripletsSolution.kt
// Complete the countTriplets function below.
fun countTriplets(arr: Array<Long>, r: Long): Long {
var befMap = HashMap<Long, Int>()
var aftMap = HashMap<Long, Int>()
for(num in arr) {
if(aftMap.containsKey(num)) aftMap[num] = aftMap[num]!! + 1
else aftMap[num] = 1
}