Skip to content

Commit

Permalink
Merge pull request #1 from depromeet/feature/impl-main-tab-set
Browse files Browse the repository at this point in the history
바텀 네비게이션 탭 구현, color, dimen, theme 속성 파일 추가
  • Loading branch information
LeeOhHyung authored May 19, 2021
2 parents a9de887 + 41f8b0a commit b58ab5e
Show file tree
Hide file tree
Showing 26 changed files with 433 additions and 195 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Toonitooniandroid">
<activity android:name=".MainActivity">
android:theme="@style/AppTheme">
<activity android:name=".features.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
27 changes: 0 additions & 27 deletions app/src/main/kotlin/kr/tooni/tooni/MainActivity.kt

This file was deleted.

12 changes: 0 additions & 12 deletions app/src/main/kotlin/kr/tooni/tooni/MainRepository.kt

This file was deleted.

17 changes: 0 additions & 17 deletions app/src/main/kotlin/kr/tooni/tooni/MainRepositoryImpl.kt

This file was deleted.

28 changes: 0 additions & 28 deletions app/src/main/kotlin/kr/tooni/tooni/MainViewModel.kt

This file was deleted.

58 changes: 58 additions & 0 deletions app/src/main/kotlin/kr/tooni/tooni/base/arch/Event.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kr.tooni.tooni.base.arch

import androidx.lifecycle.Observer

/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {

var hasBeenHandled = false
private set // Allow external read but not write

/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}

/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}

/**
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has
* already been handled.
*
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled.
*/
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>?) {
event?.getContentIfNotHandled()?.let { value ->
onEventUnhandledContent(value)
}
}
}
17 changes: 0 additions & 17 deletions app/src/main/kotlin/kr/tooni/tooni/data/api/MortyApi.kt

This file was deleted.

This file was deleted.

16 changes: 16 additions & 0 deletions app/src/main/kotlin/kr/tooni/tooni/extensions/Activity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Created by Leo on 2021. 05. 19 ..
*/
package kr.tooni.tooni.extensions

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import kr.tooni.tooni.base.arch.Event
import kr.tooni.tooni.base.arch.EventObserver

fun <T> AppCompatActivity.observeEvent(
liveData: LiveData<Event<T>>,
action: (T) -> Unit
) {
liveData.observe(this, EventObserver { action(it) })
}
10 changes: 10 additions & 0 deletions app/src/main/kotlin/kr/tooni/tooni/extensions/Context.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Created by Leo on 2021. 05. 19 ..
*/
package kr.tooni.tooni.extensions

import android.content.Context
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat

fun Context.getDrawableCompat(@DrawableRes resourceId: Int) = ContextCompat.getDrawable(this, resourceId)
25 changes: 25 additions & 0 deletions app/src/main/kotlin/kr/tooni/tooni/extensions/Fragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Created by Leo on 2021. 04. 17 ..
*/
package kr.tooni.tooni.extensions

import android.graphics.drawable.Drawable
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import kr.tooni.tooni.base.arch.Event
import kr.tooni.tooni.base.arch.EventObserver

fun Fragment.getDrawableCompat(@DrawableRes resourceId: Int): Drawable? {
return requireContext().getDrawableCompat(resourceId)
}
fun Fragment.getColor(@ColorRes colorId: Int) = ContextCompat.getColor(requireContext(), colorId)

inline fun <T> Fragment.observeEvent(
liveData: LiveData<Event<T>>,
crossinline action: (T) -> Unit
) {
liveData.observe(viewLifecycleOwner, EventObserver { action(it) })
}
15 changes: 0 additions & 15 deletions app/src/main/kotlin/kr/tooni/tooni/extensions/FragmentActivity.kt

This file was deleted.

79 changes: 79 additions & 0 deletions app/src/main/kotlin/kr/tooni/tooni/features/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package kr.tooni.tooni.features

import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import kr.tooni.tooni.R
import kr.tooni.tooni.base.BaseActivity
import kr.tooni.tooni.databinding.ActivityMainBinding
import kr.tooni.tooni.features.day.DayWebtoonFragment

class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {

private val viewModel by viewModels<MainViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initView()
setInitialTab()
setBottomNavigationItemSelectedListener()
}

private fun initView() {
binding.viewModel = viewModel
binding.lifecycleOwner = this
}

private fun setInitialTab() {
showPhotoFragment()
}

private fun setBottomNavigationItemSelectedListener() {
binding.bottomNavigationView.setOnNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.home_1 -> showPhotoFragment()
R.id.home_2 -> showPhotoFragment()
R.id.home_3 -> showPhotoFragment()
R.id.home_4 -> showPhotoFragment()
else -> throw IllegalArgumentException("${menuItem.itemId} is invalid itemId")
}

return@setOnNavigationItemSelectedListener true
}
}

private fun showPhotoFragment() {
val tag = DayWebtoonFragment::class.java.name

supportFragmentManager.findFragmentByTag(tag)
?.let { fragment ->
showFragment(fragment)
}
?: addFragment(
fragment = DayWebtoonFragment.newInstance(),
tag = tag
)
}

private fun addFragment(fragment: Fragment, tag: String) =
supportFragmentManager
.beginTransaction()
.hideFragments()
.add(R.id.container, fragment, tag)
.commitAllowingStateLoss()

private fun showFragment(fragment: Fragment) =
supportFragmentManager
.beginTransaction()
.hideFragments()
.show(fragment)
.commitAllowingStateLoss()

private fun FragmentTransaction.hideFragments(): FragmentTransaction =
this.apply {
supportFragmentManager
.fragments
.forEach { fragment -> hide(fragment) }
}
}
8 changes: 8 additions & 0 deletions app/src/main/kotlin/kr/tooni/tooni/features/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Created by Leo on 2021. 04. 17 ..
*/
package kr.tooni.tooni.features

import kr.tooni.tooni.base.BaseViewModel

class MainViewModel : BaseViewModel()
Loading

0 comments on commit b58ab5e

Please sign in to comment.