Skip to content

Commit c562935

Browse files
committed
V2 update!!
- POWER TRAINING YEAHHH - Update checker! - Credits screen! - Bugfix for a crash!
1 parent 1351f1f commit c562935

File tree

16 files changed

+567
-145
lines changed

16 files changed

+567
-145
lines changed

src/app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ android {
1313
minSdk = 24
1414
targetSdk = 33
1515
versionCode = 1
16-
versionName = "1.0"
16+
versionName = "2.0"
1717

1818
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1919
}

src/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
4+
<uses-permission android:name="android.permission.INTERNET"/>
45

56
<application
67
android:allowBackup="true"
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import android.content.Context
2+
import android.os.AsyncTask
3+
import android.widget.Toast
4+
import org.json.JSONObject
5+
import java.io.BufferedReader
6+
import java.io.InputStreamReader
7+
import java.net.HttpURLConnection
8+
import java.net.URL
9+
import java.net.UnknownHostException
10+
11+
class GitHubReleaseChecker(private val context: Context, private val repositoryUrl: String) :
12+
AsyncTask<Void, Void, String>() {
13+
14+
override fun doInBackground(vararg params: Void?): String {
15+
try {
16+
val apiUrl = "$repositoryUrl/releases/latest"
17+
val url = URL(apiUrl)
18+
val connection = url.openConnection() as HttpURLConnection
19+
20+
val reader = BufferedReader(InputStreamReader(connection.inputStream))
21+
val stringBuilder = StringBuilder()
22+
var line: String?
23+
24+
while (reader.readLine().also { line = it } != null) {
25+
stringBuilder.append(line)
26+
}
27+
28+
reader.close()
29+
return stringBuilder.toString()
30+
} catch (e: UnknownHostException) {
31+
return "Network Error"
32+
} catch (e: Exception) {
33+
return "Error"
34+
}
35+
}
36+
37+
override fun onPostExecute(result: String) {
38+
super.onPostExecute(result)
39+
40+
when {
41+
result == "Network Error" -> return //showToast("Network error occurred.")
42+
result == "Error" -> return //showToast("An error occurred.")
43+
result == "" -> return
44+
else -> {
45+
try {
46+
val jsonObject = JSONObject(result)
47+
val tagName = jsonObject.getString("tag_name")
48+
val latestVersion = tagName.replace("v", "").trim()
49+
50+
if (isUpdateAvailable(latestVersion)) {
51+
showToast("Update available: $latestVersion")
52+
showToast("Go to GitHub to update!")
53+
} else {
54+
//showToast("You are using the latest version.")
55+
}
56+
} catch (e: Exception) {
57+
showToast("Error checking for updates.")
58+
}
59+
}
60+
}
61+
}
62+
63+
private fun isUpdateAvailable(latestVersion: String): Boolean {
64+
// Replace this with the current installed version of your app.
65+
val currentVersion = "2.0"
66+
67+
// Split the version strings into individual parts.
68+
val currentVersionParts = currentVersion.split(".")
69+
val latestVersionParts = latestVersion.split(".")
70+
71+
// Compare each part of the version numbers.
72+
for (i in 0 until minOf(currentVersionParts.size, latestVersionParts.size)) {
73+
val currentPart = currentVersionParts[i].toInt()
74+
val latestPart = latestVersionParts[i].toInt()
75+
76+
if (currentPart < latestPart) {
77+
return true
78+
} else if (currentPart > latestPart) {
79+
return false
80+
}
81+
}
82+
83+
// If all parts are equal, consider it up-to-date.
84+
return false
85+
}
86+
87+
private fun showToast(message: String) {
88+
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
89+
}
90+
}

src/app/src/main/java/com/helloyanis/rucoycalculator/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ class MainActivity : AppCompatActivity() {
3131
)
3232
setupActionBarWithNavController(navController, appBarConfiguration)
3333
navView.setupWithNavController(navController)
34+
3435
}
3536
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.helloyanis.rucoycalculator.ui.credits
2+
3+
import android.content.Intent
4+
import android.graphics.Color
5+
import android.net.Uri
6+
import android.os.Bundle
7+
import android.os.Handler
8+
import android.os.Looper
9+
import android.view.LayoutInflater
10+
import android.view.View
11+
import android.view.ViewGroup
12+
import androidx.fragment.app.Fragment
13+
import androidx.lifecycle.ViewModelProvider
14+
import com.helloyanis.rucoycalculator.databinding.CreditsBinding
15+
16+
class CreditsFragment : Fragment() {
17+
18+
private var _binding: CreditsBinding? = null
19+
private val binding get() = _binding!!
20+
21+
private val colors = listOf(
22+
Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA
23+
)
24+
private var currentIndex = 0
25+
private val handler = Handler(Looper.getMainLooper())
26+
private val delayMillis = 2000L // Change color every 1 second
27+
28+
override fun onCreateView(
29+
inflater: LayoutInflater,
30+
container: ViewGroup?,
31+
savedInstanceState: Bundle?
32+
): View {
33+
val creditsViewModel =
34+
ViewModelProvider(this).get(CreditsViewModel::class.java)
35+
36+
_binding = CreditsBinding.inflate(inflater, container, false)
37+
val root: View = binding.root
38+
39+
// Get the button view
40+
val supportMeButton = binding.supportMe
41+
42+
// Start the color-changing loop for the button
43+
startRainbowEffect(supportMeButton)
44+
45+
supportMeButton.setOnClickListener {
46+
val i = Intent(Intent.ACTION_VIEW, Uri.parse("https://ko-fi.com/helloyanis"))
47+
startActivity(i)
48+
}
49+
50+
val githubbutton = binding.github
51+
52+
githubbutton.setOnClickListener{
53+
val i = Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/helloyanis/rucoy-calculator"))
54+
startActivity(i)
55+
}
56+
57+
return root
58+
}
59+
60+
override fun onDestroyView() {
61+
super.onDestroyView()
62+
// Remove the handler callbacks when the view is destroyed
63+
handler.removeCallbacksAndMessages(null)
64+
_binding = null
65+
}
66+
67+
private fun startRainbowEffect(button: View) {
68+
handler.postDelayed(object : Runnable {
69+
override fun run() {
70+
// Change the button's background color
71+
button.setBackgroundColor(colors[currentIndex])
72+
73+
// Increment the index, or loop back to 0 if it reaches the end
74+
currentIndex = (currentIndex + 1) % colors.size
75+
76+
// Schedule the next color change
77+
handler.postDelayed(this, delayMillis)
78+
}
79+
}, delayMillis)
80+
}
81+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.helloyanis.rucoycalculator.ui.credits
2+
3+
import androidx.lifecycle.LiveData
4+
import androidx.lifecycle.MutableLiveData
5+
import androidx.lifecycle.ViewModel
6+
7+
class CreditsViewModel : ViewModel() {
8+
9+
}

src/app/src/main/java/com/helloyanis/rucoycalculator/ui/ptrain/PtrainFragment.kt

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/app/src/main/java/com/helloyanis/rucoycalculator/ui/ptrain/PtrainViewModel.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)