Skip to content

Instantly share code, notes, and snippets.

View AkshayChordiya's full-sized avatar
🔱

Akshay Chordiya AkshayChordiya

🔱
View GitHub Profile
@AkshayChordiya
AkshayChordiya / UserDao.kt
Created October 22, 2017 11:39
User DAO - Rx - Room
@Query("SELECT * FROM users")
fun getUsers(): Flowable<List<User>>
@AkshayChordiya
AkshayChordiya / UserDao.kt
Created October 22, 2017 11:33
User DAO - LiveData - Room
@Query("SELECT * FROM users")
fun getUsers(): LiveData<List<User>>
@AkshayChordiya
AkshayChordiya / UserDao.kt
Created October 22, 2017 10:19
User DAO - Room
@Dao
interface UserDao {
@Insert
fun insertAll(users: List<User>)
@Update
fun update(user: User)
@Delete
@AkshayChordiya
AkshayChordiya / User.kt
Created October 22, 2017 09:57
User entity with ignore
@Entity(tableName = "users")
data class User(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
var name: String = "",
@Ignore
var secretId: String = ""
)
@AkshayChordiya
AkshayChordiya / User.kt
Created October 22, 2017 09:53
User entity with column info
@Entity(tableName = "users")
data class User(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
@ColumnInfo(name = "full_name")
var name: String = ""
)
@AkshayChordiya
AkshayChordiya / User.kt
Created October 22, 2017 09:38
User entity model with composite primary key
@Entity(tableName = "users", primaryKeys = arrayOf("id", "name"))
data class User(
var id: Int = 0,
var name: String = ""
)
@AkshayChordiya
AkshayChordiya / User.kt
Created October 22, 2017 09:28
User entity model
@Entity(tableName = "users")
data class User(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
var name: String = ""
)
@AkshayChordiya
AkshayChordiya / FileLiveData.kt
Last active November 16, 2023 23:42
FileLiveData to observe changes to file
class FileLiveData(private val context: Context) : LiveData<List<String>>() {
private val fileObserver: FileObserver
init {
val path = File(context.filesDir, "users.txt").path
fileObserver = object : FileObserver(path) {
override fun onEvent(event: Int, path: String?) {
// The file has changed, so let’s reload the data
loadData()
}
@AkshayChordiya
AkshayChordiya / UserViewModel.kt
Last active August 21, 2017 12:52
UserViewModel with LiveData map transformation
class UserViewModel() : ViewModel() {
/**
* The user
*/
private var user: LiveData<User>
/**
* Full name of the user
*/
@AkshayChordiya
AkshayChordiya / DetailUserActivity.kt
Last active August 17, 2017 09:53
DetailUserActivity with ViewModel - LiveData and transformations
class DetailUserActivity : LifecycleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
// Get the ViewModel instance
val userViewModel = ViewModelProviders.of(this).get(UserViewModel::class.java)
userViewModel.setUserId(getSelectedUserId())
userViewModel.getUser().observe(this, Observer {