-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working Wear remote PTT (mic/speaker is currently still phone only)
Add wear/AlfredAiApp, consistent ViewModel init in App.onCreate
- Loading branch information
Showing
13 changed files
with
241 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
package com.swooby.alfredai | ||
|
||
import android.app.Application | ||
import androidx.activity.ComponentActivity | ||
import androidx.annotation.MainThread | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelLazy | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewModelStore | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
|
||
@MainThread | ||
inline fun <reified VM : ViewModel> ComponentActivity.appViewModels(): Lazy<VM> { | ||
return ViewModelLazy( | ||
VM::class, | ||
{ (application as AlfredAiApp).viewModelStore }, | ||
{ ViewModelProvider.AndroidViewModelFactory.getInstance(application) }, | ||
{ defaultViewModelCreationExtras } | ||
) | ||
} | ||
|
||
class AlfredAiApp : Application(), ViewModelStoreOwner | ||
{ | ||
override val viewModelStore = ViewModelStore() | ||
|
||
val mobileViewModel by lazy { | ||
ViewModelProvider( | ||
this, | ||
ViewModelProvider.AndroidViewModelFactory.getInstance(this) | ||
)[MobileViewModel::class.java] | ||
} | ||
|
||
class AlfredAiApp : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
mobileViewModel.init() | ||
} | ||
|
||
override fun onTerminate() { | ||
super.onTerminate() | ||
mobileViewModel.close() | ||
} | ||
} |
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,41 @@ | ||
package com.swooby.alfredai | ||
|
||
import android.app.Application | ||
import androidx.activity.ComponentActivity | ||
import androidx.annotation.MainThread | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelLazy | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewModelStore | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
|
||
@MainThread | ||
inline fun <reified VM : ViewModel> ComponentActivity.appViewModels(): Lazy<VM> { | ||
return ViewModelLazy( | ||
VM::class, | ||
{ (application as AlfredAiApp).viewModelStore }, | ||
{ ViewModelProvider.AndroidViewModelFactory.getInstance(application) }, | ||
{ defaultViewModelCreationExtras } | ||
) | ||
} | ||
|
||
class AlfredAiApp : Application(), ViewModelStoreOwner { | ||
override val viewModelStore = ViewModelStore() | ||
|
||
val wearViewModel by lazy { | ||
ViewModelProvider( | ||
this, | ||
ViewModelProvider.AndroidViewModelFactory.getInstance(this) | ||
)[WearViewModel::class.java] | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
wearViewModel.init() | ||
} | ||
|
||
override fun onTerminate() { | ||
super.onTerminate() | ||
wearViewModel.close() | ||
} | ||
} |
Oops, something went wrong.