Skip to content

Instantly share code, notes, and snippets.

Avatar

Tobiloba Oyelekan tobioyelekan

  • ZoneTechPark
  • Lagos, Nigeria
View GitHub Profile
View ClassB.kt
class A(private val b: B) {
b.doSomething()
}
class B {
fun doSomething
}
View ClassA.kt
class A {
val b = B()
b.doSomething()
}
class B {
fun doSomething
}
View ApplicationClass.kt
class ApplicationClass : Application() {
override fun onCreate() {
super.onCreate()
val constraint = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val workManager = WorkManager.getInstance(applicationContext)
View DatabaseClass.kt
@Database(
entities = [User::class],
version = 1,
exportSchema = false
)
abstract class DatabaseClass : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
private var instance: DatabaseClass? = null
View UserDao.kt
@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
User Entity Class
View User.kt
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "user")
data class User(
@PrimaryKey
val id: String,
val firstName: String,
val lastName: String
)
View layout.kt
//ActivityViewModel.kt
private val _textToShow = MutableLiveData<String>()
val textToShow : LiveData<String>()
get() = textToShow
fun buttonClicked(){
_textToShow.value = "Hello"
}
View AdapterClass.kt
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 Jul 3, 2020
typical user view model
View userViewModel.kt
private val userId = MutableLiveData<String>()
val userDetails = userId.switchMap {
userRepo.getUser(it)
}
val userFullName = userDetails.map {
getFullName(it.firstName, it.lastName)
}