1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
* Copyright © 2020. Daniel Schaal <daniel@schaal.email>
*
* This file is part of ocreader.
*
* ocreader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ocreader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
package email.schaal.ocreader.database.model
import android.content.Context
import android.os.Parcelable
import email.schaal.ocreader.R
import io.realm.Realm
import io.realm.RealmModel
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import io.realm.annotations.RealmClass
import io.realm.kotlin.createObject
import io.realm.kotlin.where
import kotlinx.parcelize.Parcelize
import java.util.*
@Parcelize
@RealmClass
open class Feed(
@PrimaryKey var id: Long = 0,
var folderId: Long? = null,
var folder: Folder? = null,
var url: String = "",
var name: String = "",
var link: String? = null,
var faviconLink: String? = null,
var added: Date = Date(),
var unreadCount: Int = 0,
var starredCount: Int = 0,
var ordering: Int = 0,
var pinned: Boolean = false,
var updateErrorCount: Int = 0,
var lastUpdateError: String? = null
) : RealmModel, TreeItem, Insertable, Parcelable {
fun isConsideredFailed(): Boolean {
return updateErrorCount >= 50
}
companion object {
fun get(realm: Realm, feedId: Long) : Feed? {
return realm.where<Feed>().equalTo(Feed::id.name, feedId).findFirst()
}
fun getOrCreate(realm: Realm, feedId: Long) : Feed? {
return get(realm, feedId) ?: realm.createObject(feedId)
}
}
override fun treeItemId(): Long {
return id
}
override fun treeItemName(context: Context): String {
return name
}
override fun getIcon(): Int {
return R.drawable.ic_feed_icon
}
override fun getCount(realm: Realm): Int {
return unreadCount
}
override fun getFeeds(realm: Realm, onlyUnread: Boolean): List<Feed> {
return listOf(this)
}
override fun getItems(realm: Realm, onlyUnread: Boolean): List<Item> {
val query = realm.where<Item>().equalTo(Item::feedId.name, id)
if(onlyUnread)
query.equalTo(Item.UNREAD, true)
return query.findAll()
}
fun incrementUnreadCount(value: Int) {
unreadCount += value
}
fun incrementStarredCount(value: Int) {
starredCount += value
}
override fun insert(realm: Realm) {
folder = folderId?.let { Folder.getOrCreate(realm, it) }
realm.insertOrUpdate(this)
}
override fun delete(realm: Realm) {
realm.where<Item>().equalTo(Item::feedId.name, id).findAll().deleteAllFromRealm()
RealmObject.deleteFromRealm(this)
}
fun getFolderTitle(context: Context?): CharSequence? {
return folder?.name ?: if(folderId == 0L || folderId == null) context?.getString(R.string.root_folder) else null
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Feed) return false
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
}
|