Skip to content
Open
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
26 changes: 14 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ plugins {
}

kotlin {
jvmToolchain(17)
jvmToolchain(20)
}

android {
compileSdk 34
compileSdk 35
namespace "otus.gpb.homework.fragments"

defaultConfig {
applicationId "otus.gpb.homework.fragments"
minSdk 26
targetSdk 34
targetSdk 35
versionCode 1
versionName "1.0"

Expand All @@ -28,21 +28,23 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
sourceCompatibility JavaVersion.VERSION_20
targetCompatibility JavaVersion.VERSION_20
}
kotlinOptions {
jvmTarget = '17'
jvmTarget = '20'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "androidx.core:core-ktx:1.16.0"
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
implementation "androidx.fragment:fragment-ktx:1.8.6"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

}
14 changes: 14 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/ColorViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package otus.gpb.homework.fragments

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class ColorViewModel : ViewModel() {
private val _currentColor = MutableLiveData<Int>()
val currentColor: LiveData<Int> get() = _currentColor

fun setColor(color: Int) {
_currentColor.value = color
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Const.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package otus.gpb.homework.fragments

const val RESULT_KEY_A = "key_color_a"
const val RESULT_KEY_A_A = "key_color_a_a"

const val RESULT_BUNDLE = "bundle"
63 changes: 63 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package otus.gpb.homework.fragments

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.activity.OnBackPressedCallback
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

class FragmentA : Fragment() {

override fun onAttach(context: Context) {
super.onAttach(context)

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (childFragmentManager.backStackEntryCount > 0) {
childFragmentManager.popBackStack()
} else {
isEnabled = false
@Suppress("DEPRECATION")
requireActivity().onBackPressed()
}
}
}

requireActivity().onBackPressedDispatcher.addCallback(callback)
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_a, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.findViewById<Button>(R.id.btn_open_fragment_aa).setOnClickListener {
val backgroundColor = ColorGenerator.generateColor()

childFragmentManager.setFragmentResult(
RESULT_KEY_A,
bundleOf(RESULT_BUNDLE to backgroundColor)
)
childFragmentManager.popBackStackImmediate(
"fragmentA",
FragmentManager.POP_BACK_STACK_INCLUSIVE
)

childFragmentManager.beginTransaction()
.replace(R.id.fragment_view_container_a, FragmentAA())
.addToBackStack("fragmentA")
.commit()
}
}
}
53 changes: 53 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentAA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResultListener

class FragmentAA : Fragment() {
private val SAVED_COLOR = "saved_color"
private var savedBackgroundColor: Int = ColorGenerator.generateColor()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
savedBackgroundColor = savedInstanceState?.getInt(SAVED_COLOR) ?: savedBackgroundColor
return inflater.inflate(R.layout.fragment_aa, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.setBackgroundColor(savedBackgroundColor)

view.findViewById<Button>(R.id.btn_open_fragment_ab).setOnClickListener {
val backgroundColor = ColorGenerator.generateColor()

parentFragmentManager.beginTransaction()
.replace(R.id.fragment_view_container_a, FragmentAB())
.addToBackStack("fragmentAB")
.commit()

parentFragmentManager.setFragmentResult(
RESULT_KEY_A_A,
bundleOf(RESULT_BUNDLE to backgroundColor)
)
}

setFragmentResultListener(RESULT_KEY_A) { _, bundle ->
savedBackgroundColor = bundle.getInt(RESULT_BUNDLE)
view.setBackgroundColor(savedBackgroundColor)
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(SAVED_COLOR, savedBackgroundColor)
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentAB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResultListener

class FragmentAB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_ab, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setFragmentResultListener(RESULT_KEY_A_A) { _, bundle ->
view.setBackgroundColor(bundle.getInt(RESULT_BUNDLE))
}
}
}
59 changes: 59 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels


class FragmentB : Fragment() {
private val viewModel: ColorViewModel by viewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(
R.layout.fragment_b,
container,
false
)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewModel.currentColor.observe(viewLifecycleOwner) { color ->
updateFragments(color)
}
if (resources.getBoolean(R.bool.isLandscape)) {
showLandscape()
} else {
showPortrait()
}
super.onViewCreated(view, savedInstanceState)
}

private fun showLandscape() {
childFragmentManager.beginTransaction()
.replace(R.id.fragment_view_container_ba, FragmentBA())
.replace(R.id.fragment_view_container_bb, FragmentBB())
.commit()
}

private fun showPortrait() {
childFragmentManager.beginTransaction()
.addToBackStack(null)
.replace(R.id.fragment_view_container_ba, FragmentBA())
.commit()
}

private fun updateFragments(color: Int) {
val fragmentBA = childFragmentManager.findFragmentById(R.id.fragment_view_container_ba)
if (fragmentBA is FragmentBA) {
fragmentBA.updateColor(color)
}
}

}
42 changes: 42 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentBA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels

class FragmentBA : Fragment() {
private val parentViewModel: ColorViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_ba, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

parentViewModel.currentColor.observe(viewLifecycleOwner) { color ->
view.setBackgroundColor(color)
}

if (!requireContext().isLandscape()) {
view.findViewById<Button>(R.id.btn_open_fragment_bb).setOnClickListener {
parentFragmentManager.beginTransaction()
.replace(R.id.fragment_view_container_ba, FragmentBB())
.addToBackStack(null)
.commit()
}
}
}

fun updateColor(color: Int) {
view?.setBackgroundColor(color)
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/FragmentBB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package otus.gpb.homework.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels

class FragmentBB : Fragment() {
private val parentViewModel: ColorViewModel by activityViewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_bb, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

view.findViewById<Button>(R.id.btn_send_color_to_ba).setOnClickListener {
val backgroundColor = ColorGenerator.generateColor()
parentViewModel.setColor(backgroundColor)

if (!requireContext().isLandscape()) {
parentFragmentManager.popBackStack()
}
}
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/otus/gpb/homework/fragments/Helper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package otus.gpb.homework.fragments

import android.content.Context
import android.content.res.Configuration

fun Context.isLandscape() =
resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
Loading