Commit 1ceae466 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Saves the sorting and grouping preferences for each server.

parent e463622d
......@@ -44,7 +44,6 @@ class MessageInfoFragment : Fragment(), MessageInfoView {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidSupportInjection.inject(this)
setHasOptionsMenu(true)
val bundle = arguments
if (bundle != null) {
......@@ -52,6 +51,8 @@ class MessageInfoFragment : Fragment(), MessageInfoView {
} else {
requireNotNull(bundle) { "no arguments supplied when the fragment was instantiated" }
}
setHasOptionsMenu(true)
}
override fun onCreateView(
......
......@@ -268,7 +268,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidSupportInjection.inject(this)
setHasOptionsMenu(true)
arguments?.run {
chatRoomId = getString(BUNDLE_CHAT_ROOM_ID, "")
......@@ -292,6 +291,8 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
navigator = navigator,
analyticsManager = analyticsManager
)
setHasOptionsMenu(true)
}
override fun onCreateView(
......
......@@ -9,20 +9,18 @@ import chat.rocket.android.db.model.ChatRoomEntity
import chat.rocket.android.helper.UserHelper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.main.presentation.MainNavigator
import chat.rocket.android.server.domain.GetAccountsInteractor
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.domain.SortingAndGroupingInteractor
import chat.rocket.android.server.domain.useRealName
import chat.rocket.android.server.domain.useSpecialCharsOnRoom
import chat.rocket.android.server.infraestructure.ConnectionManager
import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.retryDB
import chat.rocket.android.util.retryIO
import chat.rocket.common.RocketChatAuthException
import chat.rocket.common.RocketChatException
import chat.rocket.common.model.RoomType
import chat.rocket.common.model.User
import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull
import chat.rocket.core.internal.realtime.createDirectMessage
import chat.rocket.core.internal.rest.me
import chat.rocket.core.internal.rest.show
......@@ -38,7 +36,7 @@ class ChatRoomsPresenter @Inject constructor(
private val strategy: CancelStrategy,
private val navigator: MainNavigator,
@Named("currentServer") private val currentServer: String,
private val getAccountsInteractor: GetAccountsInteractor,
private val sortingAndGroupingInteractor: SortingAndGroupingInteractor,
private val dbManager: DatabaseManager,
manager: ConnectionManager,
private val localRepository: LocalRepository,
......@@ -52,33 +50,38 @@ class ChatRoomsPresenter @Inject constructor(
fun toSettings() = navigator.toSettings()
fun getCurrentServerName() {
view.setupToolbar(currentServer)
fun getCurrentServerName() = view.setupToolbar(currentServer)
fun getSortingAndGroupingPreferences() {
with(sortingAndGroupingInteractor) {
view.setupSortingAndGrouping(
getSortByName(currentServer),
getUnreadOnTop(currentServer),
getGroupByType(currentServer),
getGroupByFavorites(currentServer)
)
}
}
fun loadChatRoom(roomId: String) {
launchUI(strategy) {
view.showLoadingRoom("")
try {
val room = dbManager.getRoom(roomId)
if (room != null) {
loadChatRoom(room.chatRoom, true)
} else {
Timber.d("Error loading channel")
Timber.e("Error loading channel")
view.showGenericErrorMessage()
}
} catch (ex: Exception) {
Timber.d(ex, "Error loading channel")
Timber.e(ex, "Error loading channel")
view.showGenericErrorMessage()
} finally {
view.hideLoadingRoom()
}
}
}
fun loadChatRoom(chatRoom: RoomUiModel) {
launchUI(strategy) {
view.showLoadingRoom(chatRoom.name)
try {
val room = retryDB("getRoom(${chatRoom.id}") { dbManager.getRoom(chatRoom.id) }
if (room != null) {
......@@ -98,10 +101,8 @@ class ChatRoomsPresenter @Inject constructor(
}
}
} catch (ex: Exception) {
Timber.d(ex, "Error loading channel")
Timber.e(ex, "Error loading channel")
view.showGenericErrorMessage()
} finally {
view.hideLoadingRoom()
}
}
}
......
......@@ -2,7 +2,6 @@ package chat.rocket.android.chatrooms.presentation
import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView
import chat.rocket.android.server.domain.model.Account
interface ChatRoomsView : LoadingView, MessageView {
......@@ -13,7 +12,19 @@ interface ChatRoomsView : LoadingView, MessageView {
*/
fun setupToolbar(serverName: String)
fun showLoadingRoom(name: CharSequence)
fun hideLoadingRoom()
/**
* Setups the sorting and grouping in the bases of the user preference for
* the current logged in server.
*
* @param isSortByName True if sorting by name, false otherwise.
* @param isUnreadOnTop True if grouping by unread on top, false otherwise.
* @param isGroupByType True if grouping by type , false otherwise.
* @param isGroupByFavorites True if grouping by favorites, false otherwise.
*/
fun setupSortingAndGrouping(
isSortByName: Boolean,
isUnreadOnTop: Boolean,
isGroupByType: Boolean,
isGroupByFavorites: Boolean
)
}
\ No newline at end of file
package chat.rocket.android.chatrooms.ui
import android.app.ProgressDialog
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
......@@ -27,10 +25,8 @@ import chat.rocket.android.chatrooms.viewmodel.ChatRoomsViewModel
import chat.rocket.android.chatrooms.viewmodel.ChatRoomsViewModelFactory
import chat.rocket.android.chatrooms.viewmodel.LoadingState
import chat.rocket.android.chatrooms.viewmodel.Query
import chat.rocket.android.helper.ChatRoomsSortOrder
import chat.rocket.android.helper.Constants
import chat.rocket.android.helper.SharedPreferenceHelper
import chat.rocket.android.servers.ui.ServersBottomSheetFragment
import chat.rocket.android.sortingandgrouping.ui.SortingAndGroupingBottomSheetFragment
import chat.rocket.android.util.extension.onQueryTextListener
import chat.rocket.android.util.extensions.ifNotNullNotEmpty
import chat.rocket.android.util.extensions.inflate
......@@ -48,42 +44,38 @@ internal const val TAG_CHAT_ROOMS_FRAGMENT = "ChatRoomsFragment"
private const val BUNDLE_CHAT_ROOM_ID = "BUNDLE_CHAT_ROOM_ID"
class ChatRoomsFragment : Fragment(), ChatRoomsView {
@Inject
lateinit var presenter: ChatRoomsPresenter
@Inject
lateinit var factory: ChatRoomsViewModelFactory
@Inject
lateinit var analyticsManager: AnalyticsManager
private lateinit var viewModel: ChatRoomsViewModel
private val handler = Handler()
private var chatRoomId: String? = null
private var progressDialog: ProgressDialog? = null
companion object {
fun newInstance(chatRoomId: String? = null): ChatRoomsFragment = ChatRoomsFragment().apply {
fun newInstance(chatRoomId: String?): Fragment = ChatRoomsFragment().apply {
arguments = Bundle(1).apply {
putString(BUNDLE_CHAT_ROOM_ID, chatRoomId)
}
}
}
}
class ChatRoomsFragment : Fragment(), ChatRoomsView {
@Inject lateinit var presenter: ChatRoomsPresenter
@Inject lateinit var factory: ChatRoomsViewModelFactory
@Inject lateinit var analyticsManager: AnalyticsManager
private lateinit var viewModel: ChatRoomsViewModel
private var chatRoomId: String? = null
private var isSortByName = false
private var isUnreadOnTop = false
private var isGroupByType = false
private var isGroupByFavorites = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidSupportInjection.inject(this)
setHasOptionsMenu(true)
arguments?.run {
chatRoomId = getString(BUNDLE_CHAT_ROOM_ID)
chatRoomId.ifNotNullNotEmpty { roomId ->
presenter.loadChatRoom(roomId)
chatRoomId.ifNotNullNotEmpty {
presenter.loadChatRoom(it)
chatRoomId = null
}
}
}
override fun onDestroy() {
handler.removeCallbacks(dismissStatus)
super.onDestroy()
setHasOptionsMenu(true)
}
override fun onCreateView(
......@@ -95,65 +87,47 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter.getCurrentServerName()
setupListeners()
with(presenter) {
getCurrentServerName()
getSortingAndGroupingPreferences()
}
viewModel = ViewModelProviders.of(this, factory).get(ChatRoomsViewModel::class.java)
subscribeUi()
setupListeners()
analyticsManager.logScreenView(ScreenViewEvent.ChatRooms)
}
private fun subscribeUi() {
ui {
val adapter = RoomsAdapter { room ->
presenter.loadChatRoom(room)
}
with(recycler_view) {
layoutManager = LinearLayoutManager(it)
addItemDecoration(
DividerItemDecoration(
it,
resources.getDimensionPixelSize(R.dimen.divider_item_decorator_bound_start),
resources.getDimensionPixelSize(R.dimen.divider_item_decorator_bound_end)
)
)
itemAnimator = DefaultItemAnimator()
}
viewModel.getChatRooms().observe(viewLifecycleOwner, Observer { rooms ->
rooms?.let {
Timber.d("Got items: $it")
adapter.values = it
if (recycler_view.adapter != adapter) {
recycler_view.adapter = adapter
}
if (rooms.isNotEmpty()) {
// text_no_data_to_display.isVisible = false
}
}
})
viewModel.loadingState.observe(viewLifecycleOwner, Observer { state ->
when (state) {
is LoadingState.Loading -> if (state.count == 0L) showLoading()
is LoadingState.Loaded -> {
hideLoading()
if (state.count == 0L) showNoChatRoomsToDisplay()
override fun setupToolbar(serverName: String) {
with((activity as AppCompatActivity)) {
with(toolbar) {
setSupportActionBar(this)
setNavigationOnClickListener { presenter.toSettings() }
}
is LoadingState.Error -> {
hideLoading()
showGenericErrorMessage()
}
text_server_name.text = serverName
}
})
viewModel.getStatus().observe(viewLifecycleOwner, Observer { status ->
status?.let { showConnectionState(status) }
})
override fun setupSortingAndGrouping(
isSortByName: Boolean,
isUnreadOnTop: Boolean,
isGroupByType: Boolean,
isGroupByFavorites: Boolean
) {
this.isSortByName = isSortByName
this.isUnreadOnTop = isUnreadOnTop
this.isGroupByType = isGroupByType
this.isGroupByFavorites = isGroupByFavorites
updateSort()
if (isSortByName) {
text_sort_by.text =
getString(R.string.msg_sort_by, getString(R.string.msg_sort_by_name).toLowerCase())
} else {
text_sort_by.text = getString(
R.string.msg_sort_by,
getString(R.string.msg_sort_by_activity).toLowerCase()
)
}
}
......@@ -193,26 +167,6 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
return super.onOptionsItemSelected(item)
}
private fun updateSort() {
val sortType = SharedPreferenceHelper.getInt(
Constants.CHATROOM_SORT_TYPE_KEY,
ChatRoomsSortOrder.ACTIVITY
)
val grouped = SharedPreferenceHelper.getBoolean(Constants.CHATROOM_GROUP_BY_TYPE_KEY, false)
val query = when (sortType) {
ChatRoomsSortOrder.ALPHABETICAL -> {
Query.ByName(grouped)
}
ChatRoomsSortOrder.ACTIVITY -> {
Query.ByActivity(grouped)
}
else -> Query.ByActivity()
}
viewModel.setQuery(query)
}
private fun showNoChatRoomsToDisplay() {
// ui { text_no_data_to_display.isVisible = true }
}
......@@ -239,16 +193,6 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
override fun showGenericErrorMessage() = showMessage(getString(R.string.msg_generic_error))
override fun showLoadingRoom(name: CharSequence) {
ui {
progressDialog = ProgressDialog.show(activity, "Rocket.Chat", "Loading room $name")
}
}
override fun hideLoadingRoom() {
progressDialog?.dismiss()
}
private fun showConnectionState(state: State) {
Timber.d("Got new state: $state")
// ui {
......@@ -272,20 +216,56 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
// }
}
private val dismissStatus = {
// if (text_connection_status != null) {
// text_connection_status.fadeOut()
// }
private fun subscribeUi() {
ui {
val adapter = RoomsAdapter { room ->
presenter.loadChatRoom(room)
}
override fun setupToolbar(serverName: String) {
with((activity as AppCompatActivity)) {
with(toolbar) {
setSupportActionBar(this)
setNavigationOnClickListener { presenter.toSettings() }
with(recycler_view) {
layoutManager = LinearLayoutManager(it)
addItemDecoration(
DividerItemDecoration(
it,
resources.getDimensionPixelSize(R.dimen.divider_item_decorator_bound_start),
resources.getDimensionPixelSize(R.dimen.divider_item_decorator_bound_end)
)
)
itemAnimator = DefaultItemAnimator()
}
viewModel.getChatRooms().observe(viewLifecycleOwner, Observer { rooms ->
rooms?.let {
adapter.values = it
if (recycler_view.adapter != adapter) {
recycler_view.adapter = adapter
}
if (rooms.isNotEmpty()) {
// text_no_data_to_display.isVisible = false
}
}
})
viewModel.loadingState.observe(viewLifecycleOwner, Observer { state ->
when (state) {
is LoadingState.Loading -> if (state.count == 0L) showLoading()
is LoadingState.Loaded -> {
hideLoading()
if (state.count == 0L) showNoChatRoomsToDisplay()
}
is LoadingState.Error -> {
hideLoading()
showGenericErrorMessage()
}
}
})
viewModel.getStatus().observe(viewLifecycleOwner, Observer { status ->
status?.let { showConnectionState(status) }
})
showAllChats()
}
text_server_name.text = serverName
}
private fun setupListeners() {
......@@ -297,9 +277,9 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
}
text_sort_by.setOnClickListener {
SortByBottomSheetFragment().show(
SortingAndGroupingBottomSheetFragment().show(
activity?.supportFragmentManager,
chat.rocket.android.chatrooms.ui.TAG
chat.rocket.android.sortingandgrouping.ui.TAG
)
}
}
......@@ -310,6 +290,11 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
isGroupByType: Boolean,
isGroupByFavorites: Boolean
) {
this.isSortByName = isSortByName
this.isUnreadOnTop = isUnreadOnTop
this.isGroupByType = isGroupByType
this.isGroupByFavorites = isGroupByFavorites
if (isSortByName) {
viewModel.setQuery(Query.ByName(isGroupByType))
changeSortByTitle(getString(R.string.msg_sort_by_name))
......@@ -325,10 +310,18 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
private fun queryChatRoomsByName(name: String?): Boolean {
if (name.isNullOrEmpty()) {
updateSort()
showAllChats()
} else {
viewModel.setQuery(Query.Search(name))
}
return true
}
private fun showAllChats() {
if (isSortByName) {
viewModel.setQuery(Query.ByName(isGroupByType))
} else {
viewModel.setQuery(Query.ByActivity(isGroupByType))
}
}
}
......@@ -37,6 +37,7 @@ import chat.rocket.android.servers.di.ServersBottomSheetFragmentProvider
import chat.rocket.android.settings.di.SettingsFragmentProvider
import chat.rocket.android.settings.password.di.PasswordFragmentProvider
import chat.rocket.android.settings.password.ui.PasswordActivity
import chat.rocket.android.sortingandgrouping.di.SortingAndGroupingBottomSheetFragmentProvider
import chat.rocket.android.userdetails.di.UserDetailsFragmentProvider
import chat.rocket.android.videoconference.di.VideoConferenceModule
import chat.rocket.android.videoconference.ui.VideoConferenceActivity
......@@ -67,6 +68,7 @@ abstract class ActivityBuilder {
modules = [MainModule::class,
ChatRoomsFragmentProvider::class,
ServersBottomSheetFragmentProvider::class,
SortingAndGroupingBottomSheetFragmentProvider::class,
CreateChannelProvider::class,
ProfileFragmentProvider::class,
SettingsFragmentProvider::class,
......
......@@ -27,23 +27,23 @@ import chat.rocket.android.push.PushManager
import chat.rocket.android.server.domain.AccountsRepository
import chat.rocket.android.server.domain.AnalyticsTrackingInteractor
import chat.rocket.android.server.domain.AnalyticsTrackingRepository
import chat.rocket.android.server.domain.BasicAuthRepository
import chat.rocket.android.server.domain.ChatRoomsRepository
import chat.rocket.android.server.domain.CurrentServerRepository
import chat.rocket.android.server.domain.GetAccountInteractor
import chat.rocket.android.server.domain.GetAccountsInteractor
import chat.rocket.android.server.domain.GetBasicAuthInteractor
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.GetSettingsInteractor
import chat.rocket.android.server.domain.JobSchedulerInteractor
import chat.rocket.android.server.domain.MessagesRepository
import chat.rocket.android.server.domain.MultiServerTokenRepository
import chat.rocket.android.server.domain.PermissionsRepository
import chat.rocket.android.server.domain.SaveBasicAuthInteractor
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.domain.SortingAndGroupingRepository
import chat.rocket.android.server.domain.TokenRepository
import chat.rocket.android.server.domain.UsersRepository
import chat.rocket.android.server.domain.BasicAuthRepository
import chat.rocket.android.server.domain.GetBasicAuthInteractor
import chat.rocket.android.server.domain.SaveBasicAuthInteractor
import chat.rocket.android.server.infraestructure.SharedPrefsBasicAuthRepository
import chat.rocket.android.server.infraestructure.DatabaseMessageMapper
import chat.rocket.android.server.infraestructure.DatabaseMessagesRepository
import chat.rocket.android.server.infraestructure.JobSchedulerInteractorImpl
......@@ -53,11 +53,13 @@ import chat.rocket.android.server.infraestructure.SharedPreferencesAccountsRepos
import chat.rocket.android.server.infraestructure.SharedPreferencesPermissionsRepository
import chat.rocket.android.server.infraestructure.SharedPreferencesSettingsRepository
import chat.rocket.android.server.infraestructure.SharedPrefsAnalyticsTrackingRepository
import chat.rocket.android.server.infraestructure.SharedPrefsBasicAuthRepository
import chat.rocket.android.server.infraestructure.SharedPrefsConnectingServerRepository
import chat.rocket.android.server.infraestructure.SharedPrefsCurrentServerRepository
import chat.rocket.android.server.infraestructure.SharedPrefsSortingAndGroupingRepository
import chat.rocket.android.util.AppJsonAdapterFactory
import chat.rocket.android.util.HttpLoggingInterceptor
import chat.rocket.android.util.BasicAuthenticatorInterceptor
import chat.rocket.android.util.HttpLoggingInterceptor
import chat.rocket.android.util.TimberLogger
import chat.rocket.common.internal.FallbackSealedClassJsonAdapter
import chat.rocket.common.internal.ISO8601Date
......@@ -123,7 +125,10 @@ class AppModule {
@Provides
@Singleton
fun provideOkHttpClient(logger: HttpLoggingInterceptor, basicAuthenticator: BasicAuthenticatorInterceptor): OkHttpClient {
fun provideOkHttpClient(
logger: HttpLoggingInterceptor,
basicAuthenticator: BasicAuthenticatorInterceptor
): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(logger)
.addInterceptor(basicAuthenticator)
......@@ -194,6 +199,12 @@ class AppModule {
return SharedPrefsAnalyticsTrackingRepository(prefs)
}
@Provides
@Singleton
fun provideSortingAndGroupingRepository(prefs: SharedPreferences): SortingAndGroupingRepository {
return SharedPrefsSortingAndGroupingRepository(prefs)
}
@Provides
@ForAuthentication
fun provideConnectingServerRepository(prefs: SharedPreferences): CurrentServerRepository {
......@@ -293,7 +304,7 @@ class AppModule {
@Provides
@Singleton
fun provideBasicAuthRepository (
fun provideBasicAuthRepository(
preferences: SharedPreferences,
moshi: Moshi
): BasicAuthRepository =
......
package chat.rocket.android.helper
object Constants {
const val CHATROOM_SORT_TYPE_KEY: String = "chatroom_sort_type"
const val CHATROOM_GROUP_BY_TYPE_KEY: String = "chatroom_group_by_type"
const val CHATROOM_GROUP_FAVOURITES_KEY: String = "chatroom_group_favourites"
//Used to sort chat rooms
const val CHATROOM_CHANNEL = 0
const val CHATROOM_PRIVATE_GROUP = 1
const val CHATROOM_DM = 2
const val CHATROOM_LIVE_CHAT = 3
}
object ChatRoomsSortOrder {
const val ALPHABETICAL: Int = 0
const val ACTIVITY: Int = 1
}
\ No newline at end of file
package chat.rocket.android.helper
import android.content.SharedPreferences
import android.preference.PreferenceManager
import chat.rocket.android.app.RocketChatApplication
object SharedPreferenceHelper {
private var sharedPreferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(RocketChatApplication.getAppContext())
private var editor: SharedPreferences.Editor? = sharedPreferences.edit()
//Add more methods for other types if needed
fun putInt(key: String, value: Int) {
editor!!.putInt(key, value).apply()
}
fun getInt(key: String, defaultValue: Int): Int {
return sharedPreferences.getInt(key, defaultValue)
}
fun putLong(key: String, value: Long) {
editor!!.putLong(key, value).apply()
}
fun getLong(key: String, defaultValue: Long): Long {
return sharedPreferences.getLong(key, defaultValue)
}
fun putString(key: String, value: String) {
editor!!.putString(key, value).apply()
}
fun getString(key: String, defaultValue: String): String? {
return sharedPreferences.getString(key, defaultValue)
}
fun putBoolean(key: String, value: Boolean) {
editor!!.putBoolean(key, value).apply()
}
fun getBoolean(key: String, defaultValue: Boolean): Boolean {
return sharedPreferences.getBoolean(key, defaultValue)
}
fun remove(key: String) {
editor!!.remove(key).apply()
}
}
\ No newline at end of file
......@@ -21,7 +21,7 @@ class MainNavigator(internal val activity: MainActivity) {
fun toChatList(chatRoomId: String? = null) {
activity.addFragment(TAG_CHAT_ROOMS_FRAGMENT, R.id.fragment_container) {
ChatRoomsFragment.newInstance(chatRoomId)
chat.rocket.android.chatrooms.ui.newInstance(chatRoomId)
}
}
......
......@@ -63,8 +63,8 @@ class ProfileFragment : Fragment(), ProfileView, ActionMode.Callback {
}
override fun onCreate(savedInstanceState: Bundle?) {
AndroidSupportInjection.inject(this)
super.onCreate(savedInstanceState)
AndroidSupportInjection.inject(this)
setHasOptionsMenu(true)
}
......
package chat.rocket.android.server.domain
import javax.inject.Inject
class SortingAndGroupingInteractor @Inject constructor(val repository: SortingAndGroupingRepository) {
fun save(
currentServerUrl: String,
isSortByName: Boolean,
isUnreadOnTop: Boolean,
isGroupByType: Boolean,
isGroupByFavorites: Boolean
) = repository.save(
currentServerUrl,
isSortByName,
isUnreadOnTop,
isGroupByType,
isGroupByFavorites
)
fun getSortByName(currentServerUrl: String): Boolean =
repository.getSortByName(currentServerUrl)
fun getUnreadOnTop(currentServerUrl: String): Boolean =
repository.getUnreadOnTop(currentServerUrl)
fun getGroupByType(currentServerUrl: String): Boolean =
repository.getGroupByType(currentServerUrl)
fun getGroupByFavorites(currentServerUrl: String): Boolean =
repository.getGroupByFavorites(currentServerUrl)
}
\ No newline at end of file
package chat.rocket.android.server.domain
interface SortingAndGroupingRepository {
fun save(
currentServerUrl: String,
isSortByName: Boolean,
isUnreadOnTop: Boolean,
isGroupByType: Boolean,
isGroupByFavorites: Boolean
)
fun getSortByName(currentServerUrl: String): Boolean
fun getUnreadOnTop(currentServerUrl: String): Boolean
fun getGroupByType(currentServerUrl: String): Boolean
fun getGroupByFavorites(currentServerUrl: String): Boolean
}
\ No newline at end of file
package chat.rocket.android.server.infraestructure
import android.content.SharedPreferences
import chat.rocket.android.server.domain.SortingAndGroupingRepository
private const val SORT_BY_NAME_KEY = "SORT_BY_NAME_KEY"
private const val UNREAD_ON_TOP_KEY = "UNREAD_ON_TOP_KEY"
private const val GROUP_BY_TYPE_KEY = "GROUP_BY_TYPE_KEY"
private const val GROUP_BY_FAVORITES_KEY = "GROUP_BY_FAVORITES_KEY"
class SharedPrefsSortingAndGroupingRepository(private val preferences: SharedPreferences) :
SortingAndGroupingRepository {
override fun save(
currentServerUrl: String,
isSortByName: Boolean,
isUnreadOnTop: Boolean,
isGroupByType: Boolean,
isGroupByFavorites: Boolean
) {
preferences.edit().putBoolean(SORT_BY_NAME_KEY + currentServerUrl, isSortByName).apply()
preferences.edit().putBoolean(UNREAD_ON_TOP_KEY + currentServerUrl, isUnreadOnTop).apply()
preferences.edit().putBoolean(GROUP_BY_TYPE_KEY + currentServerUrl, isGroupByType).apply()
preferences.edit().putBoolean(GROUP_BY_FAVORITES_KEY + currentServerUrl, isGroupByFavorites)
.apply()
}
override fun getSortByName(currentServerUrl: String): Boolean =
preferences.getBoolean(SORT_BY_NAME_KEY + currentServerUrl, false)
override fun getUnreadOnTop(currentServerUrl: String): Boolean =
preferences.getBoolean(UNREAD_ON_TOP_KEY + currentServerUrl, false)
override fun getGroupByType(currentServerUrl: String): Boolean =
preferences.getBoolean(GROUP_BY_TYPE_KEY + currentServerUrl, false)
override fun getGroupByFavorites(currentServerUrl: String): Boolean =
preferences.getBoolean(GROUP_BY_FAVORITES_KEY + currentServerUrl, false)
}
\ No newline at end of file
......@@ -11,6 +11,5 @@ class ServersBottomSheetFragmentModule {
@Provides
@PerFragment
fun membersView(frag: ServersBottomSheetFragment): ServersView = frag
fun serversView(frag: ServersBottomSheetFragment): ServersView = frag
}
\ No newline at end of file
package chat.rocket.android.sortingandgrouping.di
import chat.rocket.android.dagger.scope.PerFragment
import chat.rocket.android.sortingandgrouping.presentation.SortingAndGroupingView
import chat.rocket.android.sortingandgrouping.ui.SortingAndGroupingBottomSheetFragment
import dagger.Module
import dagger.Provides
@Module
class SortingAndGroupingBottomSheetFragmentModule {
@Provides
@PerFragment
fun sortingAndGroupingView(frag: SortingAndGroupingBottomSheetFragment): SortingAndGroupingView =
frag
}
\ No newline at end of file
package chat.rocket.android.sortingandgrouping.di
import chat.rocket.android.dagger.scope.PerFragment
import chat.rocket.android.sortingandgrouping.ui.SortingAndGroupingBottomSheetFragment
import dagger.Module
import dagger.android.ContributesAndroidInjector
@Module
abstract class SortingAndGroupingBottomSheetFragmentProvider {
@ContributesAndroidInjector(modules = [SortingAndGroupingBottomSheetFragmentModule::class])
@PerFragment
abstract fun provideSortingAndGroupingBottomSheetFragment(): SortingAndGroupingBottomSheetFragment
}
\ No newline at end of file
package chat.rocket.android.sortingandgrouping.presentation
import chat.rocket.android.server.domain.SortingAndGroupingInteractor
import javax.inject.Inject
import javax.inject.Named
class SortingAndGroupingPresenter @Inject constructor(
private val view: SortingAndGroupingView,
private val sortingAndGroupingInteractor: SortingAndGroupingInteractor,
@Named("currentServer") private val currentServerUrl: String
) {
fun getSortingAndGroupingPreferences() {
with(sortingAndGroupingInteractor) {
view.showSortingAndGroupingPreferences(
getSortByName(currentServerUrl),
getUnreadOnTop(currentServerUrl),
getGroupByType(currentServerUrl),
getGroupByFavorites(currentServerUrl)
)
}
}
fun saveSortingAndGroupingPreferences(
isSortByName: Boolean,
isUnreadOnTop: Boolean,
isGroupByType: Boolean,
isGroupByFavorites: Boolean
) {
sortingAndGroupingInteractor.save(
currentServerUrl,
isSortByName,
isUnreadOnTop,
isGroupByType,
isGroupByFavorites
)
}
}
\ No newline at end of file
package chat.rocket.android.sortingandgrouping.presentation
interface SortingAndGroupingView {
/**
* Shows the sorting and grouping preferences for the current logged in server.
*
* @param isSortByName True if sorting by name, false otherwise.
* @param isUnreadOnTop True if grouping by unread on top, false otherwise.
* @param isGroupByType True if grouping by type , false otherwise.
* @param isGroupByFavorites True if grouping by favorites, false otherwise.
*/
fun showSortingAndGroupingPreferences(
isSortByName: Boolean,
isUnreadOnTop: Boolean,
isGroupByType: Boolean,
isGroupByFavorites: Boolean
)
}
\ No newline at end of file
package chat.rocket.android.chatrooms.ui
package chat.rocket.android.sortingandgrouping.ui
import DrawableHelper
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
......@@ -8,12 +9,20 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.DrawableRes
import chat.rocket.android.R
import chat.rocket.android.chatrooms.ui.ChatRoomsFragment
import chat.rocket.android.chatrooms.ui.TAG_CHAT_ROOMS_FRAGMENT
import chat.rocket.android.sortingandgrouping.presentation.SortingAndGroupingPresenter
import chat.rocket.android.sortingandgrouping.presentation.SortingAndGroupingView
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.bottom_sheet_fragment_sort_by.*
import javax.inject.Inject
const val TAG = "SortByBottomSheetFragment"
const val TAG = "SortingAndGroupingBottomSheetFragment"
class SortByBottomSheetFragment : BottomSheetDialogFragment() {
class SortingAndGroupingBottomSheetFragment : BottomSheetDialogFragment(), SortingAndGroupingView {
@Inject
lateinit var presenter: SortingAndGroupingPresenter
private var isSortByName = false
private var isUnreadOnTop = false
private var isGroupByType = false
......@@ -27,6 +36,11 @@ class SortByBottomSheetFragment : BottomSheetDialogFragment() {
private val groupByTypeDrawable by lazy { R.drawable.ic_group_by_type_20dp }
private val groupByFavoritesDrawable by lazy { R.drawable.ic_favorites_20dp }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AndroidSupportInjection.inject(this)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
......@@ -36,9 +50,44 @@ class SortByBottomSheetFragment : BottomSheetDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter.getSortingAndGroupingPreferences()
setupListeners()
}
override fun onCancel(dialog: DialogInterface?) {
super.onCancel(dialog)
presenter.saveSortingAndGroupingPreferences(
isSortByName,
isUnreadOnTop,
isGroupByType,
isGroupByFavorites
)
}
override fun showSortingAndGroupingPreferences(
isSortByName: Boolean,
isUnreadOnTop: Boolean,
isGroupByType: Boolean,
isGroupByFavorites: Boolean
) {
this.isSortByName = isSortByName
this.isUnreadOnTop = isUnreadOnTop
this.isGroupByType = isGroupByType
this.isGroupByFavorites = isGroupByFavorites
if (isSortByName) {
changeSortByTitle(getString(R.string.msg_sort_by_name))
checkSelection(text_name, filterDrawable)
} else {
changeSortByTitle(getString(R.string.msg_sort_by_activity))
checkSelection(text_activity, activityDrawable)
}
if (isUnreadOnTop) checkSelection(text_unread_on_top, unreadOnTopDrawable)
if (isGroupByType) checkSelection(text_group_by_type, groupByTypeDrawable)
if (isGroupByFavorites) checkSelection(text_group_by_favorites, groupByFavoritesDrawable)
}
private fun setupListeners() {
text_name.setOnClickListener {
changeSortByTitle(getString(R.string.msg_sort_by_name))
......
......@@ -22,6 +22,7 @@
android:drawableEnd="@drawable/ic_arrow_expand_20dp"
android:drawablePadding="10dp"
android:fontFamily="sans-serif-medium"
android:maxLines="1"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="normal"
......
......@@ -4,7 +4,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".chatrooms.ui.SortByBottomSheetFragment">
tools:context=".sortingandgrouping.ui.SortingAndGroupingBottomSheetFragment">
<TextView
android:id="@+id/text_sort_by"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment