Skip to content

Commit

Permalink
Finished App
Browse files Browse the repository at this point in the history
  • Loading branch information
azzumw committed Feb 26, 2022
1 parent b2312ee commit cf5d300
Show file tree
Hide file tree
Showing 19 changed files with 1,454 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Affirmations/.idea/modules/Affirmations.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions CupCake/.idea/modules/CupCake.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions CupCake/.idea/modules/app/CupCake.app.iml

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions LunchTray/.idea/modules/LunchTray.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

206 changes: 206 additions & 0 deletions LunchTray/.idea/modules/app/LunchTray.app.iml

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions MarsPhotos/.idea/modules/MarsPhotos.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

214 changes: 214 additions & 0 deletions MarsPhotos/.idea/modules/app/MarsPhotos.app.iml

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions PrefDataStore/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions PrefDataStore/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ dependencies {
//navigation
implementation "androidx.navigation:navigation-fragment-ktx:2.4.1"
implementation "androidx.navigation:navigation-ui-ktx:2.4.1"

//data-store
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import android.os.Bundle
import android.view.*
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.asLiveData
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.prefdatastore.data.SettingsDataStore
import com.example.prefdatastore.databinding.FragmentLetterListBinding
import kotlinx.coroutines.launch


class LetterListFragment : Fragment() {

private lateinit var settingsDataStore: SettingsDataStore
private var _binding: FragmentLetterListBinding? = null

// This property is only valid between onCreateView and
Expand Down Expand Up @@ -39,6 +45,15 @@ class LetterListFragment : Fragment() {
// Sets the LayoutManager of the recyclerview
// On the first run of the app, it will be LinearLayoutManager
chooseLayout()

settingsDataStore = SettingsDataStore(requireContext())

//convert the preferenceFlow to Livedata using asLiveData().
// Attach an observer and pass in the viewLifecycleOwner as the owner.
settingsDataStore.preferenceFlow.asLiveData().observe(viewLifecycleOwner) {
isLinearLayoutManager = it
chooseLayout()
}
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand Down Expand Up @@ -80,6 +95,11 @@ class LetterListFragment : Fragment() {
R.id.action_switch_layout -> {
// Sets isLinearLayoutManager (a Boolean) to the opposite value
isLinearLayoutManager = !isLinearLayoutManager

// Launch a coroutine and write the layout setting in the preference Datastore
lifecycleScope.launch {
settingsDataStore.saveLayoutToPreferencesStore(isLinearLayoutManager, requireContext())
}
// Sets layout and icon
chooseLayout()
setIcon(item)
Expand All @@ -102,5 +122,4 @@ class LetterListFragment : Fragment() {
super.onDestroyView()
_binding = null
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.prefdatastore.data

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.catch
import java.io.IOException


private const val LAYOUT_PREFERENCES_NAME = "layout_preferences"

private val Context.dataStore : DataStore<Preferences> by preferencesDataStore(
name = LAYOUT_PREFERENCES_NAME
)

class SettingsDataStore(context:Context) {
private val IS_LINEAR_LAYOUT_MANAGER = booleanPreferencesKey("is_linear_layout_manager")

val preferenceFlow: Flow<Boolean> = context.dataStore.data
.catch {
if (it is IOException) {
it.printStackTrace()
emit(emptyPreferences())
} else {
throw it
}
}
.map { preferences ->
// On the first run of the app, we will use LinearLayoutManager by default
preferences[IS_LINEAR_LAYOUT_MANAGER] ?: true
}

suspend fun saveLayoutToPreferencesStore(isLinearLayoutManager: Boolean, context: Context){
//call dataStore.edit(), and pass in a block of code to store the new value.
context.dataStore.edit { preferences->
preferences[IS_LINEAR_LAYOUT_MANAGER] = isLinearLayoutManager
}
}
}
19 changes: 19 additions & 0 deletions Theme/.idea/modules/Theme.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf5d300

Please sign in to comment.