Skip to content

Instantly share code, notes, and snippets.

View tobioyelekan's full-sized avatar

Tobiloba Oyelekan tobioyelekan

  • ZoneTechPark
  • Lagos, Nigeria
View GitHub Profile
class A(private val b: B) {
b.doSomething()
}
class B {
fun doSomething
}
class A {
val b = B()
b.doSomething()
}
class B {
fun doSomething
}
class ApplicationClass : Application() {
override fun onCreate() {
super.onCreate()
val constraint = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val workManager = WorkManager.getInstance(applicationContext)
@Database(
entities = [User::class],
version = 1,
exportSchema = false
)
abstract class DatabaseClass : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
private var instance: DatabaseClass? = null
@Dao
interface UserDao {
@Query("SELECT * FROM user")
fun getUsers(): LiveData<List<User>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun saveUsers(user: User)
@Query("DELETE FROM user")
fun deleteAll()
@tobioyelekan
tobioyelekan / User.kt
Created May 24, 2020 15:15
User Entity Class
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "user")
data class User(
@PrimaryKey
val id: String,
val firstName: String,
val lastName: String
)
//ActivityViewModel.kt
private val _textToShow = MutableLiveData<String>()
val textToShow : LiveData<String>()
get() = textToShow
fun buttonClicked(){
_textToShow.value = "Hello"
}
class CourseAdapter(private val viewModel: CourseViewModel) :
ListAdapter<Course, ViewHolder>(CourseDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(inflater.inflate(R.layout.course_item, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(viewModel, getItem(position))
}
@tobioyelekan
tobioyelekan / userViewModel.kt
Last active July 3, 2020 14:47
typical user view model
private val userId = MutableLiveData<String>()
val userDetails = userId.switchMap {
userRepo.getUser(it)
}
val userFullName = userDetails.map {
getFullName(it.firstName, it.lastName)
}