Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: JetSharing Functionality #169

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion app/src/main/java/com/devx/jetjoke/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.devx.jetjoke

import android.content.Intent
import android.content.Intent.ACTION_SEND
import android.content.Intent.EXTRA_TEXT
import android.content.Intent.createChooser
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -10,6 +14,8 @@ import androidx.compose.ui.Modifier
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.devx.jetjoke.navigation.AppNavGraph
import com.devx.jetjoke.theme.JetJokeTheme
import com.devx.jetjoke.ui.actions.AppActions
import com.devx.jetjoke.ui.actions.ShareJokeAction
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -23,9 +29,27 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,
) {
AppNavGraph()
AppNavGraph(action = ::performActions)
}
}
}
}


private fun performActions(action: AppActions){
when(action){
is ShareJokeAction -> {
shareJoke(action.joke)
}
}
}

private fun shareJoke(jokeText:String){
val shareIntent = Intent(ACTION_SEND).apply {
type = "text/plain"
putExtra(EXTRA_TEXT, jokeText)
}
val chooserIntent = createChooser(shareIntent, null)
this.startActivity(chooserIntent)
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/devx/jetjoke/navigation/AppNavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.devx.jetjoke.ui.actions.AppActions
import com.devx.jetjoke.ui.home.HomeScreen
import com.devx.jetjoke.ui.home.HomeViewModel

@Composable
fun AppNavGraph() {
fun AppNavGraph(action : (AppActions)-> Unit) {
val navController = rememberNavController()

NavHost(
Expand All @@ -26,6 +27,7 @@ fun AppNavGraph() {
loadNextJoke = {
homeViewModel.fetchJoke()
},
action = action,
)
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/devx/jetjoke/ui/actions/AppActions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.devx.jetjoke.ui.actions

sealed class AppActions

data class ShareJokeAction(val joke:String): AppActions()
19 changes: 17 additions & 2 deletions app/src/main/java/com/devx/jetjoke/ui/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import com.devx.data.util.networkUtil.Constant
import com.devx.jetjoke.R
import com.devx.jetjoke.theme.JetJokeTheme
import com.devx.jetjoke.ui.actions.AppActions
import com.devx.jetjoke.ui.actions.ShareJokeAction
import com.devx.jetjoke.ui.home.component.HomeHeader
import com.devx.jetjoke.ui.home.component.JokeCategoryTag
import com.devx.jetjoke.ui.home.component.TypewriterText
Expand All @@ -44,6 +47,7 @@ import com.devx.jetjoke.util.ThemedPreview
fun HomeScreen(
uiState: HomeScreenUiState,
loadNextJoke: () -> Unit,
action : (AppActions)-> Unit,
) {
var displayJoke by remember { mutableStateOf("") }

Expand Down Expand Up @@ -104,7 +108,7 @@ fun HomeScreen(
.padding(top = 470.dp)
.fillMaxWidth()
.wrapContentHeight(),
horizontalArrangement = Arrangement.Center,
horizontalArrangement = Arrangement.SpaceEvenly,
) {
Image(
modifier = Modifier
Expand All @@ -114,7 +118,17 @@ fun HomeScreen(
.clickable { loadNextJoke() }
.padding(all = 8.dp),
painter = painterResource(id = R.drawable.ic_refresh),
contentDescription = null,
contentDescription = stringResource(R.string.get_next_joke_button),
)
Image(
modifier = Modifier
.size(size = 64.dp)
.clip(shape = MaterialTheme.shapes.extraLarge)
.background(color = MaterialTheme.colorScheme.primary)
.clickable { action.invoke(ShareJokeAction(displayJoke)) }
.padding(all = 8.dp),
painter = painterResource(id = R.drawable.baseline_share_24),
contentDescription = stringResource(R.string.button_to_share_joke),
)
}
}
Expand All @@ -127,6 +141,7 @@ private fun HomeScreenPreview(@PreviewParameter(HomeScreenPreviewParameter::clas
HomeScreen(
uiState = uiState,
loadNextJoke = {},
action = {},
)
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_share_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z"/>

</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<string name="app_name">JetJoke</string>
<string name="app_tagline">Your Daily Dose of Humour</string>
<string name="button_to_share_joke">button to share joke</string>
<string name="get_next_joke_button">get next joke button</string>
</resources>