Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into crowdin
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa authored Aug 20, 2023
2 parents 4d090a6 + 873fcd7 commit e8bc10c
Show file tree
Hide file tree
Showing 24 changed files with 323 additions and 275 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
alt="Get it on GitHub"
height="80">](https://github.com/SuhasDissa/MemerizeApp/releases/latest)

## Translations
[Crowdin Translate](https://crowdin.com/project/memerize)
## Useful Links
<a href="https://trello.com/b/CNVfaAYD/memerize-app"><img src="https://img.shields.io/badge/Trello-%23026AA7.svg?style=for-the-badge&logo=Trello&logoColor=white" height="40"></a>
<a href="https://crowdin.com/project/memerize"><img src="https://img.shields.io/badge/Crowdin_Translate-%232E3340.svg?style=for-the-badge&logo=Crowdin&logoColor=white" height="40"></a>
12 changes: 6 additions & 6 deletions app/src/main/java/app/suhasdissa/memerize/Destination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ sealed class Destination(val route: String) {
object Subreddits : Destination("subreddits")
object Communities : Destination("communities")
object About : Destination("about")
object PhotoView : Destination("memescreen") {
val routeWithArgs = "$route/{url}"
val arguments = listOf(navArgument("url") { type = NavType.StringType })
object RedditFeed : Destination("reddit_feed") {
val routeWithArgs = "$route/{id}"
val arguments = listOf(navArgument("id") { type = NavType.IntType })
}

object VideoPlayer : Destination("videoplayer") {
val routeWithArgs = "$route/{url}"
val arguments = listOf(navArgument("url") { type = NavType.StringType })
object LemmyFeed : Destination("lemmy_feed") {
val routeWithArgs = "$route/{id}"
val arguments = listOf(navArgument("id") { type = NavType.IntType })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MemerizeApplication : Application(), ImageLoaderFactory {
.respectCacheHeaders(false)
.diskCache(
DiskCache.Builder()
.directory(cacheDir.resolve("coil"))
.directory(cacheDir.resolve("image_cache"))
.maxSizeBytes(
preferences.getInt(imageCacheKey, defaultImageCacheSize) * 1024 * 1024L
)
Expand Down
51 changes: 17 additions & 34 deletions app/src/main/java/app/suhasdissa/memerize/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ package app.suhasdissa.memerize

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import app.suhasdissa.memerize.backend.viewmodels.LemmyViewModel
import app.suhasdissa.memerize.backend.viewmodels.PlayerViewModel
import app.suhasdissa.memerize.backend.viewmodels.RedditViewModel
import app.suhasdissa.memerize.ui.screens.home.CommunityScreen
import app.suhasdissa.memerize.ui.screens.home.HomeScreen
import app.suhasdissa.memerize.ui.screens.home.SubredditScreen
import app.suhasdissa.memerize.ui.screens.primary.LemmyMemeScreen
import app.suhasdissa.memerize.ui.screens.primary.RedditMemeScreen
import app.suhasdissa.memerize.ui.screens.secondary.PhotoView
import app.suhasdissa.memerize.ui.screens.secondary.VideoView
import app.suhasdissa.memerize.ui.screens.secondary.LemmyMemeFeed
import app.suhasdissa.memerize.ui.screens.secondary.RedditMemeFeed
import app.suhasdissa.memerize.ui.screens.settings.AboutScreen
import app.suhasdissa.memerize.ui.screens.settings.SettingsScreen

Expand All @@ -32,9 +28,6 @@ fun AppNavHost(
onDrawerOpen: () -> Unit,
modifier: Modifier = Modifier
) {
val redditViewModel: RedditViewModel = viewModel(factory = RedditViewModel.Factory)
val lemmyViewModel: LemmyViewModel = viewModel(factory = LemmyViewModel.Factory)
val playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory)
NavHost(
navController = navController,
startDestination = Destination.Home.route,
Expand All @@ -45,9 +38,7 @@ fun AppNavHost(
onNavigate = { destination ->
navController.navigateTo(destination.route)
},
onDrawerOpen,
redditViewModel = redditViewModel,
lemmyViewModel = lemmyViewModel
onDrawerOpen
)
}
composable(route = Destination.Settings.route) {
Expand All @@ -71,45 +62,37 @@ fun AppNavHost(
route = Destination.RedditMemeView.route
) {
RedditMemeScreen(
redditViewModel = redditViewModel,
onClickMeme = { url ->
navController.navigateTo("${Destination.PhotoView.route}/$url")
},
onClickVideo = { url ->
navController.navigateTo("${Destination.VideoPlayer.route}/$url")
onClickCard = { id ->
navController.navigateTo("${Destination.RedditFeed.route}/$id")
}
)
}
composable(
route = Destination.LemmyMemeView.route
) {
LemmyMemeScreen(
lemmyViewModel = lemmyViewModel,
onClickMeme = { url ->
navController.navigateTo("${Destination.PhotoView.route}/$url")
},
onClickVideo = { url ->
navController.navigateTo("${Destination.VideoPlayer.route}/$url")
onClickCard = { id ->
navController.navigateTo("${Destination.LemmyFeed.route}/$id")
}
)
}
composable(
route = Destination.PhotoView.routeWithArgs,
arguments = Destination.PhotoView.arguments
route = Destination.RedditFeed.routeWithArgs,
arguments = Destination.RedditFeed.arguments
) {
val imgurl = it.arguments?.getString("url")
if (imgurl != null) {
PhotoView(imgurl)
val id = it.arguments?.getInt("id")
if (id != null) {
RedditMemeFeed(initialPage = id)
}
}

composable(
route = Destination.VideoPlayer.routeWithArgs,
arguments = Destination.VideoPlayer.arguments
route = Destination.LemmyFeed.routeWithArgs,
arguments = Destination.LemmyFeed.arguments
) {
val url = it.arguments?.getString("url")
if (url != null) {
VideoView(url = url, playerViewModel = playerViewModel)
val id = it.arguments?.getInt("id")
if (id != null) {
LemmyMemeFeed(initialPage = id)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ private const val header =
interface LemmyApi {
/**
* @param community The name of the community.
* @param sort "TopAll", "TopDay", "TopMonth", "TopWeek".
* @param sort "Active", "Hot", "MostComments", "New", "NewComments",
* "Old", "TopAll", "TopDay", "TopMonth", "TopWeek",
* "TopYear".
*/
@Headers(header)
@GET("https://{instance}/api/v3/post/list")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ private const val header =

interface RedditApi {
@Headers(header)
@GET("r/{subreddit}/top.json?sort=top&limit=100")
@GET("r/{subreddit}/{sort}.json")
suspend fun getRedditData(
@Path("subreddit") subreddit: String,
@Query("t") time: String
@Path("sort") sort: String,
@Query("t") time: String? = null
): Reddit

@Headers(header)
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/java/app/suhasdissa/memerize/backend/model/Sort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
Created By Suhas Dissanayake on 7/30/23, 6:12 PM
Copyright (c) 2023
https://github.com/SuhasDissa/
All Rights Reserved
******************************************************************************/

package app.suhasdissa.memerize.backend.model

import app.suhasdissa.memerize.R

sealed class Sort(open val name: Int, val redditSort: String, open val lemmySort: String) {
object Hot : Sort(R.string.hot, "hot", "Hot")
object New : Sort(R.string.sort_new, "new", "New")
object Rising : Sort(R.string.rising, "rising", "Active")
sealed class Top(val redditT: String) : Sort(R.string.top, "top", "") {
object Today : Top("today") {
override val name = R.string.reddit_today_btn
override val lemmySort = "TopDay"
}

object Week : Top("week") {
override val name = R.string.reddit_week_btn
override val lemmySort = "TopWeek"
}

object Month : Top("month") {
override val name = R.string.reddit_month_btn
override val lemmySort = "TopMonth"
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import app.suhasdissa.memerize.backend.apis.LemmyApi
import app.suhasdissa.memerize.backend.database.dao.LemmyMemeDAO
import app.suhasdissa.memerize.backend.database.entity.LemmyCommunity
import app.suhasdissa.memerize.backend.database.entity.LemmyMeme
import app.suhasdissa.memerize.backend.model.Sort

interface LemmyMemeRepository : MemeRepository<LemmyMeme, LemmyCommunity>
class LemmyMemeRepositoryImpl(
Expand All @@ -22,10 +23,10 @@ class LemmyMemeRepositoryImpl(

override suspend fun getOnlineData(
community: LemmyCommunity,
time: String
sort: Sort
): List<LemmyMeme>? {
return try {
val memesList = getNetworkData(community, time)
val memesList = getNetworkData(community, sort.lemmySort)
Thread {
insertMemes(memesList)
}.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ package app.suhasdissa.memerize.backend.repositories

import app.suhasdissa.memerize.backend.database.entity.AboutCommunity
import app.suhasdissa.memerize.backend.database.entity.Meme
import app.suhasdissa.memerize.backend.model.Sort

interface MemeRepository<T : Meme, C : AboutCommunity> {
suspend fun getOnlineData(community: C, time: String): List<T>?
suspend fun getOnlineData(community: C, sort: Sort): List<T>?
suspend fun getLocalData(community: C): List<T>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ All Rights Reserved

package app.suhasdissa.memerize.backend.repositories

import android.util.Log
import androidx.annotation.WorkerThread
import app.suhasdissa.memerize.backend.apis.RedditApi
import app.suhasdissa.memerize.backend.database.dao.RedditMemeDao
import app.suhasdissa.memerize.backend.database.entity.RedditCommunity
import app.suhasdissa.memerize.backend.database.entity.RedditMeme
import app.suhasdissa.memerize.backend.model.Sort

interface RedditMemeRepository : MemeRepository<RedditMeme, RedditCommunity>

Expand All @@ -23,25 +25,34 @@ class RedditMemeRepositoryImpl(
private val imageRegex = Regex("^.+\\.(jpg|jpeg|png|webp)\$")
override suspend fun getOnlineData(
community: RedditCommunity,
time: String
sort: Sort
): List<RedditMeme>? {
val srt = when (sort) {
is Sort.Top -> sort.redditSort to sort.redditT
else -> sort.redditSort to null
}
return try {
val memesList = getNetworkData(community.id, time)
val memesList = getNetworkData(community.id, srt.first, srt.second)
Thread {
insertMemes(memesList)
}.start()
memesList
} catch (e: Exception) {
Log.e("Reddit Repository", e.message, e)
null
}
}

override suspend fun getLocalData(community: RedditCommunity): List<RedditMeme> =
redditMemeDao.getAll(community.id)

private suspend fun getNetworkData(subreddit: String, time: String): List<RedditMeme> {
private suspend fun getNetworkData(
subreddit: String,
sort: String,
time: String?
): List<RedditMeme> {
val memeList: ArrayList<RedditMeme> = arrayListOf()
val redditData = redditApi.getRedditData(subreddit, time).data.children
val redditData = redditApi.getRedditData(subreddit, sort, time).data.children
redditData.forEach { child ->
val url = child.Childdata.url
if (url.matches(imageRegex)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import app.suhasdissa.memerize.MemerizeApplication
import app.suhasdissa.memerize.backend.database.entity.LemmyCommunity
import app.suhasdissa.memerize.backend.model.SortTime
import app.suhasdissa.memerize.backend.model.lemmy
import app.suhasdissa.memerize.backend.model.Sort
import app.suhasdissa.memerize.backend.repositories.LemmyMemeRepository
import app.suhasdissa.memerize.backend.viewmodels.state.MemeUiState
import kotlinx.coroutines.launch
Expand All @@ -31,19 +30,19 @@ class LemmyViewModel(private val lemmyRepository: LemmyMemeRepository) :

var currentCommunity: LemmyCommunity? = null
private set
var currentSortTime: SortTime = SortTime.TODAY
var currentSortTime: Sort = Sort.Top.Today
private set

fun getMemePhotos(
community: LemmyCommunity? = currentCommunity,
time: SortTime = SortTime.TODAY
sort: Sort = Sort.Top.Today
) {
currentCommunity = community!!
currentSortTime = time
currentSortTime = sort
viewModelScope.launch {
memeUiState = MemeUiState.Loading

memeUiState = when (val data = lemmyRepository.getOnlineData(community, time.lemmy)) {
memeUiState = when (val data = lemmyRepository.getOnlineData(community, sort)) {
null -> {
MemeUiState.Error("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,16 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import app.suhasdissa.memerize.MemerizeApplication
import app.suhasdissa.memerize.utils.RedditVideoDownloader
import kotlinx.coroutines.launch

@UnstableApi
class PlayerViewModel(appContext: Context) : ViewModel() {
val player: ExoPlayer = ExoPlayer.Builder(appContext).build()

class PlayerViewModel() : ViewModel() {
var downloadState: DownloadState by mutableStateOf(DownloadState.NotStarted)

fun playPause() {
if (player.isPlaying) player.pause() else player.play()
}

fun seekTo(to: Long) {
player.seekTo(to)
}

@RequiresApi(Build.VERSION_CODES.O)
fun downloadVideo(context: Context, url: String) {
viewModelScope.launch {
Expand All @@ -54,14 +40,8 @@ class PlayerViewModel(appContext: Context) : ViewModel() {
}
}
}
}

companion object {
val Factory: ViewModelProvider.Factory = viewModelFactory {
initializer {
val application =
(this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MemerizeApplication) // ktlint-disable max-line-length
PlayerViewModel(application)
}
}
}
fun Player.playPause() {
if (isPlaying) pause() else play()
}
Loading

0 comments on commit e8bc10c

Please sign in to comment.