Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Item.kt « model « database « ocreader « schaal « email « java « main « src « app - github.com/schaal/ocreader.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5842712ad09d0045bb6872a6e8de1ffc2e26557 (plain)
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
130
/*
 * 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.content.Intent
import android.net.Uri
import android.os.Parcelable
import io.realm.Realm
import io.realm.RealmModel
import io.realm.RealmObject
import io.realm.Sort
import io.realm.annotations.PrimaryKey
import io.realm.annotations.RealmClass
import io.realm.annotations.RealmField
import io.realm.kotlin.isManaged
import io.realm.kotlin.where
import kotlinx.parcelize.Parcelize
import java.util.*

@Parcelize
@RealmClass
open class Item(
        @PrimaryKey var id: Long = 0,
        var guid: String? = null,
        var guidHash: String? = null,
        var url: String? = null,
        var title: String? = null,
        var author: String? = null,
        var pubDate: Date? = null,
        var body: String = "",
        var enclosureMime: String? = null,
        var enclosureLink: String? = null,
        var feed: Feed? = null,
        var feedId: Long = 0,
        @RealmField(name = "unread")
        private var actualUnread: Boolean = true,
        var unreadChanged: Boolean = false,
        @RealmField(name = "starred")
        private var actualStarred: Boolean = false,
        var starredChanged: Boolean = false,
        var lastModified: Date? = null,
        var fingerprint: String? = null,
        var contentHash: String? = null,
        var active: Boolean = true
) : RealmModel, Insertable, Parcelable {
    companion object {
        const val UNREAD = "actualUnread"
        const val STARRED = "actualStarred"

        fun removeExcessItems(realm: Realm, maxItems: Int) {
            val itemCount = realm.where<Item>().count()
            if (itemCount > maxItems) {
                realm.where<Item>()
                        .equalTo(UNREAD, false)
                        .equalTo(STARRED, false)
                        .equalTo(Item::active.name, false)
                        .sort(Item::lastModified.name, Sort.ASCENDING)
                        .limit(itemCount - maxItems)
                        .findAll()
                        .deleteAllFromRealm()
            }
        }

    }

    var unread: Boolean
        get() { return actualUnread }
        set(value) {
            if(isManaged() && actualUnread != value) {
                unreadChanged = !unreadChanged
                feed?.incrementUnreadCount(if(value) 1 else -1)
            }
            actualUnread = value
        }

    var starred: Boolean
        get() { return actualStarred }
        set(value) {
            if(isManaged() && actualStarred != value) {
                starredChanged = !starredChanged
                feed?.incrementStarredCount(if(value) 1 else -1)
            }
            actualStarred = value
        }

    override fun insert(realm: Realm) {
        if(title == null) {
            val fullItem = realm.where<Item>().equalTo(Item::contentHash.name, contentHash).findFirst()
            fullItem?.unread = unread
            fullItem?.starred = starred
        } else {
            feed = Feed.getOrCreate(realm, feedId)
        }
        realm.insertOrUpdate(this)
    }

    override fun delete(realm: Realm) {
        RealmObject.deleteFromRealm(this)
    }

    fun isPlayable(): Boolean {
        return enclosureMime?.run { startsWith("video/") || startsWith("audio/") } ?: false
    }

    fun play(context: Context) {
        if(enclosureLink != null) {
            val playIntent = Intent(Intent.ACTION_VIEW)
            playIntent.data = Uri.parse(enclosureLink)
            context.startActivity(playIntent)
        }
    }
}