Skip to content

Commit

Permalink
Add preferences for storage options via dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
profiluefter committed Mar 13, 2021
1 parent 85b04cd commit 4f582fc
Show file tree
Hide file tree
Showing 39 changed files with 181 additions and 23 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* file name provider only executed on app launch
* overflow menu settings icon white on white
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0'
implementation 'androidx.preference:preference-ktx:1.1.1'
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
}
14 changes: 10 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Notian">
<activity
android:name=".activities.NoteEditorActivity"
android:windowSoftInputMode="adjustResize" />
<activity android:name=".activities.NoteDetailsActivity" />
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.NoteDetailsActivity"
android:label="@string/note_action_details"/>
<activity
android:name=".activities.NoteEditorActivity"
android:label="@string/note_action_edit"
android:windowSoftInputMode="adjustResize" />
<activity
android:name=".activities.PreferenceActivity"
android:label="@string/preferences"/>
</application>

</manifest>
45 changes: 29 additions & 16 deletions app/src/main/java/me/profiluefter/profinote/HiltModule.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
package me.profiluefter.profinote

import dagger.Binds
import android.content.Context
import androidx.preference.PreferenceManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import me.profiluefter.profinote.data.BinarySerializer
import me.profiluefter.profinote.data.PrivateFileStorage
import me.profiluefter.profinote.data.Serializer
import me.profiluefter.profinote.data.Storage
import me.profiluefter.profinote.data.*
import javax.inject.Named
import javax.inject.Provider

@Module
@InstallIn(SingletonComponent::class)
abstract class BindingModule {
@Binds
abstract fun bindStorage(impl: PrivateFileStorage): Storage
object PreferenceBasedModule {
@Provides
@Named("fileName")
fun storageLocation(@ApplicationContext context: Context): String =
PreferenceManager.getDefaultSharedPreferences(context).getString("fileName", "notes.bin")!!

@Binds
abstract fun bindSerializer(impl: BinarySerializer): Serializer
}
@Provides
fun serializerBinding(
@ApplicationContext context: Context,
binary: Provider<BinarySerializer>,
csv: Provider<CSVSerializer>
): Serializer = when (PreferenceManager.getDefaultSharedPreferences(context)
.getString("storageFormat", "binary")) {
"binary" -> binary.get()
"CSV" -> csv.get()
else -> null!!
}

@Module
@InstallIn(SingletonComponent::class)
object ConstantsModule {
@Provides
@Named("fileName")
fun storageLocation(): String = "notes.bin"
fun storageBinding(
@ApplicationContext context: Context,
private: Provider<PrivateFileStorage>
): Storage = when (PreferenceManager.getDefaultSharedPreferences(context)
.getString("storageLocation", "privateFile")) {
"privateFile" -> private.get()
else -> null!!
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.profiluefter.profinote.activities

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.Menu
Expand All @@ -8,6 +9,7 @@ import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.MenuBuilder
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -53,8 +55,14 @@ class MainActivity : AppCompatActivity() {
)
}

@SuppressLint("RestrictedApi") // https://stackoverflow.com/q/48607853
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)

if(menu is MenuBuilder) {
menu.setOptionalIconsVisible(true)
}

return super.onCreateOptionsMenu(menu)
}

Expand Down Expand Up @@ -88,6 +96,10 @@ class MainActivity : AppCompatActivity() {
snackbar.show()
}

fun onSettings(item: MenuItem) {
startActivity(Intent(this, PreferenceActivity::class.java))
}

fun onNewNote(view: View) = onNewNote()

private fun onNewNote() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.profiluefter.profinote.activities

import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity

class PreferenceActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager
.beginTransaction()
.replace(android.R.id.content, PreferenceFragment())
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> finish()
else -> return super.onOptionsItemSelected(item)
}
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.profiluefter.profinote.activities

import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import me.profiluefter.profinote.R

class PreferenceFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import me.profiluefter.profinote.data.Serializer
import javax.inject.Inject
import javax.inject.Provider

private const val TAG = "MainActivityViewModel"

@HiltViewModel
class MainActivityViewModel @Inject constructor(private val serializer: Serializer) : ViewModel() {
class MainActivityViewModel @Inject constructor(private val serializer: Provider<Serializer>) : ViewModel() {
val notes: MutableLiveData<List<Note>> by lazy {
MutableLiveData<List<Note>>().also {
it.value = listOf()
Expand All @@ -23,7 +24,7 @@ class MainActivityViewModel @Inject constructor(private val serializer: Serializ
private fun loadNotes(liveData: MutableLiveData<List<Note>>) {
Log.i(TAG, "Loading notes...")
viewModelScope.launch {
val existingNotes = serializer.load()
val existingNotes = serializer.get().load()
liveData.postValue((liveData.value!! + existingNotes).sorted())
}
}
Expand All @@ -36,7 +37,7 @@ class MainActivityViewModel @Inject constructor(private val serializer: Serializ
fun saveNotes() {
Log.i(TAG, "Saving notes...")
viewModelScope.launch {
serializer.save(notes.value!!)
serializer.get().save(notes.value!!)
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/baseline_settings_20.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="20dp"
android:height="20dp"
android:viewportWidth="20"
android:viewportHeight="20"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M15.95,10.78c0.03,-0.25 0.05,-0.51 0.05,-0.78s-0.02,-0.53 -0.06,-0.78l1.69,-1.32c0.15,-0.12 0.19,-0.34 0.1,-0.51l-1.6,-2.77c-0.1,-0.18 -0.31,-0.24 -0.49,-0.18l-1.99,0.8c-0.42,-0.32 -0.86,-0.58 -1.35,-0.78L12,2.34c-0.03,-0.2 -0.2,-0.34 -0.4,-0.34H8.4c-0.2,0 -0.36,0.14 -0.39,0.34l-0.3,2.12c-0.49,0.2 -0.94,0.47 -1.35,0.78l-1.99,-0.8c-0.18,-0.07 -0.39,0 -0.49,0.18l-1.6,2.77c-0.1,0.18 -0.06,0.39 0.1,0.51l1.69,1.32c-0.04,0.25 -0.07,0.52 -0.07,0.78s0.02,0.53 0.06,0.78L2.37,12.1c-0.15,0.12 -0.19,0.34 -0.1,0.51l1.6,2.77c0.1,0.18 0.31,0.24 0.49,0.18l1.99,-0.8c0.42,0.32 0.86,0.58 1.35,0.78l0.3,2.12c0.04,0.2 0.2,0.34 0.4,0.34h3.2c0.2,0 0.37,-0.14 0.39,-0.34l0.3,-2.12c0.49,-0.2 0.94,-0.47 1.35,-0.78l1.99,0.8c0.18,0.07 0.39,0 0.49,-0.18l1.6,-2.77c0.1,-0.18 0.06,-0.39 -0.1,-0.51l-1.67,-1.32zM10,13c-1.65,0 -3,-1.35 -3,-3s1.35,-3 3,-3 3,1.35 3,3 -1.35,3 -3,3z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/baseline_settings_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
android:onClick="onSaveNotes"
android:title="@string/save_notes"
app:showAsAction="ifRoom" />
<item
android:icon="@drawable/baseline_settings_24"
android:onClick="onSettings"
android:title="@string/preferences"
app:showAsAction="never" />
</menu>
17 changes: 17 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="storage_format_name">
<item>@string/storage_format_csv</item>
<item>@string/storage_format_binary</item>
</string-array>
<string-array name="storage_format_values">
<item>CSV</item>
<item>binary</item>
</string-array>
<string-array name="storage_location_name">
<item>@string/storage_location_private_file</item>
</string-array>
<string-array name="storage_location_values">
<item>privateFile</item>
</string-array>
</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@
<string name="note_deleted">Note deleted</string>
<string name="undo">Undo</string>
<string name="saved_notes">Saved notes</string>
<string name="storage_format_csv">CSV</string>
<string name="storage_format_binary">Binary</string>
<string name="storage_format_selection">Storage Format</string>
<!-- <string name="storage_format_description">The format that is used to save your notes</string>-->
<string name="storage_category">Storage</string>
<string name="storage_location_private_file">Internal Storage</string>
<string name="storage_location_selection">Storage Location</string>
<!-- <string name="storage_location_description">The location where your notes are saved</string>-->
<string name="preferences">Preferences</string>
<string name="storage_file_name">File Name</string>
</resources>
36 changes: 36 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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="@string/storage_category">

<ListPreference
android:defaultValue="binary"
android:entries="@array/storage_format_name"
android:entryValues="@array/storage_format_values"
android:icon="@drawable/baseline_save_24"
android:key="storageFormat"
android:title="@string/storage_format_selection"
app:useSimpleSummaryProvider="true" />

<ListPreference
android:defaultValue="privateFile"
android:entries="@array/storage_location_name"
android:entryValues="@array/storage_location_values"
android:icon="@drawable/baseline_save_24"
android:key="storageLocation"
android:title="@string/storage_location_selection"
app:useSimpleSummaryProvider="true" />

<EditTextPreference
android:defaultValue="notes.bin"
android:icon="@drawable/baseline_save_24"
android:key="fileName"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/storage_file_name"
app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

</PreferenceScreen>

0 comments on commit 4f582fc

Please sign in to comment.