Is it possible to use androidx.appcompat.app.ActionBarDrawerToggle WITHOUT toobar - android-actionbar

*android.useAndroidX=true
android.enableJetifier=true*
dependencies {
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation('com.crashlytics.sdk.android:crashlytics:2.7.0#aar') { transitive = true; }
I migrate to androidx package.
All project is success build except this:
package android.support.v7.app does not exist
private ActionBarDrawerToggle mDrawerToggle;
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_navigation_drawer, R.string.app_name, R.string.app_name)
in my activity:
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
public class MainNavigationDrawerFragmentActivity extends androidx.fragment.app.FragmentActivity {
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_navigation_drawer, R.string.app_name, R.string.app_name)
}
In new androidx.appcompat.app.ActionBarDrawerToggle I must use androidx.appcompat.widget.Toolbar.
But in my code I use action bar. So as result not compile.
The questions are:
Is it possible to use androidx.appcompat.app.ActionBarDrawerToggle WITHOUT toobar
Is it possible to use androidx.appcompat.app.ActionBarDrawerToggle WITH actionbar ?

Related

Import headers from c++ library in swift

I am learning how to communicate between swift and c++ for ios. As a first step I have looked on this example:
https://github.com/leetal/ios-cmake
There is an example-app that I have managed to compile and run. Took some time to get it to work. That is an objective-c project.
The next step is to create a new swift project and try and import the compiled library and use the headers in swift instead.
I have not managed to do that. I think the current problem is that I cannot include the header HelloWorldIOS.h.
import SwiftUI
import HelloWorldIOS.h <- No such module found
struct ContentView: View {
var body: some View {
Text(sayHello())
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I have tried to create a bridging file example-Bridging-Header.h as suggested here: https://developer.apple.com/documentation/swift/importing-objective-c-into-swift
It looks like:
//
// example-Bridging-Header.h
// example-swift
//
#ifndef example_Bridging_Header_h
#define example_Bridging_Header_h
#import "HelloWorldIOS.h"
#endif /* example_Bridging_Header_h */
I have also added the path to the headers in Target - Build Settings - Header Search Paths
The Objective-C Bridging Header looks like example-swift/example-Bridging-Header.h.
Are there any good instructions for how to call c++ code from a compiled library? I hoped this example I found would be easy to get to work.
The comment below helped me plus that I had to link to libc++.tbd.
You don't import anything in your Swift code when Objective-C headers are imported in the bridging header.
All public interfaces available from the imported files get available in the entire Swift module by default after that.
Sample listing
TDWObject.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
#interface TDWObject : NSObject
- (void)someCPPCode;
#end
NS_ASSUME_NONNULL_END
TDWObject.mm
#include <iostream>
#import "TDWObject.h"
#implementation TDWObject
- (void)someCPPCode {
std::cout << "Hello from CPP cout" << std::endl;
}
#end
Some-Bridging-Header.h
#import "TDWObject.h"
main.swift
TDWObject().someCPPCode()
Provided the main.swift file is the entry point of the program, it will print Hello from CPP cout.
You can use Scapix Language Bridge to automatically bridge C++ to Swift (among other languages). Bridge code automatically generated on the fly directly from C++ header files. Here is an example:
C++:
#include <scapix/bridge/object.h>
class contact : public scapix::bridge::object<contact>
{
public:
std::string name();
void send_message(const std::string& msg, std::shared_ptr<contact> from);
void add_tags(const std::vector<std::string>& tags);
void add_friends(std::vector<std::shared_ptr<contact>> friends);
void notify(std::function<bool(std::shared_ptr<contact>)> callback);
};
Swift:
class ViewController: UIViewController {
func send(friend: Contact) {
let contact = Contact()
contact.sendMessage("Hello", friend)
contact.addTags(["a","b","c"])
contact.addFriends([friend])
contact.notify() {
(c: Contact) in
//...
return true
}
}
}
Disclaimer: I am the author of Scapix Language Bridge.

cannot find implementation for com.application.unittest.data.local.ShoppingItemDatabase. ShoppingItemDatabase_Impl does not exist

I have a problem with Room test I have checked all stackoverflow solutions, but nothing works for me.
I'm so beginner at test.
I tried a lot of solution like change kapt and add other libraries for room
This is the Error:
java.lang.RuntimeException: cannot find implementation for com.application.unittest.data.local.ShoppingItemDatabase. ShoppingItemDatabase_Impl does not exist
at androidx.room.Room.getGeneratedImplementation(Room.java:100)
at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1486)
at com.application.unittest.data.local.ShoppingDaoTest.setup(ShoppingDaoTest.kt:40)
this is my DB class:
#Database(entities = [ShoppingItem::class], version = 1)
abstract class ShoppingItemDatabase:RoomDatabase() {
abstract fun shoppingDao():ShoppingDao
}
This is the interface:
#Dao
interface ShoppingDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertShoppingItem(shoppingItem: ShoppingItem)
#Delete
suspend fun deleteShoppingItem(shoppingItem: ShoppingItem)
#Query("SELECT * FROM shopping_items")
fun observeAllShoppingItems():LiveData<List<ShoppingItem>>
#Query("SELECT SUM(price * amount) FROM shopping_items")
fun observeTotalPrice():LiveData<Float>
}
This is the Test:
#ExperimentalCoroutinesApi
#RunWith(AndroidJUnit4::class)
#SmallTest
class ShoppingDaoTest {
#get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var database: ShoppingItemDatabase
private lateinit var dao: ShoppingDao
#Before
fun setup(){
database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
ShoppingItemDatabase::class.java
)
.allowMainThreadQueries()
.build()
dao = database.shoppingDao()
}
#After
fun teardown(){
database.close()
}
#Test
fun insertShoppingItem() = runTest {
val shoppingItem = ShoppingItem(id = 1,"name",1,2f,"image")
dao.insertShoppingItem(shoppingItem)
val allShoppingItems = dao.observeAllShoppingItems().getOrAwaitValue()
assertThat(allShoppingItems).contains(shoppingItem)
}
}
So what should I do?
UPDATE:
This is my dependencies. I guess maybe I missed something in my dependencies:
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
//Material Design
implementation "com.google.android.material:material:1.6.1"
//Architectural Component
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
//Lifecycle
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime:2.4.1"
//Room
implementation "androidx.room:room-runtime:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
annotationProcessor "androidx.room:room-compiler:2.4.2"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
kaptAndroidTest "androidx.room:room-compiler:2.4.2"
//Kotlin extension and Coroutines for Room
implementation "androidx.room:room-ktx:2.4.2"
//Retrofit
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
//Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1"
//Coroutines lifecycle scope
implementation "androidx.lifecycle:lifecycle-viewmodel:2.4.1"
//Navigation Component
implementation "androidx.navigation:navigation-fragment-ktx:2.4.2"
implementation "androidx.navigation:navigation-ui-ktx:2.4.2"
//Glide
implementation "com.github.bumptech.glide:glide:4.12.0"
kapt "com.github.bumptech.glide:compiler:4.12.0"
//Activity KTX for ViewModel
implementation "androidx.activity:activity-ktx:1.4.0"
//Dagger-Hilt
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-android-compiler:2.38.1"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"
//Timber
implementation "com.jakewharton.timber:timber:5.0.1"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//Local Unit Test
implementation "androidx.test:core:1.4.0"
testImplementation "org.hamcrest:hamcrest-all:1.3"
testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "org.robolectric:robolectric:4.3.1"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1"
testImplementation "org.mockito:mockito-core:2.28.2"
testImplementation "com.google.truth:truth:1.0.1"
testImplementation "androidx.room:room-testing:2.4.2"
androidTestImplementation "com.google.truth:truth:1.0.1"
//Instrumentation Unit Test
androidTestImplementation "junit:junit:4.13.2"
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.28.1"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1"
androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
androidTestImplementation "com.google.truth:truth:1.0.1"
androidTestImplementation "androidx.test.ext:junit:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
androidTestImplementation "org.mockito:mockito-core:2.28.2"
Do you have an application class to instantiate the room database?
if so make sure your android manifest has this line of code under application.
<application
android:name="com.application.unittest.(then you application class name)"
The app I built recently had the same error as yours but it worked after I put the application name in the manifest. I was not using TDD or Hilt though so I am not sure if this will work or not.
But the application class I used contained this:
class MyApplication: Application() {
val database: your database class by lazy { your database class).getDatabase(this) }
}
Then make sure the application name is in the manifest. I hope this helps.

Is casting necessary in Typescript for this simple case?

Say I have a test.ts and a MY_MODULE.d.ts file:
MY_MODULE.d.ts:
module MY_MODULE
{
export class Config {
UserId : string;
};
export function Init( config : Config );
}
test.ts:
/// <reference path="MY_MODULE.d.ts" />
MY_MODULE.Init(<MY_MODULE.Config>{ UserId: 'Josh' });
My question: Is it possible to fix either the definition file or the .ts file so that the cast in the latter is unnecessary?
Use export interface Config instead of export class Config.
Typescript will infer the correct type based on the signature of the object, so the cast is not necessary.
Copying the code into the playground, a couple of compile errors were immediately picked up. Firstly, Init needs an implementation.
Secondly, the Config class does not have a constructor.
The following code compiles cleanly:
module MY_MODULE
{
export class Config {
UserId : string;
};
export function Init( config : Config ) {
}
}
var myConfig = new MY_MODULE.Config();
myConfig.UserId = 'Josh'
MY_MODULE.Init(myConfig);
It would be better to define a constructor for Config as follows:
module MY_MODULE
{
export class Config {
UserId : string;
constructor(userId : string) {
this.UserId = userId;
}
};
export function Init( config : Config ) {
}
}
MY_MODULE.Init(new MY_MODULE.Config('Josh'));

Cannot cast from Graphics to Graphics2D

My program code is here:
package Chapter12;
import java.awt.*;
import javax.swing.*;
public class Pv2 extends JPanel {
public static void main (String []args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.setContentPane(new Pv2());
}
#Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
}
}
Eclipse shows this error when I'm trying to compile the code.
Cannot cast from Graphics to Graphics2D
This error occurs on this line :
Graphics2D g2 = (Graphics2D) g;
Tried jre 1.6 and jre 1.7 - the same thing. Why the cast isn't allowed? I even tried to download some prepared codes and the error was the same.
You code is not broken and this cast is surely possible. The following code works:
import java.awt.*;
import javax.swing.*;
public class Pv2 extends JPanel {
public static void main (String []args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
frame.setContentPane(new Pv2());
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(0,0, getWidth(), getHeight());
}
}
One of the possible explanations, you have the class named exactly Graphics or Graphics2D in the same package (Chapter12). Then, with imports as you have, this class will have priority; if it is not compatible, the cast will be rejected by the compiler. I was able to reproduce this by crating the empty class in the same package and naming it Graphics2D.
To solve, use explicit imports: replace your import statements by
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
This will tell the compiler which Graphics and Graphics2D do you mean. Even with confusing Graphics2D class, I do not longer see the error after rewriting imports as shown.

D template specialization in different source file

I recently asked this question about how to simulate type classes in D and suggested a way to do this using template specialization.
I discovered that D doesn´t recognize template specialization in a different source file. So I couldn´t just make a specialization in a file not included from the file where the generic function is defined. To illustrate, consider this example:
//template.d
import std.stdio;
template Generic(A) {
void sayHello() {
writefln("Generic");
}
}
void testTemplate(A)() {
Generic!A.sayHello();
}
//specialization.d
import std.stdio;
import Template;
template Generic(A:int) {
void sayHello() {
writefln("only for ints");
}
}
void main() {
testTemplate!int();
}
This code prints "generic" when I run it. So I´m asking whether there is some good workaround, so that the more specialized form can be used from the algorithm.
The workaround I used in the question about Type classes was to mixin the generic functions after importing all files with template specialization, but this is somewhat ugly and limited.
I heard c++1x will have extern templates, which will allow this. Does D have a similar feature?
I think I can give a proper answer to this question. No.
What you are trying to do is highjack the functionality of template.d (also case should match on file and import Template, some operating systems it matters). Consider:
// template.d
...
// spezialisation.d
import std.stdio;
import template;
void main() {
testTemplate!int();
}
Now someone updates the code:
// specialization.d
import std.stdio;
import template;
import helper;
void main() {
testTemplate!int();
getUserData();
}
Perfect right? well inside helper:
// helper.d
getUserData() { ... }
template Generic(A:int) {
A placeholder; //...
}
You have now changed the behavior of specialization.d just from an import and in fact this would fail to compile as it can not call sayHello. This highjack prevention does have its issues. For example you may have a function which takes a Range, but the consumer of your library can not pass an array unless your library imports std.array since this is where an array is "transformed" into a range.
I do not have a workaround for your problem.
Michal's comment provides a solution to the second form of highjacking, where say specialization.d tried to highjack getUserData
// specialization.d
import std.stdio;
import template;
import helper;
alias helper.getUserData getUserData;
string getUserData(int num) { ... }
void main() {
testTemplate!int();
getUserData();
}
IIRC; as a general matter in D, symbols in different files can't overload because the full name of a symbol includes the module name (file name) making them different symbols. If 2 or more symbols have the same unqualified name and are from 2 or more files, attempting to use that unqualified symbol will result in a compile error.