Last active
January 11, 2018 02:41
-
-
Save guodong1111/c71034073754f83deaaeb1b8cbf6ed3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.guodong.remotecontrol.database | |
import android.arch.persistence.room.Database | |
import android.arch.persistence.room.RoomDatabase | |
import tw.guodong.remotecontrol.pojo.DeviceInfo | |
import tw.guodong.remotecontrol.pojo.dao.DeviceInfoDao | |
/** | |
* Created by tony on 2018/1/11. | |
*/ | |
@Database(entities = arrayOf(DeviceInfo::class), version = 1) | |
abstract class DeviceDatabase : RoomDatabase() { | |
abstract fun deviceDao(): DeviceInfoDao | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.guodong.remotecontrol.pojo | |
import android.arch.persistence.room.Entity | |
import android.arch.persistence.room.PrimaryKey | |
import android.arch.persistence.room.ColumnInfo | |
import android.text.TextUtils | |
/** | |
* Created by tony on 2018/1/10. | |
*/ | |
@Entity | |
data class DeviceInfo( | |
@PrimaryKey() | |
val id: String, | |
@ColumnInfo(name = "name") | |
var name: String = id, | |
@ColumnInfo(name = "push_token") | |
var pushToken: String = "") { | |
init { | |
if(TextUtils.isEmpty(name)) name = id | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tw.guodong.remotecontrol.pojo.dao | |
import android.arch.persistence.room.Dao | |
import android.arch.persistence.room.Insert | |
import android.arch.persistence.room.Query | |
import tw.guodong.remotecontrol.pojo.DeviceInfo | |
/** | |
* Created by tony on 2018/1/11. | |
*/ | |
@Dao | |
interface DeviceInfoDao { | |
@Query("IF EXISTS(SELECT * FROM device WHERE id = :deviceId)") | |
fun isDeviceExist(deviceId: String): Boolean | |
@Query("SELECT * FROM device WHERE id = :deviceId LIMIT 1") | |
fun getDeviceInfo(deviceId: String): DeviceInfo | |
@Insert | |
fun insertDeviceInfo(deviceInfo: DeviceInfo) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val deviceDatabase: DeviceDatabase = Room.databaseBuilder(MyApplication.maperApplicationContext, DeviceDatabase::class.java, "device").build() | |
val deviceDao = deviceDatabase.deviceDao() | |
val deviceId = getDeviceId(MyApplication.maperApplicationContext) | |
val isDeviceExist = deviceDao.isDeviceExist(deviceId) | |
val deviceInfo = if(isDeviceExist) { | |
deviceDao.getDeviceInfo(deviceId) | |
} else { | |
val tmpDeviceInfo = DeviceInfo(getDeviceId(MyApplication.maperApplicationContext)) | |
deviceDao.insertDeviceInfo(tmpDeviceInfo) | |
tmpDeviceInfo | |
} | |
Log.v("guodong", "deviceInfo: $deviceInfo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment