-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
936977b
commit 524ac5e
Showing
8 changed files
with
205 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Copyright 2025 Splendo Consulting B.V. The Netherlands | ||
// | ||
// 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 | ||
// | ||
// http://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. | ||
// | ||
|
||
import UIKit | ||
import KalugaExampleShared | ||
|
||
class MediaListViewController: UITableViewController { | ||
|
||
private lazy var navigator: ViewControllerNavigator<MediaListNavigationAction> = ViewControllerNavigator(parentVC: self) { action in | ||
NavigationSpec.Segue(identifier: action.segueKey) | ||
} | ||
|
||
private lazy var viewModel = MediaListViewModel(navigator: navigator) | ||
private var lifecycleManager: LifecycleManager! | ||
|
||
private var media = [String]() | ||
private var onSelected: ((Int) -> Void)? | ||
|
||
deinit { | ||
lifecycleManager.unbind() | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
title = "feature_media".localized() | ||
|
||
lifecycleManager = viewModel.addLifecycleManager(parent: self) { [weak self] in | ||
guard let viewModel = self?.viewModel else { return [] } | ||
return [ | ||
viewModel.media.observeInitialized { next in | ||
let media = next ?? [] | ||
self?.media = media.map { ($0 as? Media)?.title ?? "" } | ||
self?.onSelected = { (index: Int) in | ||
if let media = media[index] as? Media { | ||
viewModel.onMediaSelected(media: media) | ||
} | ||
} | ||
self?.tableView.reloadData() | ||
} | ||
] | ||
} | ||
} | ||
|
||
override func numberOfSections(in tableView: UITableView) -> Int { | ||
return 1 | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
return media.count | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
return tableView.dequeueTypedReusableCell(withIdentifier: MediaListCell.Const.identifier, for: indexPath) { (cell: MediaListCell) in | ||
cell.label.text = media[indexPath.row] | ||
} | ||
} | ||
|
||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
_ = onSelected?(indexPath.row) | ||
tableView.deselectRow(at: indexPath, animated: true) | ||
} | ||
} | ||
|
||
class MediaListCell: UITableViewCell { | ||
|
||
enum Const { | ||
static let identifier = "MediaListCell" | ||
} | ||
|
||
@IBOutlet weak var label: UILabel! | ||
} | ||
|
||
private extension MediaListNavigationAction { | ||
var segueKey: String { | ||
switch self { | ||
case is MediaListNavigationAction.Media: return "showMedia" | ||
case is MediaListNavigationAction.Sound: return "showSound" | ||
default: return "" | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...commonMain/kotlin/com/splendo/kaluga/example/shared/viewmodel/media/MediaListViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright 2025 Splendo Consulting B.V. The Netherlands | ||
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 | ||
http://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 com.splendo.kaluga.example.shared.viewmodel.media | ||
|
||
import com.splendo.kaluga.architecture.navigation.NavigationAction | ||
import com.splendo.kaluga.architecture.navigation.Navigator | ||
import com.splendo.kaluga.architecture.observable.observableOf | ||
import com.splendo.kaluga.architecture.viewmodel.NavigatingViewModel | ||
import com.splendo.kaluga.resources.localized | ||
|
||
sealed class MediaListNavigationAction : NavigationAction<Nothing>(null) { | ||
|
||
data object Media : MediaListNavigationAction() | ||
data object Sound : MediaListNavigationAction() | ||
} | ||
|
||
enum class Media(private val titleKey: String) { | ||
MEDIA("feature_media"), | ||
SOUND("feature_media_sound"), | ||
; | ||
|
||
val title: String get() = titleKey.localized() | ||
} | ||
|
||
class MediaListViewModel(navigator: Navigator<MediaListNavigationAction>) : NavigatingViewModel<MediaListNavigationAction>(navigator) { | ||
|
||
val media = observableOf(Media.values().toList()) | ||
|
||
fun onMediaSelected(media: Media) { | ||
navigator.navigate( | ||
when (media) { | ||
Media.MEDIA -> MediaListNavigationAction.Media | ||
Media.SOUND -> MediaListNavigationAction.Sound | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters