Skip to content

Commit

Permalink
Merge pull request #51 from alexch33/bookmarks
Browse files Browse the repository at this point in the history
bookmarks feature added, version 0.5.0
  • Loading branch information
alexch33 authored Jan 26, 2025
2 parents e91b115 + c1c0e8f commit f0ac8b8
Show file tree
Hide file tree
Showing 31 changed files with 756 additions and 169 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ android {
applicationId "com.myAllVideoBrowser"
minSdkVersion build_versions.min_sdk
targetSdkVersion build_versions.target_sdk
versionCode 23
versionName "0.4.41"
versionCode 24
versionName "0.5.0"
testInstrumentationRunner "util.TestRunner"

ndk {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 4,
"identityHash": "c280710f27f325be3d92e90ee8e49e0d",
"identityHash": "4581fbe39c79237b1f8fdd6697a0eae6",
"entities": [
{
"tableName": "PageInfo",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `isSystem` INTEGER NOT NULL, `name` TEXT NOT NULL, `link` TEXT NOT NULL, `icon` TEXT NOT NULL, `favicon` BLOB, PRIMARY KEY(`link`))",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `isSystem` INTEGER NOT NULL, `name` TEXT NOT NULL, `link` TEXT NOT NULL, `icon` TEXT NOT NULL, `favicon` BLOB, `order` INTEGER NOT NULL, PRIMARY KEY(`link`))",
"fields": [
{
"fieldPath": "id",
Expand Down Expand Up @@ -43,6 +43,12 @@
"columnName": "favicon",
"affinity": "BLOB",
"notNull": false
},
{
"fieldPath": "order",
"columnName": "order",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
Expand Down Expand Up @@ -334,7 +340,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'c280710f27f325be3d92e90ee8e49e0d')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4581fbe39c79237b1f8fdd6697a0eae6')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,65 @@ import android.net.Uri
import com.myAllVideoBrowser.data.local.room.dao.PageDao
import com.myAllVideoBrowser.data.local.room.entity.PageInfo
import com.myAllVideoBrowser.data.repository.TopPagesRepository
import com.myAllVideoBrowser.util.SharedPrefHelper
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TopPagesLocalDataSource @Inject constructor(
private val pageDao: PageDao
private val pageDao: PageDao,
private val sharedPrefHelper: SharedPrefHelper
) : TopPagesRepository {

override suspend fun getTopPages(): List<PageInfo> {
val list1 = arrayListOf<PageInfo>()
val imdb = Uri.parse("https://www.imdb.com")
list1.add(PageInfo(link = imdb.toString()))
list1.add(PageInfo(link = "https://www.tiktok.com"))
list1.add(PageInfo(link = "https://www.dailymotion.com"))
list1.add(PageInfo(link = "https://www.facebook.com/watch"))
list1.add(PageInfo(link = "https://www.instagram.com"))
list1.add(PageInfo(link = "https://www.twitter.com"))
list1.add(PageInfo(link = "https://www.pinterest.com/videos"))
list1.add(PageInfo(link = "https://www.twitch.tv"))

for (page in list1) {
page.name = Uri.parse(page.link).host.toString()
}
val list2 = pageDao.getPageInfos().blockingFirst()

val result = arrayListOf<PageInfo>()
for (item in list1) {
val daoItem = list2.firstOrNull { it.link == item.link }
if (daoItem != null) {
result.add(daoItem)
} else {
result.add(item)
val localBookmarks = pageDao.getPageInfos().blockingFirst(emptyList())
if (localBookmarks.isEmpty()) {
val isFirstStart = sharedPrefHelper.getIsFirstStart()
if (isFirstStart) {
val defaultList = getDefaultBookmarks()
pageDao.insertAllProgressInfo(defaultList)

return defaultList
}
}

return result
return localBookmarks
}

override fun saveTopPage(pageInfo: PageInfo) {
pageDao.insertProgressInfo(pageInfo)
}

override fun replaceBookmarksWith(pageInfos: List<PageInfo>) {
pageDao.deleteAll()
pageDao.insertAllProgressInfo(pageInfos)
}

override fun deletePageInfo(pageInfo: PageInfo) {
pageDao.deleteProgressInfo(pageInfo)
}

override suspend fun updateLocalStorage() {
override suspend fun updateLocalStorageFavicons(): Flow<PageInfo> {
throw NotImplementedError("NO NEED, HANDLED BY REPO")
}

private fun getDefaultBookmarks(): List<PageInfo> {
val defaultList = arrayListOf<PageInfo>()

defaultList.add(PageInfo(link = "https://www.imdb.com"))
defaultList.add(PageInfo(link = "https://www.tiktok.com"))
defaultList.add(PageInfo(link = "https://www.dailymotion.com"))
defaultList.add(PageInfo(link = "https://www.facebook.com/watch"))
defaultList.add(PageInfo(link = "https://www.instagram.com"))
defaultList.add(PageInfo(link = "https://www.twitter.com"))
defaultList.add(PageInfo(link = "https://www.pinterest.com/videos"))
defaultList.add(PageInfo(link = "https://www.twitch.tv"))

return defaultList.mapIndexed { index, page ->
page.name = Uri.parse(page.link).host.toString()
page.order = index
page
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.room.TypeConverters
import com.myAllVideoBrowser.data.local.room.dao.*
import com.myAllVideoBrowser.data.local.room.entity.*

const val DB_VERSION = 4
const val DB_VERSION = 5

@Database(
entities = [PageInfo::class, SupportedPage::class, VideoInfo::class, ProgressInfo::class, HistoryItem::class, AdHost::class],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import io.reactivex.rxjava3.core.Observable
@Dao
interface PageDao {

@Query("SELECT * FROM PageInfo")
@Query("SELECT * FROM PageInfo ORDER BY `order` ASC")
fun getPageInfos(): Observable<List<PageInfo>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertProgressInfo(progressInfo: PageInfo)

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAllProgressInfo(progressInfos: List<PageInfo>)

@Delete
fun deleteProgressInfo(progressInfo: PageInfo)

@Query("DELETE FROM PageInfo")
fun deleteAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ data class PageInfo(
var icon: String = "",

@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
var favicon: ByteArray? = null
var favicon: ByteArray? = null,

@ColumnInfo(name = "order")
@SerializedName("order")
@Expose
var order: Int = 0
) {
// TODO use regex
fun getTitleFiltered(): String {
Expand All @@ -59,10 +64,12 @@ data class PageInfo(

other as PageInfo

if (!favicon.contentEquals(other.favicon)) return false

return link == other.link
}

override fun hashCode(): Int {
return 31 * link.hashCode()
return 31 * link.hashCode() * favicon.contentHashCode()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.myAllVideoBrowser.data.remote

import android.net.Uri
import com.myAllVideoBrowser.data.local.room.entity.PageInfo
import com.myAllVideoBrowser.data.remote.service.ConfigService
import com.myAllVideoBrowser.data.repository.TopPagesRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -13,30 +13,20 @@ class TopPagesRemoteDataSource @Inject constructor(
) : TopPagesRepository {

override suspend fun getTopPages(): List<PageInfo> {
val result = arrayListOf<PageInfo>()
result.add(PageInfo(link = "https://www.imdb.com"))
result.add(PageInfo(link = "https://www.tiktok.com"))
result.add(PageInfo(link = "https://www.vimeo.com/watch"))
result.add(PageInfo(link = "https://www.facebook.com/watch"))
result.add(PageInfo(link = "https://www.instagram.com"))
result.add(PageInfo(link = "https://www.twitter.com"))
result.add(PageInfo(link = "https://www.bilibili.com"))
result.add(PageInfo(link = "https://www.dailymotion.com"))

for (page in result) {
page.name = Uri.parse(page.link).host.toString()
}

return result
return emptyList()
}

override fun saveTopPage(pageInfo: PageInfo) {
}

override fun deletePageInfo(pageInfo: PageInfo) {
override fun replaceBookmarksWith(pageInfos: List<PageInfo>) {

}

override suspend fun updateLocalStorage() {
override fun deletePageInfo(pageInfo: PageInfo) {
}

override suspend fun updateLocalStorageFavicons() : Flow<PageInfo> {
throw NotImplementedError("updateLocalStorage for remote storage NOT IMPLEMENTED")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import com.myAllVideoBrowser.di.qualifier.LocalData
import com.myAllVideoBrowser.di.qualifier.RemoteData
import com.myAllVideoBrowser.util.FaviconUtils
import com.myAllVideoBrowser.util.proxy_utils.OkHttpProxyClient
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -13,9 +17,11 @@ interface TopPagesRepository {

fun saveTopPage(pageInfo: PageInfo)

fun replaceBookmarksWith(pageInfos: List<PageInfo>)

fun deletePageInfo(pageInfo: PageInfo)

suspend fun updateLocalStorage()
suspend fun updateLocalStorageFavicons(): Flow<PageInfo>
}

@Singleton
Expand All @@ -33,29 +39,39 @@ class TopPagesRepositoryImpl @Inject constructor(
localDataSource.saveTopPage(pageInfo)
}

override fun replaceBookmarksWith(pageInfos: List<PageInfo>) {
localDataSource.replaceBookmarksWith(pageInfos)
}

override fun deletePageInfo(pageInfo: PageInfo) {
localDataSource.deletePageInfo(pageInfo)
}

override suspend fun updateLocalStorage() {
// Returns flow of updated elements
override suspend fun updateLocalStorageFavicons(): Flow<PageInfo> = callbackFlow {
val pages = localDataSource.getTopPages()
for (page in pages) {
val bitmap = try {
FaviconUtils.getEncodedFaviconFromUrl(
okHttpClient.getProxyOkHttpClient(),
page.link
)
} catch (e: Throwable) {
null
}
localDataSource.saveTopPage(page)
val bitmapBytes = try {
FaviconUtils.bitmapToBytes(bitmap)
} catch (e: Throwable) {
null
if (page.favicon == null) {
val bitmap = try {
FaviconUtils.getEncodedFaviconFromUrl(
okHttpClient.getProxyOkHttpClient(), page.link
)
} catch (e: Throwable) {
null
}

val bitmapBytes = try {
FaviconUtils.bitmapToBytes(bitmap)
} catch (e: Throwable) {
null
}
page.favicon = bitmapBytes
localDataSource.saveTopPage(page)
delay(10)
trySend(page)
}
page.favicon = bitmapBytes
localDataSource.saveTopPage(page)
}

awaitClose { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ val MIGRATION_3_4 = object : Migration(3, 4) {
}
}

val MIGRATION_4_5 = object : Migration(4, 5) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE PageInfo ADD COLUMN `order` INTEGER NOT NULL DEFAULT 0")
}
}

@Module
class DatabaseModule {

Expand All @@ -46,7 +52,8 @@ class DatabaseModule {
return Room.databaseBuilder(application, AppDatabase::class.java, "dl.db").addMigrations(
MIGRATION_1_2,
MIGRATION_2_3,
MIGRATION_3_4
MIGRATION_3_4,
MIGRATION_4_5
).build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import com.myAllVideoBrowser.di.ActivityScoped
import com.myAllVideoBrowser.di.FragmentScoped
import com.myAllVideoBrowser.ui.main.bookmarks.BookmarksFragment
import com.myAllVideoBrowser.ui.main.help.HelpFragment
import com.myAllVideoBrowser.ui.main.history.HistoryFragment
import com.myAllVideoBrowser.ui.main.home.browser.BrowserFragment
Expand Down Expand Up @@ -71,6 +72,10 @@ abstract class MainModule {
@ContributesAndroidInjector
abstract fun bindLinkFragment(): LinkFragment

@FragmentScoped
@ContributesAndroidInjector
abstract fun bindBookmarksFragment(): BookmarksFragment

@ActivityScoped
@Binds
abstract fun bindMainActivity(mainActivity: MainActivity): Activity
Expand Down
Loading

0 comments on commit f0ac8b8

Please sign in to comment.