Skip to content

Commit

Permalink
chore(*): apply pr recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
humblerookie committed Mar 9, 2024
1 parent 39bb728 commit bfcef72
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 153 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/pr_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ on:
pull_request:
branches: [ main ]

env:
VERSION: ${{ vars.VERSION }}
DESKTOP_VERSION: ${{ vars.DESKTOP_VERSION }}
VERSION_CODE: ${{ vars.VERSION_CODE }}
IS_DEBUG: ${{ vars.IS_DEBUG }}
SENTRY_ORG: ${{ vars.SENTRY_ORG }}
SENTRY_PROJECT: ${{ vars.SENTRY_PROJECT }}
SENTRY_ENVIRONMENT: ${{ vars.SENTRY_ENVIRONMENT }}

concurrency:
group: pr-check-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
run-checks:
runs-on: macos-latest
environment: development
steps:

- name: Checkout Branch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,74 +23,72 @@ import me.tatarka.inject.annotations.Inject

@Inject
actual class NotificationService(
private val context: Context,
private val userDataStore: UserDataStore,
private val permissionHandler: PermissionHandler
private val context: Context,
private val userDataStore: UserDataStore,
private val permissionHandler: PermissionHandler
) {

private val channelId = "MATCH_ORDERS"
private val channelId = "MATCH_ORDERS"

@SuppressLint("MissingPermission")
actual fun notify(items: List<NotificationModel>) {
with(NotificationManagerCompat.from(context)) {
permissionHandler.hasPermission(PermissionType.NOTIFICATION) { hasPermission ->
if (hasPermission) {
createNotificationChannel()
createNotification(items).forEach {
val id = userDataStore.notificationId + 1
userDataStore.notificationId = id
notify(id, it)
}
}
}
@SuppressLint("MissingPermission")
actual fun notify(items: List<NotificationModel>) {
with(NotificationManagerCompat.from(context)) {
permissionHandler.hasPermission(PermissionType.NOTIFICATION) { hasPermission ->
if (hasPermission) {
createNotificationChannel()
createNotification(items).forEach {
val id = userDataStore.notificationId + 1
userDataStore.notificationId = id
notify(id, it)
}
}
}
}
}

private fun createNotification(items: List<NotificationModel>): List<Notification> {
val intent =
Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(Constants.APP_SCHEME)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
private fun createNotification(items: List<NotificationModel>): List<Notification> {
val intent =
Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(Constants.APP_SCHEME)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}

val pendingIntent: PendingIntent =
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val pendingIntent: PendingIntent =
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

val uuid = UUID.randomUUID().toString()
return buildList {
items.forEach {
add(
NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(it.title)
.setContentText(it.message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setGroup(uuid)
.build()
)
}
}
val uuid = UUID.randomUUID().toString()
return buildList {
items.forEach {
add(
NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(it.title)
.setContentText(it.message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setGroup(uuid)
.build()
)
}
}
}

private fun createNotificationChannel() {
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private fun createNotificationChannel() {
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

if (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
notificationManager.getNotificationChannel(channelId) == null
) {
val name = getLocaleStrings().matchNotificationTitle
val descriptionText = getLocaleStrings().matchNotificationDescription
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel =
NotificationChannel(channelId, name, importance).apply {
description = descriptionText
}
// Register the channel with the system.
notificationManager.createNotificationChannel(channel)
}
if (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
notificationManager.getNotificationChannel(channelId) == null
) {
val name = getLocaleStrings().matchNotificationTitle
val descriptionText = getLocaleStrings().matchNotificationDescription
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel =
NotificationChannel(channelId, name, importance).apply { description = descriptionText }
// Register the channel with the system.
notificationManager.createNotificationChannel(channel)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ import me.tatarka.inject.annotations.Inject
@Inject
@AppScope
actual class PlatformScheduler(private val context: Context) {
private lateinit var filterOrdersRequest: PeriodicWorkRequest
private lateinit var resetOrdersRequest: PeriodicWorkRequest
private val constraints =
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
private val refreshOrdersKey = "RefreshOrdersTask"
private val resetOrdersKey = "ResetOrdersTask"
private lateinit var filterOrdersRequest: PeriodicWorkRequest
private lateinit var resetOrdersRequest: PeriodicWorkRequest
private val constraints =
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
private val refreshOrdersKey = "RefreshOrdersTask"
private val resetOrdersKey = "ResetOrdersTask"

actual fun schedule(executor: RequestExecutor) {
// Ignore Parameter on android since we rely on inject
with(WorkManager.getInstance(context)) {
filterOrdersRequest =
PeriodicWorkRequest.Builder(
RefreshOrdersTask::class.java,
INTERVAL_MINUTES,
TimeUnit.MINUTES,
)
.setConstraints(constraints)
.build()
enqueueUniquePeriodicWork(refreshOrdersKey, CANCEL_AND_REENQUEUE, filterOrdersRequest)
resetOrdersRequest =
PeriodicWorkRequest.Builder(
ResetOrdersTask::class.java,
INTERVAL_DAYS,
TimeUnit.DAYS,
)
.build()
enqueueUniquePeriodicWork(resetOrdersKey, CANCEL_AND_REENQUEUE, resetOrdersRequest)
}
actual fun schedule(executor: RequestExecutor) {
// Ignore Parameter on android since we rely on inject
with(WorkManager.getInstance(context)) {
filterOrdersRequest =
PeriodicWorkRequest.Builder(
RefreshOrdersTask::class.java,
INTERVAL_MINUTES,
TimeUnit.MINUTES,
)
.setConstraints(constraints)
.build()
enqueueUniquePeriodicWork(refreshOrdersKey, CANCEL_AND_REENQUEUE, filterOrdersRequest)
resetOrdersRequest =
PeriodicWorkRequest.Builder(
ResetOrdersTask::class.java,
INTERVAL_DAYS,
TimeUnit.DAYS,
)
.build()
enqueueUniquePeriodicWork(resetOrdersKey, CANCEL_AND_REENQUEUE, resetOrdersRequest)
}
}

actual fun cancel() {
if (::filterOrdersRequest.isInitialized) {
with(WorkManager.getInstance(context)) {
cancelWorkById(filterOrdersRequest.id)
cancelWorkById(resetOrdersRequest.id)
}
}
actual fun cancel() {
if (::filterOrdersRequest.isInitialized) {
with(WorkManager.getInstance(context)) {
cancelWorkById(filterOrdersRequest.id)
cancelWorkById(resetOrdersRequest.id)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,76 +23,76 @@ import kotlinx.coroutines.sync.Mutex

abstract class BasePresenter<S, E>(dispatcherProvider: DispatcherProvider) {

fun dispatchEvent(event: E) = onEvent(event)
fun dispatchEvent(event: E) = onEvent(event)

protected abstract fun onEvent(event: E)
protected abstract fun onEvent(event: E)

abstract fun initState(): S
abstract fun initState(): S

private val _state = MutableStateFlow(this.initState())
private val _state = MutableStateFlow(this.initState())

private val mutex = Mutex()
private val mutex = Mutex()

protected fun updateState(map: (S) -> S) = _state.update(map)
protected fun updateState(map: (S) -> S) = _state.update(map)

private val presenterScope: CoroutineScope =
CoroutineScope(
SupervisorJob() +
dispatcherProvider.main() +
CoroutineExceptionHandler { _, exception ->
logE("Coroutine threw $exception: \n${exception.stackTraceToString()}")
}
)

val state: StateFlow<S> =
_state.stateIn(
presenterScope,
SharingStarted.WhileSubscribed(5000),
this.initState(),
)

protected fun launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job = presenterScope.launch(context, start, block)

val currentState: S
get() = _state.value

val viewEvents
get() = _events.receiveAsFlow()

private val _events =
Channel<SideEffect>(
capacity = Int.MAX_VALUE,
onBufferOverflow = BufferOverflow.SUSPEND,
onUndeliveredElement = { logE("Missed view event $it") },
)

protected fun sideEffect(event: SideEffect) {
launch { _events.send(event) }
}

companion object {
val presenters = mutableMapOf<String, BasePresenter<*, *>>()

inline fun <reified T> getPresenter(key: String, factory: () -> T): T {
return (presenters[key] ?: factory()) as T
}

fun removeBinding(key: String) {
presenters.remove(key)?.onCleared()
private val presenterScope: CoroutineScope =
CoroutineScope(
SupervisorJob() +
dispatcherProvider.main() +
CoroutineExceptionHandler { _, exception ->
logE("Coroutine threw $exception: \n${exception.stackTraceToString()}")
}
)

val state: StateFlow<S> =
_state.stateIn(
presenterScope,
SharingStarted.WhileSubscribed(5000),
this.initState(),
)

protected fun launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job = presenterScope.launch(context, start, block)

val currentState: S
get() = _state.value

val viewEvents
get() = _events.receiveAsFlow()

private val _events =
Channel<SideEffect>(
capacity = Int.MAX_VALUE,
onBufferOverflow = BufferOverflow.SUSPEND,
onUndeliveredElement = { logE("Missed view event $it") },
)

protected fun sideEffect(event: SideEffect) {
launch { _events.send(event) }
}

companion object {
val presenters = mutableMapOf<String, BasePresenter<*, *>>()

inline fun <reified T> getPresenter(key: String, factory: () -> T): T {
return (presenters[key] ?: factory()) as T
}

fun bind(screen: PresenterScreen) {
if (!presenters.containsKey(screen.key)) {
presenters[screen.key] = this
}
fun removeBinding(key: String) {
presenters.remove(key)?.onCleared()
}
}

fun onCleared() {
presenterScope.cancel()
fun bind(screen: PresenterScreen) {
if (!presenters.containsKey(screen.key)) {
presenters[screen.key] = this
}
}

fun onCleared() {
presenterScope.cancel()
}
}

0 comments on commit bfcef72

Please sign in to comment.