Skip to content

Commit

Permalink
feat(GiphyDialog): add theme support
Browse files Browse the repository at this point in the history
  • Loading branch information
pshoniuk committed May 18, 2023
1 parent 5247db2 commit 04cfdfe
Show file tree
Hide file tree
Showing 16 changed files with 771 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ private fun giphyDialogSettingsFromRNValue(
): GPHSettings {
val settings = initialSettings?.copy() ?: GPHSettings()

RNGiphyTheme(context, options.getMap("theme")).applyToGPHCustomTheme()
settings.theme = GPHTheme.Custom

if (options.hasKey(RNSettings.renditionType.toString())) {
settings.renditionType = RNGiphyRendition.fromRNValue(
options.getString(RNSettings.renditionType.toString())
Expand Down
129 changes: 129 additions & 0 deletions android/src/main/java/com/giphyreactnativesdk/RNGiphyTheme.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.giphyreactnativesdk


import android.content.Context
import com.facebook.react.bridge.ReadableMap
import com.giphy.sdk.ui.themes.GPHCustomTheme
import com.giphy.sdk.ui.themes.GPHTheme
import com.giphy.sdk.ui.themes.Theme
import com.giphyreactnativesdk.utils.CaseConverter

val DEFAULT_PRESET = GPHTheme.Dark


class RNGiphyTheme(context: Context, private var config: ReadableMap?) {
private var preset: Theme

init {
val presetTheme = gphThemeByName(config?.getString("preset")) ?: DEFAULT_PRESET
preset = presetTheme.getThemeResources(context)
}

fun applyToGPHCustomTheme() {
GPHCustomTheme.apply {
// region dialog's handle

handleBarColor = getIntOrPreset("handleBarColor")

// endregion

// region emoji drawer

emojiDrawerGradientTopColor = getIntOrPreset("emojiDrawerGradientTopColor")
emojiDrawerGradientBottomColor = getIntOrPreset("emojiDrawerGradientBottomColor")
emojiDrawerSeparatorColor = getIntOrPreset("emojiDrawerSeparatorColor")


// endregion

// region search bar

searchBackButtonColor = getIntOrPreset("searchBackButtonColor")
searchBarBackgroundColor = getIntOrPreset("searchBarBackgroundColor")
searchTextColor = getIntOrPreset("searchTextColor")
searchPlaceholderTextColor = getIntOrPreset("searchPlaceholderTextColor")

// endregion

// region suggestions

suggestionCellBackgroundColor = getIntOrPreset("suggestionCellBackgroundColor")
suggestionCellTextColor = getIntOrPreset("suggestionCellTextColor")

// endregion

// region tab bar

tabBarSwitchDefaultColor = getIntOrPreset("tabBarSwitchDefaultColor")
tabBarSwitchSelectedColor = getIntOrPreset("tabBarSwitchSelectedColor")

// endregion

// region confirmation

confirmationSelectButtonColor = getIntOrPreset("confirmationSelectButtonColor")
confirmationSelectButtonTextColor = getIntOrPreset("confirmationSelectButtonTextColor")
confirmationBackButtonColor = getIntOrPreset("confirmationBackButtonColor")
confirmationViewOnGiphyColor = getIntOrPreset("confirmationViewOnGiphyColor")

// endregion

// region other

defaultTextColor = getIntOrPreset("defaultTextColor")
backgroundColor = getIntOrPreset("backgroundColor")
dialogOverlayBackgroundColor = getIntOrPreset("dialogOverlayBackgroundColor")
usernameColor = getIntOrPreset("usernameColor")

// endregion
}
}

private fun getIntOrPreset(field: String): Int {
if (config == null || config?.hasKey(field) == false) {
return getPresetField(field) as Int
}
return config!!.getInt(field)
}

@Suppress("IMPLICIT_CAST_TO_ANY")
private fun getPresetField(field: String): Any {
return when (field) {
"handleBarColor" -> preset.handleBarColor
"emojiDrawerGradientTopColor" -> preset.emojiDrawerGradientTopColor
"emojiDrawerGradientBottomColor" -> preset.emojiDrawerGradientBottomColor
"emojiDrawerSeparatorColor" -> preset.emojiDrawerSeparatorColor
"searchBarBackgroundColor" -> preset.searchBarBackgroundColor
"searchTextColor" -> preset.searchTextColor
"searchButtonIcon" -> preset.searchButtonIcon
"searchBackButtonColor" -> preset.searchBackButtonColor
"searchPlaceholderTextColor" -> preset.searchPlaceholderTextColor
"suggestionCellBackgroundColor" -> preset.suggestionCellBackgroundColor
"suggestionCellTextColor" -> preset.suggestionCellTextColor
"tabBarSwitchDefaultColor" -> preset.tabBarSwitchDefaultColor
"tabBarSwitchSelectedColor" -> preset.tabBarSwitchSelectedColor
"confirmationSelectButtonColor" -> preset.confirmationSelectButtonColor
"confirmationSelectButtonTextColor" -> preset.confirmationSelectButtonTextColor
"confirmationBackButtonColor" -> preset.confirmationBackButtonColor
"confirmationViewOnGiphyColor" -> preset.confirmationViewOnGiphyColor
"backgroundColor" -> preset.backgroundColor
"dialogOverlayBackgroundColor" -> preset.dialogOverlayBackgroundColor
"defaultTextColor" -> preset.defaultTextColor
"usernameColor" -> preset.usernameColor
else -> {
throw IllegalArgumentException("Unknown field: $field")
}
} as Any
}

companion object {
private fun gphThemeByName(name: String?): GPHTheme? {
if (name == null) {
return null
}
return GPHTheme.values().firstOrNull {
it.name == CaseConverter.capitalize(name)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import com.giphyreactnativesdk.GiphyReactNativeSdkPackage;
import com.giphyreactnativesdk.RNGiphySdkPackage;

public class MainApplication extends Application implements ReactApplication {

Expand All @@ -27,7 +27,7 @@ protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for GiphyReactNativeSdkExample:
// packages.add(new MyReactNativePackage());
packages.add(new GiphyReactNativeSdkPackage());
packages.add(new RNGiphySdkPackage());
return packages;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.7">
<BuildAction
parallelizeBuildables = "NO"
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
Expand All @@ -20,38 +19,14 @@
ReferencedContainer = "container:GiphyReactNativeSdkExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "13B07F861A680F5B00A75B9A"
BuildableName = "GiphyReactNativeSdkExample.app"
BlueprintName = "GiphyReactNativeSdkExample"
ReferencedContainer = "container:GiphyReactNativeSdkExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "13B07F861A680F5B00A75B9A"
BuildableName = "GiphyReactNativeSdkExample.app"
BlueprintName = "GiphyReactNativeSdkExample"
ReferencedContainer = "container:GiphyReactNativeSdkExample.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
</Testables>
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Release"
Expand Down Expand Up @@ -84,16 +59,6 @@
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "13B07F861A680F5B00A75B9A"
BuildableName = "GiphyReactNativeSdkExample.app"
BlueprintName = "GiphyReactNativeSdkExample"
ReferencedContainer = "container:GiphyReactNativeSdkExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
Loading

0 comments on commit 04cfdfe

Please sign in to comment.