I have working java code
public class ItemCallback<T extends Identifiable<?>> extends DiffUtil.ItemCallback<T> {
#Inject
public ItemCallback() {}
#Override
public boolean areItemsTheSame(#NonNull T oldItem, #NonNull T newItem) {
return oldItem.getId().equals(newItem.getId());
}
#Override
public boolean areContentsTheSame(#NonNull T oldItem, #NonNull T newItem) {
return oldItem.compareContent(newItem);
}
}
and i wrote correspond kotlin code
class ItemCallback<T : ListElementModel<*>> #Inject constructor() : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
return oldItem == newItem
}
}
and caught error
"error: InjectProcessingStep was unable to process 'ItemCallback()' because 'any' could not be resolved."
I understand that the error in star-projections. But I don't know how to solve it.
Related
I am trying to create sharedpreferences using Hilt. Already take a look for some search but I get this error.
error: [Dagger/MissingBinding] core.domain.usecase.NotePreferencesUseCase cannot be provided without an #Provides-annotated method.
Here's my code
NotePreferenceRepository
#Singleton
class NotePreferencesRepository #Inject constructor(
private val sharedPreferences: SharedPreferences
): INotePreferencesRepository {
private val edit = sharedPreferences.edit()
override fun setTheme(theme: String) {
edit.putString(THEME, theme)
}
override fun getTheme(): String? {
return sharedPreferences.getString(THEME, "")
}
companion object {
private val THEME = "theme"
}
}
NotePreferencesModule
#Module
#InstallIn(SingletonComponent::class)
object NotePreferencesModule {
#Provides
#Singleton
fun provideNotePreferences(#ApplicationContext context: Context): SharedPreferences {
return context.getSharedPreferences("note_prefs", Context.MODE_PRIVATE)
}
}
Repository Module
#Module
#InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
#Binds
abstract fun providePreferencesRepository(notePreferencesRepository: NotePreferencesRepository): INotePreferencesRepository
}
INotePreferencesRepository
interface INotePreferencesRepository {
fun setTheme(theme: String)
fun getTheme(): String?
}
NotePreferencesInteractor
class NotePreferencesInteractor #Inject constructor(private val notePreferencesRepository: INotePreferencesRepository): NotePreferencesUseCase {
override fun setTheme(theme: String) {
notePreferencesRepository.setTheme(theme)
}
override fun getTheme(): String? = notePreferencesRepository.getTheme()
}
NotePreferencesUseCase
interface NotePreferencesUseCase {
fun setTheme(theme: String)
fun getTheme(): String?
}
SettingViewModel
#HiltViewModel
class SettingViewModel #Inject constructor(private val notePreferencesUseCase: NotePreferencesUseCase): ViewModel() {
fun getTheme() = notePreferencesUseCase.getTheme()
fun setTheme(theme: String) {
notePreferencesUseCase.setTheme(theme)
}
}
SOLVED
forgor to add AppModule
#Module
#InstallIn(ViewModelComponent::class)
abstract class AppModule {
#Binds
#ViewModelScoped
abstract fun provideNotePreferencesUseCase(notePreferencesInteractor: NotePreferencesInteractor): NotePreferencesUseCase
}
So view model can use usecase
How can i change my java code to kotlin code to view a pdf from firebase database
public class javaclasActivity extends AppCompatActivity {
private TextView text1;
private PDFView pdfView;
private FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mref = database.getReference("url");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
pdfView = (PDFView) findViewById(R.id.pdfView33);
text1 = (TextView) findViewById(R.id.text11);
mref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
text1.setText(value);
Toast.makeText(javaclasActivity.this, "updated", Toast.LENGTH_SHORT).show();
String url = text1.getText().toString();
new RetrivePdfStream().execute(url);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(javaclasActivity.this, "failed to load", Toast.LENGTH_SHORT).show();
}
});}
// i am facing a problen on this below code
class RetrivePdfStream extends AsyncTask<String, Void, InputStream> {
#Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
#Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream).load();
}
}
}
to this kotlin file
class pdfActivity : AppCompatActivity() {
lateinit var pdfview: PDFView
lateinit var text1 : TextView
var database:FirebaseDatabase=FirebaseDatabase.getInstance()
var mref :DatabaseReference=database.getReference("url")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pdf)
pdfview=findViewById(R.id.pdfView33)
text1=findViewById(R.id.text11)
}
}
the main problem is this
class RetrivePdfStream <String, Void, InputStream> {
You might want to fix some of the formatting here.
Also, if I understood correctly, you want to migrate Java to Kotlin.
For the RetrivePdfStream in Kotlin, you aren't inheriting from AsyncTask.
Do this like so:
class RetrivePdfStream : AsyncTask<String, Void, InputStream>() { ... }
I have to deal with the following java code
abstract class ScriptResult {
void ifSuccess(Consumer<Object> block) {
}
void ifError(Consumer<Throwable> block) {
}
static class Success extends ScriptResult {
private final Object returnValue;
Success(Object returnValue) {
this.returnValue = returnValue;
}
#Override
void ifSuccess(Consumer<Object> block) {
block.accept(returnValue);
}
}
static class Error extends ScriptResult {
private final Throwable throwable;
Error(Throwable throwable) {
this.throwable = throwable;
}
#Override
void ifError(Consumer<Throwable> block) {
block.accept(throwable);
}
}
}
My kotlin tests make use of the following assertion helper:
private lateinit var scriptResult: ScriptResult
inline fun <reified T : Throwable> shouldHaveThrown(): T {
scriptResult.ifSuccess { result ->
fail("should have thrown ${T::class.java.name}, but returned `$result´")
}
lateinit var holder: T // (1)
scriptResult.ifError { throwable ->
if (throwable is T) {
holder = throwable // (2)
} else {
fail("expected ${T::class.java.name} to be thrown, but threw `$throwable´")
}
}
return holder // (3)
}
The assertion helper is effective. I can use it like so:
val thrown = execution.shouldHaveThrown<MissingMethodException>()
assertThat(thrown.message).contains("missedMethod")
But I suspect there is a more idiomatic way to return throwable from shouldHaveThrown, then to (1) declaring a holder, (2) assigning it and (3) eventually returning it. How?
In class Parent.
interface Humenable{
fun Run()
}
In class Child.
fun main(args:Array<String>)
{
class Student : Humenable{
Overrides fun Run(){
println("Pass")
}
}
}
Try something like this:
interface Humenable {
fun Run()
}
class Student : Humenable {
override fun Run() {
println("Pass")
}
}
fun main(args: Array<String>) {
val student = Student()
student.Run()
}
Your mistakes:
You used Overrides instead override modifier (simple typo).
You made Student class declaration in fun {} scope.
Showing getInstance() unresolved reference when ever call.
class GlobalHolder {
private var pickerManager: PickerManager? = null
private val ourInstance = GlobalHolder()
fun getInstance() {
return ourInstance
}
private fun GlobalHolder(){}
fun getPickerManager(): PickerManager? {
return pickerManager
}
fun setPickerManager(pickerManager: PickerManager) {
this.pickerManager = pickerManager
}
}
Call
lateinit var pickerManager: PickerManager
pickerManager=GlobalHolder.getInstance().getPickerManager()
FYI
This is original Java class
public class GlobalHolder {
private PickerManager pickerManager;
private static GlobalHolder ourInstance = new GlobalHolder();
public static GlobalHolder getInstance() {
return ourInstance;
}
private GlobalHolder() {
}
public PickerManager getPickerManager() {
return pickerManager;
}
public void setPickerManager(PickerManager pickerManager) {
this.pickerManager = pickerManager;
}
}
You're willing to have a singleton, right? Make GlobalHolder an object, which will automatically provide a singleton:
object GlobalHolder {
private var pickerManager: PickerManager? = null
fun getPickerManager(): PickerManager? {
return pickerManager
}
fun setPickerManager(pickerManager: PickerManager) {
this.pickerManager = pickerManager
}
}
And then your instance like this:
GlobalHolder.getPickerManager()