Skip to content

Instantly share code, notes, and snippets.

@5ALEKSEY
Created April 11, 2020 16:57
Show Gist options
  • Save 5ALEKSEY/307609278a57a2faffaa63b1fea0b55b to your computer and use it in GitHub Desktop.
Save 5ALEKSEY/307609278a57a2faffaa63b1fea0b55b to your computer and use it in GitHub Desktop.
interface IPasswordsRepository {
fun getAllPasswords(): Flowable<List<PasswordRepoEntity>>
fun getPasswordById(passwordId: Long): Single<PasswordRepoEntity>
fun deletePasswordsByIds(passwordIds: List<Long>): Single<Boolean>
fun addNewPasswords(passwordRepoEntities: List<PasswordRepoEntity>): Single<Boolean>
fun updatePasswords(passwordRepoEntities: List<PasswordRepoEntity>): Single<Boolean>
}
@Entity(tableName = PasswordDBEntity.PASSWORD_TABLE_NAME)
data class PasswordDBEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = COLUMN_PASSWORD_ID)
var passwordIdValue: Long?,
@ColumnInfo(name = COLUMN_PASSWORD_NAME)
var passwordNameValue: String,
@ColumnInfo(name = COLUMN_PASSWORD_AVATAR_PATH)
var passwordAvatarPathValue: String,
@ColumnInfo(name = COLUMN_PASSWORD_CONTENT)
var passwordContentValue: String
) : PasswordRepoEntity {
@Ignore
constructor(passwordId: Long) : this(passwordId, "", "", "")
@Ignore
constructor(passwordName: String, passwordContent: String, avatarPath: String?)
: this(null, passwordName, avatarPath ?: "", passwordContent)
companion object {
const val PASSWORD_TABLE_NAME = "Passwords"
// column names
const val COLUMN_PASSWORD_ID = "password_id" // autogenerated primary key
const val COLUMN_PASSWORD_NAME = "password_name" // password alias
const val COLUMN_PASSWORD_AVATAR_PATH = "password_avatar_path" // url of password picture
const val COLUMN_PASSWORD_CONTENT = "password_content" // url of password picture
}
override fun getPasswordId(): Long? = passwordIdValue
override fun getPasswordName(): String = passwordNameValue
override fun getPasswordAvatarPath(): String = passwordAvatarPathValue
override fun getPasswordContent(): String = passwordContentValue
}
interface PasswordRepoEntity {
fun getPasswordId(): Long?
fun getPasswordName(): String
fun getPasswordAvatarPath(): String
fun getPasswordContent(): String
}
@Dao
interface PasswordsDAO {
@Query("SELECT * FROM " + PasswordDBEntity.PASSWORD_TABLE_NAME)
fun getAllPasswords(): Flowable<List<PasswordDBEntity>>
..............
..............
..............
..............
}
class PasswordsRepositoryImpl @Inject constructor(
private val passwordsLocalStore: PSDatabase
) : IPasswordsRepository {
override fun getAllPasswords(): Flowable<List<PasswordRepoEntity>> =
passwordsLocalStore.getPasswordsDao()
.getAllPasswords()
.subscribeOn(Schedulers.io())
...............
...............
...............
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment