Skip to content

Commit

Permalink
refactor: simplify api route logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 4, 2024
1 parent 3e3087e commit 67ba4aa
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 80 deletions.
13 changes: 3 additions & 10 deletions app/src/main/java/com/bnyro/wallpaper/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,9 @@ class App : Application(), ImageLoaderFactory {
)
.build()
}

companion object {
val psApi = PsApi()
val owApi = OwApi()
val usApi = UsApi()
val biApi = BiApi()
val reApi = ReApi()
val leApi = LeApi()
val whApi = WhApi()
val pxApi = PxApi()
val spApi = SpApi()
val apis =
listOf(WhApi(), OwApi(), UsApi(), BiApi(), ReApi(), LeApi(), PxApi(), SpApi(), PsApi())
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/Api.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.bnyro.wallpaper.api

import androidx.compose.ui.graphics.vector.ImageVector
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.Preferences

abstract class Api {
abstract val name: String
abstract val icon: ImageVector
abstract val baseUrl: String

open val filters: Map<String, List<String>> = mapOf()
open val supportsTags: Boolean = false

val route get() = name.replace(" ", "_").lowercase()
private val tagsKey get() = name + "tags"

abstract suspend fun getWallpapers(page: Int): List<Wallpaper>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/bi/BiApi.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bnyro.wallpaper.api.bi

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Nightlight
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper

class BiApi : Api() {
override val name: String = "Bing"
override val baseUrl: String = "https://www.bing.com"
override val icon = Icons.Default.Nightlight

override val filters: Map<String, List<String>> = mapOf(
"resolution" to listOf("1080x1920", "768x1366", "1366x768", "1920x1080", "UHD")
)
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/bnyro/wallpaper/api/le/LeApi.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bnyro.wallpaper.api.le

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Book
import com.bnyro.wallpaper.api.CommunityApi
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper

class LeApi : CommunityApi() {
override val name: String = "Lemmy"
override val baseUrl: String = "https://lemmy.ml"
override val name = "Lemmy"
override val baseUrl = "https://lemmy.ml"
override val icon = Icons.Default.Book

override val filters: Map<String, List<String>> = mapOf(
"sort" to listOf(
"Active",
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/bnyro/wallpaper/api/ow/OwApi.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.bnyro.wallpaper.api.ow

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Air
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

class OwApi : Api() {
override val name: String = "OWalls"
override val baseUrl: String = "https://gist.github.com/"
override val name = "OWalls"
override val baseUrl = "https://gist.github.com/"
override val icon = Icons.Default.Air

override val filters: Map<String, List<String>> = mapOf(
"style" to listOf("all", "light", "dark")
)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/ps/PsApi.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.bnyro.wallpaper.api.ps

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Pix
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper

class PsApi : Api() {
override val name: String = "Picsum"
override val baseUrl: String = "https://picsum.photos"
override val icon = Icons.Default.Pix

private val api = RetrofitHelper.create(baseUrl, Picsum::class.java)

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/px/PxApi.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bnyro.wallpaper.api.px

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Pix
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper

class PxApi : Api() {
override val name: String = "Google Pixel"
override val baseUrl: String = "https://api.github.com"
override val icon = Icons.Default.Pix

private val api = RetrofitHelper.create(baseUrl, Pixel::class.java)
private val imgSrcPrefix = "https://raw.githubusercontent.com/wacko1805/Pixel-Wallpapers/main/"

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/re/ReApi.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bnyro.wallpaper.api.re

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Forum
import com.bnyro.wallpaper.api.CommunityApi
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper

class ReApi : CommunityApi() {
override val name = "Reddit"
override val baseUrl = "https://www.reddit.com/"
override val icon = Icons.Default.Forum

override val filters: Map<String, List<String>> = mapOf(
"sort" to listOf("top", "new", "hot", "rising"),
"time" to listOf("month", "year", "hour", "day", "week")
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/sp/SpApi.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bnyro.wallpaper.api.sp

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.LightMode
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.api.sp.obj.SpotlightImage
import com.bnyro.wallpaper.db.obj.Wallpaper
Expand All @@ -10,8 +12,10 @@ import java.util.Locale
// Credits to https://github.com/ORelio/Spotlight-Downloader/blob/master/SpotlightAPI.md

class SpApi: Api() {
override val name = "Windows Spotlight"
override val name = "Spotlight"
override val baseUrl = "https://fd.api.iris.microsoft.com"
override val icon = Icons.Default.LightMode

override val filters: Map<String, List<String>> = mapOf(
"country" to listOf("US") + Locale.getISOCountries().toList().filter { it != "US" }.sorted()
)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/us/UsApi.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bnyro.wallpaper.api.us

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.WaterDrop
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper

class UsApi() : Api() {
override val name: String = "Unsplash"
override val baseUrl: String = "https://unsplash.com"
override val icon = Icons.Default.WaterDrop

override val filters: Map<String, List<String>> = mapOf(
"order_by" to listOf("latest", "oldest", "popular")
)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/wh/WhApi.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bnyro.wallpaper.api.wh

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Landscape
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper

class WhApi : Api() {
override val name: String = "Wallhaven"
override val baseUrl: String = "https://wallhaven.cc/api/v1/"
override val icon = Icons.Default.Landscape

override val filters: Map<String, List<String>> = mapOf(
"ratios" to listOf("portrait", "landscape"),
"categories" to listOf("general", "anime", "people", "all"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import com.bnyro.wallpaper.ui.models.MainModel
import com.bnyro.wallpaper.ui.nav.AppNavHost
import com.bnyro.wallpaper.ui.nav.DrawerScreens
import com.bnyro.wallpaper.ui.theme.WallYouTheme
import com.bnyro.wallpaper.util.Either
import com.bnyro.wallpaper.util.Preferences
import com.bnyro.wallpaper.util.str
import kotlinx.coroutines.launch

class MainActivity : BaseActivity() {
Expand Down Expand Up @@ -82,7 +84,7 @@ private fun MainContent(
TopAppBar(
title = {
Text(
stringResource(viewModel.titleResource)
text = viewModel.titleResource.str()
)
},
navigationIcon = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.navigation.NavController
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.ui.models.MainModel
import com.bnyro.wallpaper.ui.nav.DrawerScreens
import com.bnyro.wallpaper.util.str
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -101,7 +102,7 @@ fun NavigationDrawer(
Icon(screen.icon, null)
},
label = {
Text(stringResource(screen.titleResource))
Text(screen.title.str())
},
selected = screen == viewModel.currentDestination,
onClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.bnyro.wallpaper.ui.components.prefs.ListPreference
import com.bnyro.wallpaper.ui.components.prefs.SettingsCategory
import com.bnyro.wallpaper.ui.nav.DrawerScreens
import com.bnyro.wallpaper.util.PickFolderContract
import com.bnyro.wallpaper.util.str

@Composable
fun WallpaperChangerPref(config: WallpaperConfig, onChange: (WallpaperConfig) -> Unit) {
Expand Down Expand Up @@ -86,7 +87,7 @@ fun WallpaperChangerPref(config: WallpaperConfig, onChange: (WallpaperConfig) ->
}
MultiSelectionBlockPreference(
preferenceKey = null,
entries = DrawerScreens.apiScreens.map { stringResource(it.titleResource) },
entries = DrawerScreens.apiScreens.map { it.title.str() },
values = DrawerScreens.apiScreens.map { it.route },
defaultSelections = currentSelections
) { selections ->
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bnyro.wallpaper.App
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.db.DatabaseHolder
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.enums.ThemeMode
import com.bnyro.wallpaper.ui.nav.DrawerScreens
import com.bnyro.wallpaper.util.Either
import com.bnyro.wallpaper.util.Preferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -27,8 +29,8 @@ class MainModel : ViewModel() {
).toInt()
var themeMode by mutableStateOf(ThemeMode.values()[themeModeIndex])

var currentDestination: DrawerScreens by mutableStateOf(DrawerScreens.Wallhaven)
var api = Preferences.getApiByRoute(DrawerScreens.Wallhaven.route)
var currentDestination: DrawerScreens by mutableStateOf(DrawerScreens.apiScreens.first())
var api = App.apis.first()

var wallpapers by mutableStateOf(
listOf<Wallpaper>()
Expand All @@ -48,7 +50,7 @@ class MainModel : ViewModel() {
initialValue = listOf()
)

var titleResource by mutableIntStateOf(R.string.app_name)
var titleResource by mutableStateOf<Either<Int, String>>(Either.Left(R.string.app_name))

var page: Int = 1

Expand Down
22 changes: 11 additions & 11 deletions app/src/main/java/com/bnyro/wallpaper/ui/nav/AppNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.App
import com.bnyro.wallpaper.ui.models.MainModel
import com.bnyro.wallpaper.ui.pages.AboutPage
import com.bnyro.wallpaper.ui.pages.FavoritesPage
Expand All @@ -19,7 +19,7 @@ import com.bnyro.wallpaper.util.Preferences
fun AppNavHost(
modifier: Modifier = Modifier,
navController: NavHostController = rememberNavController(),
startDestination: String = DrawerScreens.Wallhaven.route,
startDestination: String = App.apis.first().route,
viewModel: MainModel
) {
NavHost(
Expand All @@ -29,25 +29,25 @@ fun AppNavHost(
) {
DrawerScreens.apiScreens.forEach { screen ->
composable(screen.route) {
viewModel.titleResource = screen.titleResource
viewModel.titleResource = screen.title
viewModel.api = Preferences.getApiByRoute(screen.route)
WallpaperPage(viewModel)
}
}
composable(DrawerScreens.Favorites.route) {
viewModel.titleResource = R.string.favorites
composable(DrawerScreens.Companion.Favorites.route) {
viewModel.titleResource = DrawerScreens.Companion.Favorites.title
FavoritesPage(viewModel)
}
composable(DrawerScreens.History.route) {
viewModel.titleResource = DrawerScreens.History.titleResource
composable(DrawerScreens.Companion.History.route) {
viewModel.titleResource = DrawerScreens.Companion.History.title
HistoryPage(viewModel)
}
composable(DrawerScreens.Settings.route) {
viewModel.titleResource = R.string.settings
composable(DrawerScreens.Companion.Settings.route) {
viewModel.titleResource = DrawerScreens.Companion.Settings.title
SettingsPage(viewModel)
}
composable(DrawerScreens.About.route) {
viewModel.titleResource = R.string.about
composable(DrawerScreens.Companion.About.route) {
viewModel.titleResource = DrawerScreens.Companion.About.title
AboutPage()
}
}
Expand Down
Loading

0 comments on commit 67ba4aa

Please sign in to comment.