-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement database for feed persistence
- Loading branch information
Showing
4 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
app/src/main/java/cc/sovellus/vrcaa/helper/DatabaseHelper.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cc.sovellus.vrcaa.helper | ||
|
||
import android.database.sqlite.SQLiteDatabase | ||
import android.database.sqlite.SQLiteOpenHelper | ||
import cc.sovellus.vrcaa.App | ||
|
||
class DatabaseHelper : SQLiteOpenHelper(App.getContext(), | ||
Constants.DATABASE_NAME, null, | ||
Constants.DATABASE_VERSION | ||
) { | ||
override fun onCreate(database: SQLiteDatabase) { | ||
database.execSQL(Queries.SQL_CREATE_FEED_TABLE) | ||
} | ||
|
||
override fun onUpgrade(database: SQLiteDatabase, oldVersion: Int, newVersion: Int) { | ||
onUpgrade(database, oldVersion, newVersion) | ||
} | ||
|
||
override fun onDowngrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) { | ||
onDowngrade(db, oldVersion, newVersion) | ||
} | ||
|
||
object Constants { | ||
const val DATABASE_NAME = "vrcaa.db" | ||
const val DATABASE_VERSION = 1 | ||
} | ||
|
||
object Queries { | ||
const val SQL_CREATE_FEED_TABLE = "CREATE TABLE feed(type INTEGER, feedId TEXT, friendId TEXT, friendName TEXT, friendPictureUrl TEXT, friendStatus TEXT, travelDestination TEXT, feedTimestamp BIGINT)" | ||
} | ||
|
||
object Tables { | ||
const val SQL_TABLE_FEED = "feed" | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
app/src/main/java/cc/sovellus/vrcaa/manager/DatabaseManager.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cc.sovellus.vrcaa.manager | ||
|
||
import android.content.ContentValues | ||
import cc.sovellus.vrcaa.helper.DatabaseHelper | ||
import cc.sovellus.vrcaa.helper.StatusHelper | ||
import java.time.LocalDateTime | ||
import java.time.ZoneOffset | ||
import java.util.UUID | ||
|
||
object DatabaseManager { | ||
private val dbHelper = DatabaseHelper() | ||
|
||
fun writeFeed(feed: FeedManager.Feed) { | ||
val values = ContentValues().apply { | ||
put("type", feed.type.ordinal) | ||
put("feedId", feed.feedId.toString()) | ||
put("friendId", feed.friendId) | ||
put("friendName", feed.friendName) | ||
put("friendPictureUrl", feed.friendPictureUrl) | ||
put("friendStatus", feed.friendStatus.ordinal) | ||
put("travelDestination", feed.travelDestination) | ||
put("feedTimestamp", feed.feedTimestamp.toEpochSecond(ZoneOffset.UTC)) | ||
} | ||
|
||
dbHelper.writableDatabase.insert(DatabaseHelper.Tables.SQL_TABLE_FEED, null, values) | ||
} | ||
|
||
fun readFeeds(): MutableList<FeedManager.Feed> { | ||
val cursor = dbHelper.readableDatabase.query( | ||
DatabaseHelper.Tables.SQL_TABLE_FEED, | ||
arrayOf("type", "feedId", "friendId", "friendName", "friendPictureUrl", "friendStatus", "travelDestination", "feedTimestamp"), | ||
null, | ||
null, | ||
null, | ||
null, | ||
null | ||
) | ||
|
||
val feeds = mutableListOf<FeedManager.Feed>() | ||
|
||
with(cursor) { | ||
while (moveToNext()) { | ||
val feed = FeedManager.Feed( | ||
type = FeedManager.FeedType.fromInt(getInt(getColumnIndexOrThrow("type"))), | ||
feedId = UUID.fromString(getString(getColumnIndexOrThrow("feedId"))), | ||
friendId = getString(getColumnIndexOrThrow("friendId")), | ||
friendName = getString(getColumnIndexOrThrow("friendName")), | ||
friendPictureUrl = getString(getColumnIndexOrThrow("friendPictureUrl")), | ||
friendStatus = StatusHelper.Status.fromInt(getInt(getColumnIndexOrThrow("friendStatus"))), | ||
travelDestination = getString(getColumnIndexOrThrow("travelDestination")), | ||
feedTimestamp = LocalDateTime.ofEpochSecond(getLong(getColumnIndexOrThrow("feedTimestamp")), 0, ZoneOffset.UTC) | ||
) | ||
feeds.add(feed) | ||
} | ||
} | ||
|
||
cursor.close() | ||
return feeds | ||
} | ||
} |
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