Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

implement settings screen #13

Merged
merged 16 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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: 2 additions & 0 deletions app/src/main/java/org/cosmicide/rewrite/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.cosmicide.rewrite.common.Prefs
import org.cosmicide.rewrite.util.FileUtil
import org.eclipse.tm4e.core.registry.IThemeSource
import java.io.File
Expand All @@ -30,6 +31,7 @@ class App : Application() {
super.onCreate()
DynamicColors.applyToActivitiesIfAvailable(this)
FileUtil.init(this)
Prefs.init(applicationContext)
indexFile = File(FileUtil.dataDir, INDEX_FILE_NAME)
scope.launch {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ fun CodeEditor.setLanguageTheme(language: EditorLanguage) {
* Sets the font and enables highlighting of the current line for the code editor.
*/
fun CodeEditor.setFont() {
typefaceText = ResourcesCompat.getCachedFont(context, R.font.noto_sans_mono)
typefaceText = ResourcesCompat.getFont(context, R.font.noto_sans_mono)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getCachedFont would be better here i think

Copy link
Collaborator Author

@aikrq aikrq Apr 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the getCachedFont method, the font will not be loaded since it is not cached.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It checks if its cached and sets it. atleast in emulator, it loads properly

Copy link
Collaborator Author

@aikrq aikrq Apr 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested on a real device? When using the getCachedFont() method on my device, the font simply didn't load.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it did work

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check last artifact (not pr's artifact)

isHighlightCurrentLine = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.cosmicide.build.BuildReporter
import org.cosmicide.project.Project
import org.cosmicide.rewrite.R
import org.cosmicide.rewrite.common.BaseBindingFragment
import org.cosmicide.rewrite.common.Prefs
import org.cosmicide.rewrite.compile.Compiler
import org.cosmicide.rewrite.databinding.FragmentCompileInfoBinding
import org.cosmicide.rewrite.extension.setFont
Expand Down Expand Up @@ -49,7 +50,7 @@ class CompileInfoFragment : BaseBindingFragment<FragmentCompileInfoBinding>() {
colorScheme = TextMateColorScheme.create(ThemeRegistry.getInstance())
setEditorLanguage(TextMateLanguage.create("source.build", true))
editable = false
setTextSize(16f)
setTextSize(Prefs.editorFontSize)
isLineNumberEnabled = false
isWordwrap = true
invalidate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.withContext
import org.cosmicide.project.Project
import org.cosmicide.rewrite.R
import org.cosmicide.rewrite.common.BaseBindingFragment
import org.cosmicide.rewrite.common.Prefs
import org.cosmicide.rewrite.databinding.FragmentEditorBinding
import org.cosmicide.rewrite.editor.JavaLanguage
import org.cosmicide.rewrite.editor.KotlinLanguage
Expand Down Expand Up @@ -116,7 +117,7 @@ class EditorFragment : BaseBindingFragment<FragmentEditorBinding>() {
}
}

binding.editor.setTextSize(20f)
binding.editor.setTextSize(Prefs.editorFontSize)
}

private fun setEditorLanguage() {
Expand Down Expand Up @@ -154,6 +155,10 @@ class EditorFragment : BaseBindingFragment<FragmentEditorBinding>() {
navigateToCompileInfoFragment()
true
}
R.id.action_settings -> {
navigateToSettingsFragment()
true
}

else -> false
}
Expand All @@ -168,6 +173,13 @@ class EditorFragment : BaseBindingFragment<FragmentEditorBinding>() {
.commit()
}

private fun navigateToSettingsFragment() {
parentFragmentManager.beginTransaction()
.replace(R.id.fragment_container, SettingsFragment())
.addToBackStack(null)
.commit()
}

private fun createTree(): Tree<DataSource<File>> {
return buildTree {
val rootDir = project.root
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.cosmicide.rewrite.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.recyclerview.widget.RecyclerView
import org.cosmicide.rewrite.R
import org.cosmicide.rewrite.BuildConfig

/**
* A [PreferenceFragmentCompat] subclass to display the preferences UI.
*/
class PreferencesFragment : PreferenceFragmentCompat() {

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)

findPreference<Preference>("version")?.run {
summary = BuildConfig.VERSION_NAME
}
}

override fun onCreateRecyclerView(inflater: LayoutInflater, parent: ViewGroup, savedInstanceState: Bundle?): RecyclerView {
val recyclerView = super.onCreateRecyclerView(inflater, parent, savedInstanceState)
recyclerView.layoutAnimation = AnimationUtils.loadLayoutAnimation(requireContext(), R.anim.preference_layout_fall_down)
return recyclerView
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlinx.coroutines.launch
import org.cosmicide.project.Project
import org.cosmicide.rewrite.R
import org.cosmicide.rewrite.common.BaseBindingFragment
import org.cosmicide.rewrite.common.Prefs
import org.cosmicide.rewrite.databinding.FragmentCompileInfoBinding
import org.cosmicide.rewrite.extension.setFont
import org.cosmicide.rewrite.util.ProjectHandler
Expand Down Expand Up @@ -70,7 +71,7 @@ class ProjectOutputFragment : BaseBindingFragment<FragmentCompileInfoBinding>()
setEditorLanguage(TextMateLanguage.create("source.build", false))
editable = false
isWordwrap = true
setTextSize(14f)
setTextSize(Prefs.editorFontSize)
setFont()
invalidate()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.cosmicide.rewrite.fragment

import android.os.Bundle
import android.view.View
import org.cosmicide.rewrite.common.BaseBindingFragment
import org.cosmicide.rewrite.databinding.FragmentSettingsBinding

/**
* Fragment for displaying settings screen.
*/
class SettingsFragment : BaseBindingFragment<FragmentSettingsBinding>() {

override fun getViewBinding() = FragmentSettingsBinding.inflate(layoutInflater)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/anim/preference_item_fall_down.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">

<translate
android:fromYDelta="15%"
android:interpolator="@android:interpolator/linear_out_slow_in"
android:toYDelta="0" />

<alpha
android:fromAlpha="0"
android:interpolator="@android:interpolator/linear_out_slow_in"
android:toAlpha="1" />

</set>
5 changes: 5 additions & 0 deletions app/src/main/res/anim/preference_layout_fall_down.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/preference_item_fall_down"
android:animationOrder="normal"
android:delay="5%" />
30 changes: 30 additions & 0 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionGroup="true"
tools:context=".fragment.SettingsFragment">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="@drawable/baseline_arrow_back_ios_24"
app:title="Settings" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.fragment.app.FragmentContainerView
android:id="@+id/settings_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="org.cosmicide.rewrite.fragment.PreferencesFragment"
android:transitionGroup="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values-sw360dp/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="config_materialPreferenceIconSpaceReserved">false</bool>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="java_version_entries">
<item>8</item>
<item>11</item>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about other version? For ex if you just want to use text blocks and want compatibility with Java 16, you can just use it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, i will add every java version from 8 to 17.

<item>17</item>
</string-array>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<style name="Theme.CosmicIDERewrite" parent="Base.Theme.CosmicIDERewrite">
<item name="android:enforceNavigationBarContrast">false</item>
<item name="android:fontFamily">@font/inter</item>
<item name="fontFamily">@font/inter</item>
</style>
</resources>
46 changes: 46 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory
android:title="Editor">

<SeekBarPreference
android:key="font_size"
android:title="Font Size"
android:summary="Set the font size for the editor"
android:defaultValue="14"
android:min="12"
android:max="22" />

</PreferenceCategory>

<PreferenceCategory
android:title="Compiler">

<SwitchPreference
android:key="use_fastjarfs"
android:title="Use fast implementation for Jar FS"
android:summary="This experimental mode may speed up compilation time, but use with caution"
android:defaultValue="false" />

<ListPreference
android:key="java_version"
android:title="Java Version"
android:summary="Select the version of Java to use for compilation"
android:entries="@array/java_version_entries"
android:entryValues="@array/java_version_entries"
android:defaultValue="17" />

</PreferenceCategory>

<PreferenceCategory
android:title="About">

<Preference
android:key="version"
android:title="Version" />

</PreferenceCategory>

</PreferenceScreen>
1 change: 1 addition & 0 deletions build-tools/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ android {


dependencies {
implementation(projects.common)
implementation(projects.project)
implementation(projects.util)
implementation(projects.kotlinc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.sun.tools.javac.api.JavacTool
import org.cosmicide.build.BuildReporter
import org.cosmicide.build.Task
import org.cosmicide.project.Project
import org.cosmicide.rewrite.common.Prefs
import org.cosmicide.rewrite.util.FileUtil
import java.io.File
import java.io.Writer
Expand Down Expand Up @@ -34,7 +35,7 @@ class JavaCompileTask(val project: Project) : Task {

override fun execute(reporter: BuildReporter) {
val output = File(project.binDir, "classes")
val version = "17"
val version = Prefs.compilerJavaVersion.toString()

try {
Files.createDirectories(output.toPath())
Expand All @@ -60,7 +61,6 @@ class JavaCompileTask(val project: Project) : Task {
return
}


fileManager.use { fm ->
fm.setLocation(StandardLocation.CLASS_OUTPUT, listOf(output))
fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, getSystemClasspath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.cosmicide.build.Task
import org.cosmicide.build.util.getSourceFiles
import org.cosmicide.build.util.getSystemClasspath
import org.cosmicide.project.Project
import org.cosmicide.rewrite.common.Prefs
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
Expand Down Expand Up @@ -41,7 +42,7 @@ class KotlinCompiler(val project: Project) : Task {
args.javaSourceRoots = kotlinSourceFiles.filter { it.extension == "java" }.map { it.absolutePath }.toTypedArray()
args.moduleName = project.name
args.pluginClasspaths = plugins
args.useFastJarFileSystem = true
args.useFastJarFileSystem = Prefs.useFastJarFs

val collector = createMessageCollector(reporter)

Expand Down
1 change: 1 addition & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.databinding:viewbinding:7.4.2")
api("androidx.preference:preference-ktx:1.2.0")
}
50 changes: 50 additions & 0 deletions common/src/main/java/org/cosmicide/rewrite/common/Prefs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.cosmicide.rewrite.common

import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager

/**
* A utility object to access shared preferences easily.
*/
object Prefs {
private lateinit var prefs: SharedPreferences

/**
* The font size selected by the user.
*/
val editorFontSize: Float
get() {
return try {
prefs.getString("font_size", "12")?.toFloat() ?: 12f
} catch (e: Exception) {
12f
}
}

/**
* The Java version selected by the user.
*/
val compilerJavaVersion: Int
get() {
return try {
prefs.getString("java_version", "17")?.toInt() ?: 17
} catch (e: Exception) {
17
}
}

/**
* The FastJarFs selected by user.
*/
val useFastJarFs: Boolean
get() = prefs.getBoolean("use_fastjarfs", false)

/**
* Initializes shared preferences.
* @param context The context of the application.
*/
fun init(context: Context) {
prefs = PreferenceManager.getDefaultSharedPreferences(context)
}
}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.defaults.buildfeatures.buildconfig=true