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

add rateUsNotitfication #103

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {


group = "com.smallcloud"
version = getVersionString("1.2.14")
version = getVersionString("1.2.15")

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.smallcloud.refactai.notifications

import com.intellij.ide.BrowserUtil
import com.intellij.ide.actions.ShowSettingsUtilImpl
import com.intellij.notification.Notification
import com.intellij.notification.NotificationAction
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationListener.Adapter
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor
Expand All @@ -15,6 +17,7 @@ import com.intellij.openapi.keymap.KeymapUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.util.concurrency.AppExecutorUtil
import com.smallcloud.refactai.PluginState
import com.smallcloud.refactai.RefactAIBundle
import com.smallcloud.refactai.Resources
Expand All @@ -23,15 +26,23 @@ import com.smallcloud.refactai.account.login
import com.smallcloud.refactai.panes.RefactAIToolboxPaneFactory
import com.smallcloud.refactai.privacy.Privacy
import com.smallcloud.refactai.privacy.PrivacyChangesNotifier
import com.smallcloud.refactai.settings.AppSettingsState.Companion.acceptedCompletionCounter
import com.smallcloud.refactai.utils.getLastUsedProject
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import java.net.URL
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import javax.swing.event.HyperlinkEvent
import com.smallcloud.refactai.io.InferenceGlobalContext.Companion.instance as InferenceGlobalContext
import com.smallcloud.refactai.lsp.LSPProcessHolder.Companion.instance as LSPProcessHolder
import com.smallcloud.refactai.privacy.PrivacyService.Companion.instance as PrivacyServiceInstance
import com.smallcloud.refactai.settings.AppSettingsState.Companion.instance as AppSettingsState

private var lastNotification: Notification? = null
private var lastRegularNotification: Notification? = null
private var lastRateUsNotification: Notification? = null
private var rateUsFuture: Future<*>? = null
private fun removeLastNotification() {
lastNotification?.apply {
expire()
Expand Down Expand Up @@ -72,6 +83,39 @@ fun startup() {
}

}, PluginState.instance)

if (!AppSettingsState.rateUsNotification) {
rateUsFuture = AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(
{
if (acceptedCompletionCounter.get() >= 30) {
emitRateUs()
}
if (AppSettingsState.rateUsNotification) {
rateUsFuture?.cancel(true)
}
}, 2, 2, TimeUnit.MINUTES)
}
}

fun emitRateUs() {
if (lastRateUsNotification != null) {
return
}
val project = getLastUsedProject()
val notification = NotificationGroupManager.getInstance().getNotificationGroup("Refact AI Notification Group")
.createNotification(Resources.titleStr, RefactAIBundle.message("notifications.rateUs"),
NotificationType.INFORMATION)
.setListener(object: Adapter() {
override fun hyperlinkActivated(notification: Notification, event: HyperlinkEvent) {
val url: URL = event.url
BrowserUtil.browse(url)
AppSettingsState.rateUsNotification = true
notification.expire()
}
})
notification.icon = Resources.Icons.LOGO_RED_16x16
notification.notify(project)
lastRateUsNotification = notification
}

private fun getVirtualFile(editor: Editor): VirtualFile? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import com.smallcloud.refactai.account.AccountManagerChangedNotifier
import com.smallcloud.refactai.io.InferenceGlobalContextChangedNotifier
import com.smallcloud.refactai.lsp.LSPProcessHolderChangedNotifier
import java.net.URI
import java.util.concurrent.atomic.AtomicInteger
import com.smallcloud.refactai.account.AccountManager.Companion.instance as AccountManager


/**
* Supports storing the application settings in a persistent way.
* The [State] and [Storage] annotations define the name of the data and the file name where
Expand Down Expand Up @@ -45,6 +45,7 @@ class AppSettingsState : PersistentStateComponent<AppSettingsState> {
var xDebugLSPPort: Int? = null
var longthinkModel: String? = null
var stagingVersion: String = ""
var rateUsNotification: Boolean = false

@Transient
private val messageBus: MessageBus = ApplicationManager.getApplication().messageBus
Expand Down Expand Up @@ -133,6 +134,8 @@ class AppSettingsState : PersistentStateComponent<AppSettingsState> {
@JvmStatic
val instance: AppSettingsState
get() = ApplicationManager.getApplication().getService(AppSettingsState::class.java)

val acceptedCompletionCounter = AtomicInteger(0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.util.concurrency.AppExecutorUtil
import com.smallcloud.refactai.Resources.defaultReportUrlSuffix
import com.smallcloud.refactai.Resources.defaultSnippetAcceptedUrlSuffix
import com.smallcloud.refactai.io.sendRequest
import com.smallcloud.refactai.settings.AppSettingsState.Companion.acceptedCompletionCounter
import com.smallcloud.refactai.lsp.LSPProcessHolder.Companion.instance as LSPProcessHolder


Expand Down Expand Up @@ -84,6 +85,7 @@ class UsageStats: Disposable {
if (!success) {
throw Exception(json.get("human_readable_message").asString)
}
acceptedCompletionCounter.incrementAndGet()
} catch (e: Exception) {
Logger.getInstance(UsageStats::class.java).warn("report to $url failed: $e")
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/bundles/RefactAI.properties
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ notifications.disableAccess=Disable Access
notifications.enableAccess=Enable {0}
notifications.filePrivacy=File privacy: {0}
notifications.selfHostedIsEnabled=Self-hosted mode is enabled
notifications.rateUs=Do you like Refact.ai? <a href="https://www.smallcloud.ai/ratejb">Give us 5 stars</a> and help spread the word about the product we passionately work on.

updateChecker.update=Update
updateChecker.newVersionIsAvailable=New version {0} is available! Let's try it!
Expand Down
Loading