mirror of
https://github.com/afollestad/nock-nock.git
synced 2025-04-20 03:25:14 +00:00
Groundwork for tags
This commit is contained in:
parent
67aa54ac22
commit
2756fc9fc7
10 changed files with 77 additions and 23 deletions
|
@ -66,6 +66,9 @@ dependencies {
|
|||
// afollestad
|
||||
implementation 'com.afollestad.material-dialogs:core:' + versions.materialDialogs
|
||||
|
||||
// Misc
|
||||
implementation 'com.github.okdroid:checkablechipview:' + versions.chipView
|
||||
|
||||
// Debugging
|
||||
implementation 'com.jakewharton.timber:timber:' + versions.timber
|
||||
implementation("com.crashlytics.sdk.android:crashlytics:${versions.fabric}") {
|
||||
|
|
|
@ -23,6 +23,7 @@ import android.content.Context.NOTIFICATION_SERVICE
|
|||
import androidx.room.Room.databaseBuilder
|
||||
import com.afollestad.nocknock.data.AppDatabase
|
||||
import com.afollestad.nocknock.data.Database1to2Migration
|
||||
import com.afollestad.nocknock.data.Database2to3Migration
|
||||
import com.afollestad.nocknock.notifications.Qualifiers.MAIN_ACTIVITY_CLASS
|
||||
import com.afollestad.nocknock.ui.main.MainActivity
|
||||
import com.afollestad.nocknock.utilities.ext.systemService
|
||||
|
@ -38,7 +39,10 @@ val mainModule = module {
|
|||
|
||||
single {
|
||||
databaseBuilder(get(), AppDatabase::class.java, "NockNock.db")
|
||||
.addMigrations(Database1to2Migration())
|
||||
.addMigrations(
|
||||
Database1to2Migration(),
|
||||
Database2to3Migration()
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
|
|
|
@ -83,6 +83,7 @@ fun fakeModel(id: Long) = Site(
|
|||
id = id,
|
||||
name = "Test",
|
||||
url = "https://test.com",
|
||||
tags = "",
|
||||
settings = fakeSettingsModel(id),
|
||||
lastResult = fakeResultModel(id),
|
||||
retryPolicy = fakeRetryPolicy(id)
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
<application>
|
||||
<uses-library
|
||||
android:name="android.test.runner"
|
||||
android:name="androidx.test.runner"
|
||||
android:required="false"/>
|
||||
</application>
|
||||
</manifest>
|
||||
|
|
|
@ -59,9 +59,7 @@ class AppDatabaseTest() {
|
|||
|
||||
@After
|
||||
@Throws(IOException::class)
|
||||
fun destroy() {
|
||||
db.close()
|
||||
}
|
||||
fun destroy() = db.close()
|
||||
|
||||
// SiteDao
|
||||
|
||||
|
@ -69,6 +67,7 @@ class AppDatabaseTest() {
|
|||
val model1 = Site(
|
||||
name = "Test 1",
|
||||
url = "https://test1.com",
|
||||
tags = "",
|
||||
settings = null,
|
||||
lastResult = null,
|
||||
retryPolicy = null
|
||||
|
@ -79,6 +78,7 @@ class AppDatabaseTest() {
|
|||
val model2 = Site(
|
||||
name = "Test 2",
|
||||
url = "https://test2.com",
|
||||
tags = "",
|
||||
settings = null,
|
||||
lastResult = null,
|
||||
retryPolicy = null
|
||||
|
@ -96,6 +96,7 @@ class AppDatabaseTest() {
|
|||
val model = Site(
|
||||
name = "Test",
|
||||
url = "https://test.com",
|
||||
tags = "",
|
||||
settings = null,
|
||||
lastResult = null,
|
||||
retryPolicy = null
|
||||
|
@ -111,6 +112,7 @@ class AppDatabaseTest() {
|
|||
val initialModel = Site(
|
||||
name = "Test 1",
|
||||
url = "https://test1.com",
|
||||
tags = "",
|
||||
settings = null,
|
||||
lastResult = null,
|
||||
retryPolicy = null
|
||||
|
@ -135,6 +137,7 @@ class AppDatabaseTest() {
|
|||
val model1 = Site(
|
||||
name = "Test 1",
|
||||
url = "https://test1.com",
|
||||
tags = "",
|
||||
settings = null,
|
||||
lastResult = null,
|
||||
retryPolicy = null
|
||||
|
@ -145,6 +148,7 @@ class AppDatabaseTest() {
|
|||
val model2 = Site(
|
||||
name = "Test 2",
|
||||
url = "https://test2.com",
|
||||
tags = "",
|
||||
settings = null,
|
||||
lastResult = null,
|
||||
retryPolicy = null
|
||||
|
@ -292,7 +296,7 @@ class AppDatabaseTest() {
|
|||
val newId = retryDao.insert(model)
|
||||
assertThat(newId).isEqualTo(1)
|
||||
|
||||
val finalModel = resultsDao.forSite(newId)
|
||||
val finalModel = retryDao.forSite(newId)
|
||||
.single()
|
||||
assertThat(finalModel).isEqualTo(model.copy(siteId = newId))
|
||||
}
|
||||
|
@ -348,6 +352,25 @@ class AppDatabaseTest() {
|
|||
assertThat(allSites[2]).isEqualTo(MOCK_MODEL_3)
|
||||
}
|
||||
|
||||
@Test fun extension_put_and_allSites_withTag() {
|
||||
val model1 = MOCK_MODEL_1.copy(tags = "one,two,three")
|
||||
val model2 = MOCK_MODEL_2.copy(tags = "four,five,six")
|
||||
val model3 = MOCK_MODEL_3.copy(tags = "seven,eight,nine")
|
||||
|
||||
db.putSite(model1)
|
||||
db.putSite(model2)
|
||||
db.putSite(model3)
|
||||
|
||||
val allSites1 = db.allSites(forTag = "one")
|
||||
assertThat(allSites1.single()).isEqualTo(model1)
|
||||
|
||||
val allSites2 = db.allSites(forTag = "five")
|
||||
assertThat(allSites2.single()).isEqualTo(model2)
|
||||
|
||||
val allSites3 = db.allSites(forTag = "nine")
|
||||
assertThat(allSites3.single()).isEqualTo(model3)
|
||||
}
|
||||
|
||||
@Test fun extension_put_getSite() {
|
||||
db.putSite(MOCK_MODEL_1)
|
||||
db.putSite(MOCK_MODEL_2)
|
||||
|
|
|
@ -62,6 +62,7 @@ fun fakeModel(id: Long) = Site(
|
|||
id = id,
|
||||
name = "Test",
|
||||
url = "https://test.com",
|
||||
tags = "",
|
||||
settings = fakeSettingsModel(id),
|
||||
lastResult = fakeResultModel(id),
|
||||
retryPolicy = fakeRetryPolicy(id)
|
||||
|
|
|
@ -52,21 +52,28 @@ abstract class AppDatabase : RoomDatabase() {
|
|||
*
|
||||
* @author Aidan Follestad (@afollestad)
|
||||
*/
|
||||
fun AppDatabase.allSites(): List<Site> {
|
||||
return siteDao().all()
|
||||
.map {
|
||||
val settings = siteSettingsDao().forSite(it.id)
|
||||
.single()
|
||||
val lastResult = validationResultsDao().forSite(it.id)
|
||||
.singleOrNull()
|
||||
val retryPolicy = retryPolicyDao().forSite(it.id)
|
||||
.singleOrNull()
|
||||
return@map it.copy(
|
||||
settings = settings,
|
||||
lastResult = lastResult,
|
||||
retryPolicy = retryPolicy
|
||||
)
|
||||
}
|
||||
fun AppDatabase.allSites(forTag: String = ""): List<Site> {
|
||||
val lowercaseTag = forTag.toLowerCase()
|
||||
var all = siteDao().all()
|
||||
if (!forTag.isEmpty()) {
|
||||
all = all.filter {
|
||||
forTag.isEmpty() ||
|
||||
it.tags.toLowerCase().split(",").contains(lowercaseTag)
|
||||
}
|
||||
}
|
||||
return all.map {
|
||||
val settings = siteSettingsDao().forSite(it.id)
|
||||
.single()
|
||||
val lastResult = validationResultsDao().forSite(it.id)
|
||||
.singleOrNull()
|
||||
val retryPolicy = retryPolicyDao().forSite(it.id)
|
||||
.singleOrNull()
|
||||
return@map it.copy(
|
||||
settings = settings,
|
||||
lastResult = lastResult,
|
||||
retryPolicy = retryPolicy
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,3 +31,15 @@ class Database1to2Migration : Migration(1, 2) {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates the database from version 2 to 3.
|
||||
*
|
||||
* @author Aidan Follestad (@afollestad)
|
||||
*/
|
||||
class Database2to3Migration : Migration(1, 2) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE `sites` ADD COLUMN tags TEXT NOT NULL")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,8 +31,10 @@ data class Site(
|
|||
@PrimaryKey(autoGenerate = true) var id: Long = 0,
|
||||
/** The site's user-given name. */
|
||||
var name: String,
|
||||
/** The URl at which validation attempts are made to. */
|
||||
/** The URL at which validation attempts are made to. */
|
||||
var url: String,
|
||||
/** Comma separated tags for this site. */
|
||||
var tags: String,
|
||||
/** Settings for the site. */
|
||||
@Ignore var settings: SiteSettings?,
|
||||
/** The last validation attempt result for the site, if any. */
|
||||
|
@ -41,7 +43,7 @@ data class Site(
|
|||
@Ignore var retryPolicy: RetryPolicy?
|
||||
) : CanNotifyModel {
|
||||
|
||||
constructor() : this(0, "", "", null, null, null)
|
||||
constructor() : this(0, "", "", "", null, null, null)
|
||||
|
||||
override fun notifyId(): Int = id.toInt()
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ ext.versions = [
|
|||
// Misc
|
||||
okHttp : '3.12.1',
|
||||
rhino : '1.7.10',
|
||||
chipView : '1.0.3',
|
||||
|
||||
// Kotlin
|
||||
kotlin : '1.3.11',
|
||||
|
|
Loading…
Add table
Reference in a new issue