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

PR: Notification Management feature #12

Merged
merged 10 commits into from
Nov 5, 2024
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package com.doyoonkim.knutice.fcm

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.drawable.Icon
import android.os.Build
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.doyoonkim.knutice.data.KnuticeRemoteSource
import com.example.knutice.R
import com.google.android.datatransport.Priority
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import java.util.UUID
import javax.inject.Inject
import kotlin.random.Random
import kotlin.random.nextInt

class PushNotificationHandler @Inject constructor() : FirebaseMessagingService() {
@Inject lateinit var remoteSource: KnuticeRemoteSource
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/doyoonkim/knutice/model/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package com.doyoonkim.knutice.model

enum class NoticeCategory { GENERAL_NEWS, ACADEMIC_NEWS, SCHOLARSHIP_NEWS, EVENT_NEWS, Unspecified }

enum class Destination { MAIN, MORE_GENERAL, MORE_ACADEMIC, MORE_SCHOLARSHIP, MORE_EVENT, SETTINGS, CS }
enum class Destination { MAIN, MORE_GENERAL, MORE_ACADEMIC, MORE_SCHOLARSHIP, MORE_EVENT, SETTINGS, CS, OSS }
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.doyoonkim.knutice.model.Destination
import com.doyoonkim.knutice.model.NoticeCategory
import com.doyoonkim.knutice.presentation.CategorizedNotification
import com.doyoonkim.knutice.presentation.MoreCategorizedNotification
import com.doyoonkim.knutice.presentation.OpenSourceLicenseNotice
import com.doyoonkim.knutice.presentation.UserPreference
import com.doyoonkim.knutice.viewModel.MainActivityViewModel

Expand Down Expand Up @@ -75,8 +76,16 @@ fun MainNavigator(
updatedCurrentLocation = Destination.SETTINGS
)
UserPreference(
Modifier.padding(top = 20.dp, start = 10.dp, end = 10.dp)
Modifier.padding(top = 20.dp, start = 10.dp, end = 10.dp),
navController
)
}

composable(Destination.OSS.name) {
viewModel.updateState(
updatedCurrentLocation = Destination.OSS
)
OpenSourceLicenseNotice()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.doyoonkim.knutice.presentation
import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand Down Expand Up @@ -48,7 +49,8 @@ fun DetailedNoticeContent(
val localContext = LocalContext.current

Surface(
modifier = modifier.fillMaxWidth(),
modifier = modifier.fillMaxWidth()
.background(MaterialTheme.colorScheme.containerBackground),
shape = RoundedCornerShape(10.dp),
color = MaterialTheme.colorScheme.containerBackground
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class MainActivity : ComponentActivity() {
// Permission is already granted, and Push Notification is available
} else {
// Need to inform user that the app won't display push notification.
// TODO: Add small pop-up style composable to be shown to indicate that a push notification won't available.
}
}

Expand Down Expand Up @@ -83,6 +84,13 @@ class MainActivity : ComponentActivity() {
}
}
}

override fun onDestroy() {
super.onDestroy()

viewModelStore.clear()
this.externalCacheDir?.delete()
}
}

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -129,6 +137,7 @@ fun MainServiceScreen(
Destination.MORE_SCHOLARSHIP -> R.string.scholarship_news
Destination.MORE_EVENT -> R.string.event_news
Destination.SETTINGS -> R.string.title_preference
Destination.OSS -> R.string.oss_notice
else -> R.string.app_name
}),
fontSize = 20.sp,
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/com/doyoonkim/knutice/presentation/OssNotice.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.doyoonkim.knutice.presentation

import android.webkit.WebView
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView

@Composable
fun OpenSourceLicenseNotice(
modifier: Modifier = Modifier
) {
AndroidView(
modifier = modifier.fillMaxWidth().padding(5.dp),
factory = { context ->
WebView(context).apply {
loadUrl("https://knutice.github.io/KNUTICE-OpenSourceLicense/Android/opensource.html")
}
}
)
}

@Composable
@Preview
fun OpenSourceLicenseNotice_Preview() {
OpenSourceLicenseNotice()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.doyoonkim.knutice.presentation

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -8,18 +12,33 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.doyoonkim.knutice.model.Destination
import com.doyoonkim.knutice.ui.theme.buttonContainer
import com.doyoonkim.knutice.ui.theme.subTitle
import com.example.knutice.R

Expand All @@ -28,7 +47,17 @@ import com.example.knutice.R
@Composable
fun UserPreference(
modifier: Modifier = Modifier,
navController: NavController = rememberNavController()
) {
var permissionStatus by remember { mutableStateOf(false) }
val context = LocalContext.current

LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
permissionStatus = ContextCompat.checkSelfPermission(
context, Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
}

Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally,
Expand Down Expand Up @@ -78,9 +107,17 @@ fun UserPreference(
}

Switch(
checked = false,
onCheckedChange = { },
enabled = false
checked = permissionStatus,
onCheckedChange = {
val settingIntent = Intent(
"android.settings.APP_NOTIFICATION_SETTINGS"
).apply {
this.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
this.putExtra("android.provider.extra.APP_PACKAGE", context.packageName)
}
context.startActivity(settingIntent)
},
enabled = true
)
}
}
Expand Down Expand Up @@ -133,8 +170,8 @@ fun UserPreference(

Row(
modifier = Modifier.fillMaxWidth().wrapContentSize()
.padding(top = 15.dp, bottom = 15.dp),
verticalAlignment = Alignment.Bottom
.padding(top = 5.dp, bottom = 5.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
modifier = Modifier.wrapContentHeight().weight(5f),
Expand All @@ -144,14 +181,15 @@ fun UserPreference(
textAlign = TextAlign.Start
)

//TODO: Should be replaced with Actual Icon Button.
Text(
modifier = Modifier.wrapContentHeight().weight(2f),
text = ">",
fontWeight = FontWeight.Normal,
fontSize = 14.sp,
textAlign = TextAlign.End
)
IconButton(
onClick = { navController.navigate(Destination.OSS.name) }
) {
Image(
painter = painterResource(R.drawable.baseline_arrow_forward_ios_24),
contentDescription = "Button to OSS Notice",
colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.buttonContainer)
)
}
}

HorizontalDivider(
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_arrow_forward_ios_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:autoMirrored="true" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M6.23,20.23l1.77,1.77l10,-10l-10,-10l-1.77,1.77l8.23,8.23z"/>

</vector>
1 change: 1 addition & 0 deletions app/src/main/res/values-ko-rKR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<string name="title_preference">설정</string>
<string name="new_notice">새로운 알림이 도착했어요!</string>
<string name="inapp_notificaiton_channel_name">신규 공지 알림</string>
<string name="oss_notice">오픈소스 라이센스</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<string name="inapp_notification_channel_id" translatable="false">knutice_inapp_notification_channel</string>
<string name="inapp_notification_channel_description" translatable="false">knutice inapp notification channel</string>
<string name="inapp_notificaiton_channel_name">KNUTICE In-app Notice</string>
<string name="oss_notice">Open Source License</string>
</resources>
Loading