Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SharedPreference #71

Merged
merged 17 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<activity
android:name=".player.PlayerActivity"
android:screenOrientation="sensorLandscape" />
<activity android:name=".MainActivity" />
<activity android:name=".MainActivity"
android:launchMode="singleTop">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package divyansh.tech.animeclassroom

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.NavigationUI
Expand All @@ -17,7 +14,6 @@ import divyansh.tech.animeclassroom.manga.MangaFragment
import divyansh.tech.animeclassroom.settings.SettingsFragment
import divyansh.tech.animeclassroom.shop.ShopFragment
import kotlinx.android.synthetic.main.activity_main.*
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/divyansh/tech/animeclassroom/SplashActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package divyansh.tech.animeclassroom

import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.view.WindowManager
import dagger.hilt.android.AndroidEntryPoint
import divyansh.tech.animeclassroom.common.setUIMode
import javax.inject.Inject

@AndroidEntryPoint
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

@Inject
lateinit var sharedPreferences: SharedPreferences

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initTheme()
setContentView(R.layout.activity_splash)

window.setFlags(
Expand All @@ -17,9 +27,14 @@ class SplashActivity : AppCompatActivity() {
)

Handler().postDelayed({
val intent = Intent(this,MainActivity::class.java)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
},3000)
}, 3000)
}

private fun initTheme() {
val uiMode = sharedPreferences.getInt(C.THEME, C.UI_MODE.SYSTEM_MODE.value)
setUIMode(uiMode)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package divyansh.tech.animeclassroom.common

import android.content.Context
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatDelegate

fun Context.isDarkTheme(): Boolean {
return this.resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}

fun setUIMode(value: Int){
when(value){
0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
2 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package divyansh.tech.animeclassroom.di

import android.content.Context
import android.content.SharedPreferences
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.scopes.ActivityScoped

const val APP_SHARED_PREFERENCE = "divyansh.tech.animeclassroom.SharedPreference"

@Module
@InstallIn(ActivityComponent::class)
class AppSharedPreference {

@ActivityScoped
@Provides
fun provideSharedPreference(@ApplicationContext context: Context): SharedPreferences {
return context.getSharedPreferences(APP_SHARED_PREFERENCE, Context.MODE_PRIVATE)
}

}
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
package divyansh.tech.animeclassroom.settings

import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.Fragment
import dagger.hilt.android.AndroidEntryPoint
import divyansh.tech.animeclassroom.C
import divyansh.tech.animeclassroom.R
import divyansh.tech.animeclassroom.databinding.FragmentHomeBinding
import divyansh.tech.animeclassroom.databinding.FragmentSearchBinding
import divyansh.tech.animeclassroom.generated.callback.OnClickListener
import divyansh.tech.animeclassroom.common.setUIMode
import divyansh.tech.animeclassroom.databinding.FragmentSettingsBinding
import kotlinx.android.synthetic.main.fragment_settings.*

//Todo: Use FragmentSettingsBinding
import javax.inject.Inject

@AndroidEntryPoint
class SettingsFragment: Fragment(){

@Inject
lateinit var sharedPreference: SharedPreferences

private lateinit var _settingsFragmentBinding: FragmentSettingsBinding
val binding: FragmentSettingsBinding get() = _settingsFragmentBinding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.fragment_settings, container, false)
_settingsFragmentBinding = FragmentSettingsBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpListeners()
about_settings.setOnClickListener {
aboutSettings()
}
}


private fun setUpListeners() {
binding.uiMode.setOnClickListener{showUIDialog()}
binding.aboutSettings.setOnClickListener { aboutSettings() }
}

private fun aboutSettings(){
Expand All @@ -45,18 +53,14 @@ class SettingsFragment: Fragment(){
}
}

private fun setUpListeners() {
ui_mode.setOnClickListener{showUIDialog()}
}

private fun showUIDialog() {
val uiModes = resources.getStringArray(R.array.ui_mode)
val checkedItem = 0
val uiModes = resources.getStringArray(R.array.ui_modes)
val checkedItem = sharedPreference.getInt(C.THEME, C.UI_MODE.SYSTEM_MODE.value)

val alertDialogBuilder = AlertDialog.Builder(requireContext())
alertDialogBuilder.setTitle("Choose ui Mode")
alertDialogBuilder.setSingleChoiceItems(uiModes, checkedItem){ dialog, checked ->
setUiMode(checked)
changeTheme(checked)
dialog.dismiss()
}
val alertDialog = alertDialogBuilder.create().apply {
Expand All @@ -65,11 +69,11 @@ class SettingsFragment: Fragment(){
}
}

private fun setUiMode(checked: Int) {
when(checked){
0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
2 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
private fun changeTheme(checked: Int) {
with(sharedPreference.edit()){
putInt(C.THEME, checked)
apply()
}
setUIMode(checked)
}
}
Loading