Skip to content

Commit

Permalink
Merge pull request #71 from yash-k9/develop
Browse files Browse the repository at this point in the history
Added SharedPreference
  • Loading branch information
justdvnsh authored Oct 13, 2021
2 parents 8d489ac + 3c23550 commit 0edc782
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 121 deletions.
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

0 comments on commit 0edc782

Please sign in to comment.