-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
27 changed files
with
1,269 additions
and
373 deletions.
There are no files selected for viewing
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
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
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,14 @@ | ||
package com.skyd.anivu.ext | ||
|
||
import androidx.compose.runtime.mutableStateMapOf | ||
import androidx.compose.runtime.saveable.Saver | ||
import androidx.compose.runtime.snapshots.SnapshotStateMap | ||
import androidx.compose.runtime.toMutableStateMap | ||
|
||
fun <K, V> snapshotStateMapSaver() = Saver<SnapshotStateMap<K, V>, Any>( | ||
save = { state -> state.toList() }, | ||
restore = { value -> | ||
@Suppress("UNCHECKED_CAST") | ||
(value as? List<Pair<K, V>>)?.toMutableStateMap() ?: mutableStateMapOf() | ||
} | ||
) |
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
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,36 @@ | ||
package com.skyd.anivu.model.bean | ||
|
||
import android.os.Parcelable | ||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.skyd.anivu.R | ||
import com.skyd.anivu.appContext | ||
import com.skyd.anivu.base.BaseBean | ||
import kotlinx.parcelize.Parcelize | ||
import kotlinx.serialization.Serializable | ||
|
||
const val GROUP_TABLE_NAME = "Group" | ||
|
||
@Parcelize | ||
@Serializable | ||
@Entity(tableName = GROUP_TABLE_NAME) | ||
data class GroupBean( | ||
@PrimaryKey | ||
@ColumnInfo(name = GROUP_ID_COLUMN) | ||
val groupId: String, | ||
@ColumnInfo(name = NAME_COLUMN) | ||
val name: String, | ||
) : BaseBean, Parcelable { | ||
companion object { | ||
const val DEFAULT_GROUP_ID = "default" | ||
|
||
const val NAME_COLUMN = "name" | ||
const val GROUP_ID_COLUMN = "groupId" | ||
|
||
val defaultGroup = GroupBean( | ||
groupId = DEFAULT_GROUP_ID, | ||
name = appContext.getString(R.string.default_feed_group) | ||
) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/skyd/anivu/model/bean/GroupWithFeedBean.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,17 @@ | ||
package com.skyd.anivu.model.bean | ||
|
||
import androidx.room.Embedded | ||
import androidx.room.Relation | ||
|
||
/** | ||
* A [group] contains many [feeds]. | ||
*/ | ||
data class GroupWithFeedBean( | ||
@Embedded | ||
var group: GroupBean, | ||
@Relation( | ||
parentColumn = GroupBean.GROUP_ID_COLUMN, | ||
entityColumn = FeedBean.GROUP_ID_COLUMN, | ||
) | ||
var feeds: List<FeedBean>, | ||
) |
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
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
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,61 @@ | ||
package com.skyd.anivu.model.db.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
import com.skyd.anivu.appContext | ||
import com.skyd.anivu.model.bean.GROUP_TABLE_NAME | ||
import com.skyd.anivu.model.bean.GroupBean | ||
import com.skyd.anivu.model.bean.GroupWithFeedBean | ||
import dagger.hilt.EntryPoint | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.EntryPointAccessors | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
interface GroupDao { | ||
@EntryPoint | ||
@InstallIn(SingletonComponent::class) | ||
interface GroupDaoEntryPoint { | ||
val feedDao: FeedDao | ||
} | ||
|
||
@Transaction | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun setGroup(groupBean: GroupBean) | ||
|
||
@Transaction | ||
@Delete | ||
suspend fun removeGroup(groupBean: GroupBean): Int | ||
|
||
@Transaction | ||
@Query("DELETE FROM `$GROUP_TABLE_NAME` WHERE ${GroupBean.GROUP_ID_COLUMN} = :groupId") | ||
suspend fun removeGroup(groupId: String): Int | ||
|
||
@Transaction | ||
suspend fun removeGroupWithFeed(groupId: String): Int { | ||
removeGroup(groupId) | ||
return EntryPointAccessors.fromApplication(appContext, GroupDaoEntryPoint::class.java).run { | ||
feedDao.removeFeedByGroupId(groupId) | ||
} | ||
} | ||
|
||
@Transaction | ||
suspend fun moveGroupFeedsTo(fromGroupId: String?, toGroupId: String?): Int { | ||
return EntryPointAccessors.fromApplication(appContext, GroupDaoEntryPoint::class.java).run { | ||
feedDao.moveFeedToGroup(fromGroupId = fromGroupId, toGroupId = toGroupId) | ||
} | ||
} | ||
|
||
@Transaction | ||
@Query("SELECT * FROM `$GROUP_TABLE_NAME`") | ||
fun getGroupWithFeeds(): Flow<List<GroupWithFeedBean>> | ||
|
||
@Transaction | ||
@Query("SELECT DISTINCT ${GroupBean.GROUP_ID_COLUMN} FROM `$GROUP_TABLE_NAME`") | ||
fun getGroupIds(): Flow<List<String>> | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/skyd/anivu/model/db/migration/Migration3To4.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,23 @@ | ||
package com.skyd.anivu.model.db.migration | ||
|
||
import androidx.room.migration.Migration | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.skyd.anivu.model.bean.FEED_TABLE_NAME | ||
import com.skyd.anivu.model.bean.FeedBean | ||
import com.skyd.anivu.model.bean.GROUP_TABLE_NAME | ||
import com.skyd.anivu.model.bean.GroupBean | ||
|
||
class Migration3To4 : Migration(3, 4) { | ||
override fun migrate(db: SupportSQLiteDatabase) { | ||
db.execSQL( | ||
""" | ||
CREATE TABLE `$GROUP_TABLE_NAME` ( | ||
${GroupBean.GROUP_ID_COLUMN} TEXT NOT NULL PRIMARY KEY, | ||
${GroupBean.NAME_COLUMN} TEXT NOT NULL | ||
) | ||
""" | ||
) | ||
db.execSQL("ALTER TABLE $FEED_TABLE_NAME ADD ${FeedBean.GROUP_ID_COLUMN} TEXT") | ||
db.execSQL("ALTER TABLE $FEED_TABLE_NAME ADD ${FeedBean.NICKNAME_COLUMN} TEXT") | ||
} | ||
} |
Oops, something went wrong.