Skip to content

Commit

Permalink
removed deprecated code, applying state working
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravjot committed Oct 30, 2024
1 parent bf479a1 commit c8048a5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 37 deletions.
30 changes: 22 additions & 8 deletions app/(tabs)/downloaded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {CheckCircle, ImageIcon} from "lucide-react-native";
import * as WallpaperManager from "@/modules/wallpaper-manager";
import {fadingPulseAnimation} from "@/lib/animations/fading_pulse";
import {onChangeListener} from "../../modules/wallpaper-manager/index";
import LoadingSpinner from "@/components/ui/LoadingSpinner";

type WallpaperApplyState = {
status: "idle" | "applying" | "applied" | "error";
Expand All @@ -24,8 +25,12 @@ export default function DownloadedWallpapersScreen() {
React.useEffect(() => {
// wallpaper change listener
const wallpaperChangeListener = onChangeListener(e => {
setApplyState({status: e.success ? "applied" : "error", path: applyState.path});
if (e.success) ToastAndroid.show("Wallpaper applied successfully", ToastAndroid.SHORT);
setApplyState({status: e.success ? "applied" : "error", path: e.path});
if (e.success) {
ToastAndroid.show("Wallpaper applied successfully", ToastAndroid.SHORT);
} else {
ToastAndroid.show("Failed to apply wallpaper", ToastAndroid.SHORT);
}
});
return () => {
// When component is killed, clear all listeners
Expand Down Expand Up @@ -57,10 +62,10 @@ export default function DownloadedWallpapersScreen() {
renderItem={({item}) => (
<WallpaperGridItem
wallpaper={item}
isApplied={applyState.status === "applied" && applyState.path === item.path}
applyWallpaper={() => {
WallpaperManager.setWallpaper(item.path);
applyState={applyState.path === item.path ? applyState.status : "idle"}
applyWallpaper={async () => {
setApplyState({status: "applying", path: item.path});
await WallpaperManager.setWallpaper(item.path);
}}
/>
)}
Expand All @@ -77,11 +82,11 @@ export default function DownloadedWallpapersScreen() {

function WallpaperGridItem({
wallpaper,
isApplied,
applyState,
applyWallpaper,
}: {
wallpaper: DownloadedWallpaperPostType;
isApplied: boolean;
applyState: "idle" | "applying" | "applied" | "error";
applyWallpaper: () => void;
}) {
return (
Expand All @@ -98,7 +103,7 @@ function WallpaperGridItem({
{/* <View className="z-10 flex-1 w-full h-full border rounded-lg border-foreground/10">
<ImageView path={wallpaper.path} />
</View> */}
{isApplied ? (
{applyState === "applied" ? (
<Button
variant={"ghost"}
size={"md"}
Expand All @@ -107,6 +112,15 @@ function WallpaperGridItem({
<CheckCircle size={20} color="white" />
<ButtonText>Applied</ButtonText>
</Button>
) : applyState === "applying" ? (
<Button
variant={"ghost"}
size={"md"}
className="absolute z-20 px-3 rounded bottom-1 right-1 bg-background/80"
disabled>
<LoadingSpinner size={20} color="white" />
<ButtonText>Applying</ButtonText>
</Button>
) : (
<Button
variant={"ghost"}
Expand Down
16 changes: 12 additions & 4 deletions app/download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ export default function DownloadScreen() {
}, []);

// Apply wallpaper
function applyWallpaper() {
async function applyWallpaper() {
try {
setApplyState("applying");
setWallpaper(fileSystemPath as string);
await setWallpaper(fileSystemPath as string);
} catch (e) {
// TODO: Log this error somewhere
console.log(e);
Expand All @@ -168,8 +168,11 @@ export default function DownloadScreen() {
<Animated.View
style={fadingPulseAnimation(3000)}
className="absolute top-0 left-0 z-0 w-full h-full bg-foreground/20"></Animated.View>
<Image className="z-10 flex-1 object-contain w-full h-full" source={{uri: wallpaper.image.url}} />
<View className="absolute top-0 left-0 right-0 z-30 w-full">
<Image
className="absolute top-0 bottom-0 left-0 right-0 z-10 object-contain w-full h-full"
source={{uri: wallpaper.image.url}}
/>
<View className="absolute top-0 left-0 right-0 z-30 w-full h-10">
<SafeAreaView>
<Animated.View style={{opacity: animateOpacity}}>
<Button
Expand Down Expand Up @@ -200,6 +203,11 @@ export default function DownloadScreen() {
<ArrowDownCircle size={16} color="white" />
<ButtonText>Download</ButtonText>
</Button>
) : applyState === "applying" ? (
<Button variant={"emerald"} className="absolute z-30 px-0 pl-2 pr-4 rounded-full -top-6 right-4" disabled>
<LoadingSpinner />
<ButtonText>Applying</ButtonText>
</Button>
) : downloadState.status === "complete" && applyState !== "applied" ? (
<Button
variant={"emerald"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import android.content.Context
import android.app.WallpaperManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageDecoder
import java.io.IOException
import android.provider.MediaStore
import android.net.Uri
import android.media.MediaScannerConnection
import android.util.Log
import androidx.core.os.bundleOf
import java.util.concurrent.Executors

class WallpaperManagerModule : Module() {

Expand All @@ -29,8 +32,10 @@ class WallpaperManagerModule : Module() {
// Defines event names that the module can send to JavaScript.
Events("onChange")

Function("setWallpaper") { path: String ->
setWallpaper(context, path)
AsyncFunction("setWallpaper") { path: String ->
Executors.newSingleThreadExecutor().execute{
setWallpaper(context, path)
}
}

// Enables the module to be used as a native view. Definition components that are accepted as part of
Expand All @@ -44,34 +49,37 @@ class WallpaperManagerModule : Module() {
}


fun setWallpaper(context: Context, path: String) {
private fun setWallpaper(context: Context, path: String) {
// Set wallpaper
val wallpaperManager = WallpaperManager.getInstance(context)

val options: BitmapFactory.Options = BitmapFactory.Options()
options.inPreferredConfig = Bitmap.Config.ARGB_8888

MediaScannerConnection.scanFile(context, arrayOf(path), null,
object : MediaScannerConnection.OnScanCompletedListener {
override fun onScanCompleted(path: String, uri: Uri) {
val bitmap: Bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
if (bitmap != null) {
try {
wallpaperManager.setBitmap(bitmap)
// send event to JavaScript
this@WallpaperManagerModule.sendEvent("onChange", bundleOf(
"success" to true
))
} catch (e: IOException) {
e.printStackTrace()
// send event to JavaScript
this@WallpaperManagerModule.sendEvent("onChange", bundleOf(
"success" to false
))
}
}
}
MediaScannerConnection.scanFile(context, arrayOf(path), null
) { path, uri ->
try {
val source: ImageDecoder.Source = ImageDecoder.createSource(context.contentResolver, uri)
val bitmap: Bitmap = ImageDecoder.decodeBitmap(source);
wallpaperManager.setBitmap(bitmap)
// send event to JavaScript
this@WallpaperManagerModule.sendEvent(
"onChange", bundleOf(
"success" to true,
"path" to path
)
)
} catch (e: Exception) {
Log.e("WallpaperManagerModule", "Attempted to set wallpaper: $path")
e.printStackTrace()
// send event to JavaScript
this@WallpaperManagerModule.sendEvent(
"onChange", bundleOf(
"success" to false,
"path" to path
)
)
}
)
}
}
}
2 changes: 1 addition & 1 deletion modules/wallpaper-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import WallpaperManagerModule from "./src/WallpaperManagerModule";
import WallpaperManagerView from "./src/WallpaperManagerView";
import {ChangeEventPayload, WallpaperManagerViewProps} from "./src/WallpaperManager.types";

export function setWallpaper(path: string) {
export async function setWallpaper(path: string): Promise<boolean> {
return WallpaperManagerModule.setWallpaper(path);
}

Expand Down
1 change: 1 addition & 0 deletions modules/wallpaper-manager/src/WallpaperManager.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type ChangeEventPayload = {
success: boolean;
path: string;
};

export type WallpaperManagerViewProps = {
Expand Down

0 comments on commit c8048a5

Please sign in to comment.