Add a preferences screen
This commit is contained in:
@@ -39,13 +39,14 @@ dependencies {
|
||||
def nav_version = "2.5.3"
|
||||
def paging_version = "3.1.1"
|
||||
def fragment_version = "1.5.6"
|
||||
def preference_version = "1.2.0"
|
||||
|
||||
implementation 'androidx.core:core-ktx:1.9.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||
implementation 'com.google.android.material:material:1.8.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
|
||||
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
|
||||
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
|
||||
implementation 'com.squareup.moshi:moshi:1.14.0'
|
||||
implementation 'com.squareup.moshi:moshi-kotlin:1.14.0'
|
||||
implementation 'com.squareup.moshi:moshi-adapters:1.14.0'
|
||||
@@ -55,6 +56,7 @@ dependencies {
|
||||
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
|
||||
implementation "androidx.paging:paging-runtime:$paging_version"
|
||||
implementation "androidx.fragment:fragment-ktx:$fragment_version"
|
||||
implementation "androidx.preference:preference-ktx:$preference_version"
|
||||
|
||||
//implementation 'androidx.core:core-ktx:+'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import androidx.core.os.bundleOf
|
||||
@@ -7,19 +9,26 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentFactory
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
import fr.uca.iut.clfreville2.teaiswarm.fragment.RepositoryListFragment
|
||||
import fr.uca.iut.clfreville2.teaiswarm.fragment.SetupConfigFragment
|
||||
import fr.uca.iut.clfreville2.teaiswarm.model.Repository
|
||||
|
||||
const val REPOSITORY_OWNER = "repository_owner"
|
||||
const val REPOSITORY_NAME = "repository_name"
|
||||
const val USERNAME = "username"
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
supportFragmentManager.fragmentFactory = RepositoryListFragmentFactory { repo ->
|
||||
val preferences = getPreferences(Context.MODE_PRIVATE)
|
||||
supportFragmentManager.fragmentFactory = RepositoryListFragmentFactory(preferences) { repo ->
|
||||
adapterOnClick(repo)
|
||||
}
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
preferences.contains(USERNAME) || return
|
||||
setContentView(R.layout.activity_main)
|
||||
val nav = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
|
||||
nav.navController.navigate(R.id.repository_list_fragment)
|
||||
}
|
||||
|
||||
private fun adapterOnClick(repository: Repository) {
|
||||
@@ -32,12 +41,20 @@ class MainActivity : AppCompatActivity() {
|
||||
nav.navController.navigate(R.id.activity_list_fragment, bundle)
|
||||
}
|
||||
|
||||
class RepositoryListFragmentFactory(private val onClick: (Repository) -> Unit) : FragmentFactory() {
|
||||
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
|
||||
if (className == RepositoryListFragment::class.java.name) {
|
||||
return RepositoryListFragment("clement.freville2", onClick);
|
||||
class RepositoryListFragmentFactory(
|
||||
private val preferences: SharedPreferences,
|
||||
private val onClick: (Repository) -> Unit
|
||||
) : FragmentFactory() {
|
||||
override fun instantiate(classLoader: ClassLoader, className: String): Fragment =
|
||||
when (className) {
|
||||
RepositoryListFragment::class.java.name -> RepositoryListFragment(
|
||||
preferences.getString(
|
||||
USERNAME,
|
||||
null
|
||||
)!!, onClick
|
||||
)
|
||||
SetupConfigFragment::class.java.name -> SetupConfigFragment(preferences)
|
||||
else -> super.instantiate(classLoader, className)
|
||||
}
|
||||
return super.instantiate(classLoader, className)
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import fr.uca.iut.clfreville2.teaiswarm.R
|
||||
|
||||
class PreferencesFragment : PreferenceFragmentCompat() {
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) =
|
||||
setPreferencesFromResource(R.xml.preferences, rootKey)
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
package fr.uca.iut.clfreville2.teaiswarm.fragment
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import androidx.core.content.edit
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.navigation.findNavController
|
||||
import fr.uca.iut.clfreville2.teaiswarm.R
|
||||
import fr.uca.iut.clfreville2.teaiswarm.USERNAME
|
||||
|
||||
class SetupConfigFragment(private val preferences: SharedPreferences) : Fragment(R.layout.setup_config) {
|
||||
|
||||
private lateinit var usernameInput: EditText
|
||||
private lateinit var confirmButton: Button
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
usernameInput = view.findViewById(R.id.username_input)
|
||||
confirmButton = view.findViewById(R.id.configure_button)
|
||||
confirmButton.setOnClickListener {
|
||||
preferences.edit {
|
||||
putString(USERNAME, usernameInput.text.toString())
|
||||
}
|
||||
view.findNavController().navigate(R.id.repository_list_fragment)
|
||||
}
|
||||
}
|
||||
}
|
23
app/src/main/res/layout/setup_config.xml
Normal file
23
app/src/main/res/layout/setup_config.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/username_input"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintBottom_toTopOf="@+id/configure_button"
|
||||
android:autofillHints="" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/configure_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/configure"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@@ -2,10 +2,14 @@
|
||||
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/nav_graph"
|
||||
app:startDestination="@id/repository_ist_fragment">
|
||||
app:startDestination="@id/setup_config_fragment">
|
||||
|
||||
<fragment
|
||||
android:id="@+id/repository_ist_fragment"
|
||||
android:id="@+id/setup_config_fragment"
|
||||
android:name="fr.uca.iut.clfreville2.teaiswarm.fragment.SetupConfigFragment"
|
||||
android:label="SetupConfigFragment" />
|
||||
<fragment
|
||||
android:id="@+id/repository_list_fragment"
|
||||
android:name="fr.uca.iut.clfreville2.teaiswarm.fragment.RepositoryListFragment"
|
||||
android:label="RepositoryListFragment" >
|
||||
<action
|
||||
@@ -16,4 +20,8 @@
|
||||
android:id="@+id/activity_list_fragment"
|
||||
android:name="fr.uca.iut.clfreville2.teaiswarm.fragment.ActivityListFragment"
|
||||
android:label="ActivityListFragment" />
|
||||
</navigation>
|
||||
<fragment
|
||||
android:id="@+id/preferences_fragment"
|
||||
android:name="fr.uca.iut.clfreville2.teaiswarm.fragment.PreferencesFragment"
|
||||
android:label="PreferencesFragment" />
|
||||
</navigation>
|
||||
|
@@ -11,4 +11,6 @@
|
||||
<string name="language">Language: %s</string>
|
||||
<string name="loading">Loading</string>
|
||||
<string name="unknown">Unknown</string>
|
||||
<string name="username">Username</string>
|
||||
<string name="configure">Configure</string>
|
||||
</resources>
|
9
app/src/main/res/xml/preferences.xml
Normal file
9
app/src/main/res/xml/preferences.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.preference.PreferenceScreen
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<EditTextPreference
|
||||
app:key="username"
|
||||
app:title="@string/username" />
|
||||
|
||||
</androidx.preference.PreferenceScreen>
|
Reference in New Issue
Block a user