-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModels.kt
More file actions
85 lines (71 loc) · 2.09 KB
/
Copy pathModels.kt
File metadata and controls
85 lines (71 loc) · 2.09 KB
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
package com.example.newsproject.data
import java.time.Instant
import java.util.Date
//API Models
data class ApiCategoryList(
val code: Int = -1,
val message: String = "",
val list: List<Category> = emptyList()
)
data class ApiNewsList(
val code: Int = -1,
val message: String = "",
val list: MutableList<News> = mutableListOf()
)
data class ApiNews(
val code: Int = -1,
val message: String = "",
val news: News = News()
)
//Cached Data
data class NewsCache(
val newsPageList: MutableList<NewsPage> = mutableListOf(),
var categoryList: List<Category> = listOf()
) {
fun containsNewsPage(categoryId: Long, page: Int): Boolean {
for (item in newsPageList)
if (item.categoryId == categoryId && item.page == page) return true
return false
}
fun addNewsPage(categoryId: Long, page: Int, list: MutableList<News>) {
newsPageList.add(NewsPage(categoryId, page, list))
}
fun getNewsList(categoryId: Long, page: Int): List<News> {
for (item in newsPageList)
if (item.categoryId == categoryId && item.page == page) return item.newsList
return listOf()
}
fun getNews(newsId: Long): News {
for (page in newsPageList)
for (news in page.newsList)
if (news.id == newsId) return news
return News()
}
fun setNews(news: News, state: Boolean = false) {
for (page in newsPageList)
for (i in page.newsList.indices)
if (page.newsList[i].id == news.id) {
news.state = state
page.newsList[i] = news
return
}
}
}
data class NewsPage(
val categoryId: Long = -1,
val page: Int = -1,
val newsList: MutableList<News> = mutableListOf()
)
//Data Model
data class Category(
val id: Long = -1,
val name: String = ""
)
data class News(
val id: Long = -1,
val title: String = "",
val date: Date = Date.from(Instant.now()),
val shortDescription: String = "",
val fullDescription: String = "",
var state: Boolean = false
)