Skip to content

Commit

Permalink
Merge pull request #37 from AKORA-Studios/AskForAppstoreReview
Browse files Browse the repository at this point in the history
AskForAppstoreReview
  • Loading branch information
DragonCat4012 authored Jun 24, 2023
2 parents b5a30d5 + 7650035 commit f86d5c2
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 30 deletions.
10 changes: 9 additions & 1 deletion Calq.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
F10E74F32A41E6C1004C7863 /* WhatsNewScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = F10E74F22A41E6C1004C7863 /* WhatsNewScreen.swift */; };
F1138E5129958878008E7FD8 /* DocumentPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1138E5029958878008E7FD8 /* DocumentPicker.swift */; };
F1138E5529959677008E7FD8 /* GradeListScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1138E5429959677008E7FD8 /* GradeListScreen.swift */; };
F14140462A47639E007E4648 /* TabViewVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = F14140452A47639E007E4648 /* TabViewVM.swift */; };
F14140482A476408007E4648 /* AppStoreRating.swift in Sources */ = {isa = PBXBuildFile; fileRef = F14140472A476408007E4648 /* AppStoreRating.swift */; };
F17F443D298C4DA7000A3993 /* CalqApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = F17F443C298C4DA7000A3993 /* CalqApp.swift */; };
F17F443F298C4DCE000A3993 /* OverviewScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = F17F443E298C4DCE000A3993 /* OverviewScreen.swift */; };
F17F4442298C4DDF000A3993 /* TabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F17F4441298C4DDF000A3993 /* TabView.swift */; };
Expand Down Expand Up @@ -153,6 +155,8 @@
F10E74F22A41E6C1004C7863 /* WhatsNewScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WhatsNewScreen.swift; sourceTree = "<group>"; };
F1138E5029958878008E7FD8 /* DocumentPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentPicker.swift; sourceTree = "<group>"; };
F1138E5429959677008E7FD8 /* GradeListScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradeListScreen.swift; sourceTree = "<group>"; };
F14140452A47639E007E4648 /* TabViewVM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabViewVM.swift; sourceTree = "<group>"; };
F14140472A476408007E4648 /* AppStoreRating.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppStoreRating.swift; sourceTree = "<group>"; };
F17F443C298C4DA7000A3993 /* CalqApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalqApp.swift; sourceTree = "<group>"; };
F17F443E298C4DCE000A3993 /* OverviewScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverviewScreen.swift; sourceTree = "<group>"; };
F17F4441298C4DDF000A3993 /* TabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -284,7 +288,6 @@
children = (
F19F8E4429F18E03006142B4 /* Localizable.strings */,
F17F443C298C4DA7000A3993 /* CalqApp.swift */,
F17F4441298C4DDF000A3993 /* TabView.swift */,
F17F4440298C4DD2000A3993 /* Screens */,
DD37556D279F3D90005DCB83 /* CalqRelease.entitlements */,
DD4F97B427524F7000D3CC57 /* CalqDebug.entitlements */,
Expand All @@ -310,6 +313,7 @@
F1B2732329B5F53900E841F4 /* ButtonStyle.swift */,
F1A53CFF2A196BFF0045C5B0 /* ListBackgroundModifier.swift */,
F1A7102C2A43757D00255F10 /* CardView.swift */,
F14140472A476408007E4648 /* AppStoreRating.swift */,
);
path = lib;
sourceTree = "<group>";
Expand Down Expand Up @@ -380,6 +384,8 @@
F17F4440298C4DD2000A3993 /* Screens */ = {
isa = PBXGroup;
children = (
F17F4441298C4DDF000A3993 /* TabView.swift */,
F14140452A47639E007E4648 /* TabViewVM.swift */,
F1E8BFE1299004AA00486A6D /* Overview */,
F10D66B0298D57AA00993237 /* ExamScreen */,
F1138E5229959627008E7FD8 /* SettingsScreen */,
Expand Down Expand Up @@ -649,6 +655,7 @@
F17F4444298C4E7D000A3993 /* SettingsScreen.swift in Sources */,
F1AAEF292A1BC1E5000CD044 /* Mapping Model 1.xcmappingmodel in Sources */,
F17F445E298C59B3000A3993 /* ColorExtension.swift in Sources */,
F14140462A47639E007E4648 /* TabViewVM.swift in Sources */,
F1B0B1682996489900F3D1FD /* EditGradeScreen.swift in Sources */,
F10D66B2298D57BD00993237 /* BlockView.swift in Sources */,
F1A772412A1795C6004B8DCC /* UserSubject+CoreDataClass.swift in Sources */,
Expand Down Expand Up @@ -683,6 +690,7 @@
F1A53D032A196C220045C5B0 /* WeightViewmodel.swift in Sources */,
F1A772392A1795C6004B8DCC /* AppSettings+CoreDataClass.swift in Sources */,
F1A53D002A196BFF0045C5B0 /* ListBackgroundModifier.swift in Sources */,
F14140482A476408007E4648 /* AppStoreRating.swift in Sources */,
F1B0B16C299648C400F3D1FD /* GradeTableOverviewScreen.swift in Sources */,
F1A772082A17940B004B8DCC /* MigrationPolicy.swift in Sources */,
F1BE91C22A41DF2A00E85494 /* SubjectListVM.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Calq/CalqApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct Calq: App {
.onAppear {
UserDefaults.standard.register(defaults: [UD_firstLaunchKey: true])
UserDefaults.standard.register(defaults: [UD_lastVersion: "0.0.0"])
UserDefaults.standard.register(defaults: [UD_lastAskedForeReview: "\(Date().timeIntervalSince1970)"])
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Calq/Screens/Overview/OverviewVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class OverViewViewModel: ObservableObject {
averagePercent = Util.generalAverage() / 15
generalAverage = Util.generalAverage()
gradeText = grade()

if shouldAskForReview() { // show appstore review popup
askForReview()
}
}

func grade() -> String {
Expand Down
29 changes: 0 additions & 29 deletions Calq/TabView.swift → Calq/Screens/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,4 @@ struct TabbarView: View {
.tabItem {Image(systemName: "gearshape.fill")}
}
}

}

class TabVM: ObservableObject {
@Published var showOverlay = false
@Published var firstLaunch = false
@Published var lastVersion = false

func checkForSheets() {
firstLaunch = !UserDefaults.standard.bool(forKey: UD_firstLaunchKey)
lastVersion = Util.checkIfNewVersion()
showOverlay = firstLaunch || lastVersion

if lastVersion {
UserDefaults.standard.set(appVersion, forKey: UD_lastVersion)
}
}

func showedFirstlaunch() {
showOverlay = false
firstLaunch = false
UserDefaults.standard.set(true, forKey: UD_firstLaunchKey)
}

func showedNewVersion() {
showOverlay = false
lastVersion = false
UserDefaults.standard.set(appVersion, forKey: UD_lastVersion)
}
}
36 changes: 36 additions & 0 deletions Calq/Screens/TabViewVM.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// TabViewVM.swift
// Calq
//
// Created by Kiara on 24.06.23.
//

import SwiftUI

class TabVM: ObservableObject {
@Published var showOverlay = false
@Published var firstLaunch = false
@Published var lastVersion = false

func checkForSheets() {
firstLaunch = !UserDefaults.standard.bool(forKey: UD_firstLaunchKey)
lastVersion = Util.checkIfNewVersion()
showOverlay = firstLaunch || lastVersion

if lastVersion {
UserDefaults.standard.set(appVersion, forKey: UD_lastVersion)
}
}

func showedFirstlaunch() {
showOverlay = false
firstLaunch = false
UserDefaults.standard.set(true, forKey: UD_firstLaunchKey)
}

func showedNewVersion() {
showOverlay = false
lastVersion = false
UserDefaults.standard.set(appVersion, forKey: UD_lastVersion)
}
}
35 changes: 35 additions & 0 deletions Calq/lib/AppStoreRating.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// AppStoreRating.swift
// Calq
//
// Created by Kiara on 24.06.23.
//

import StoreKit

func shouldAskForReview() -> Bool {
if Util.getAllSubjects().count < 8 { return false }

let nowTimestamp = Int(Date().timeIntervalSince1970)
let lastTimestamp = Int(UserDefaults.standard.string(forKey: UD_lastAskedForeReview) ?? String(nowTimestamp)) ?? 0
let timeIntervall = 60 * 60 * 24 * 7 * 4 * 3 // around 3 months

if lastTimestamp + timeIntervall < nowTimestamp {
UserDefaults.standard.set(nowTimestamp, forKey: UD_lastAskedForeReview)
return true
}

return false
}

func askForReview() {
guard let scene = UIApplication.shared.foregroundActiveScene else { return }
SKStoreReviewController.requestReview(in: scene)
}

extension UIApplication {
var foregroundActiveScene: UIWindowScene? {
connectedScenes
.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene
}
}
1 change: 1 addition & 0 deletions Calq/lib/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum ModelKit {
let UD_firstLaunchKey = "notFirstLaunch"
let UD_primaryType = "primaryGradeType"
let UD_lastVersion = "lastAppVersion"
let UD_lastAskedForeReview = "lastAskedForReview"

let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "?.?.?"
let buildVersion = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "?.?.?"
Expand Down

0 comments on commit f86d5c2

Please sign in to comment.